I am making a quiz app hence I wanted my MenuButton and NextButton view Invisible at the beginning but after satisfying a particular condition I wanted the View to be visible again hence I used findViewById(R.id.MenuButton).setVisibility(View.VISIBLE); and same for the NextButton but still these view were Invisible.What am I doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="#+id/Question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="TextView"
android:gravity="center"
android:textSize="25dp"
android:textColor="#000000"/>
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:text="RadioButton1"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:layout_marginTop="54dp"
android:layout_below="#+id/Question"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="On_RadioButton1_Click"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_below="#+id/radioButton4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/MenuButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:textSize="20dp"
android:text="menu" />
<Button
android:id="#+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginLeft="125dp"
android:text="next" />
</LinearLayout>
</RelativeLayout>
//Java file
private int Question_no=0;
private Boolean Boolean_Var=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
String[] Question_Array = getResources().getStringArray(R.array.Question1);
TextView Questions = (TextView) findViewById(R.id.Question);
Questions.setText(Question_Array[Question_no]);
String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
Radio_Button1.setText(Radio_Button1_Array[Question_no]);
findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}
public void On_RadioButton1_Click(View view)
{
if(Boolean_Var==false)
{
String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
String CorrectAns = CorrectAns_Array[Question_no];
String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
String Answer = Answer_Array[Question_no];
if(Answer.equals(CorrectAns))
{
RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
Right_Ans.setTextColor(Color.GREEN);
AnswerSubmitted();
}
else
{
RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
Wrong_Ans.setTextColor(Color.RED);
GreenTick();
AnswerSubmitted();
}
}
Boolean_Var=true;
}
public void AnswerSubmitted()
{
findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
You should get a reference to your buttons and call setVisibility on the reference. What you're currently doing is setting visibility INVISIBLE on one reference and setting it VISIBLE on another.
Button nextButton;
//in onCreate
nextButton = (Button)findViewById(R.id.nextButton);
nextButton.setVisibility(View.INVISIBLE);
//in AnswerSubmitted
nextButton.setVisibility(View.VISIBLE);
I think you should tell android that you want to execute that method when your radiobutton is clicked. by adding :
Radio_Button1.setOnClickListener(new OnClickListener (){
public void onClick(View v) {
On_RadioButton1_Click(v);
}
});
So you can get that method executed everytime you click.
Add it at the end of OnCreate
Related
I'm trying to get text from selected radiobutton. this code was working fine yesterday but now it's returning null and I don't understand why or what's suddenly causing it.
Renaming variable, rewriting the code, re define variable, re initializing it does nothing to solve this.
the XML
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</LinearLayout>
</RadioGroup>
Activity
private RadioGroup radioGroup;
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.murid_detail_activity);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
getRadioChecked();
}
public void getRadioChecked(){
int sexMurid = radioGroup.getCheckedRadioButtonId();
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
jenisKelamin = rbSelected.getText().toString();
}
This is really fine yesterday, I just don't know what's causing it? can anyone help?
This occurs when RadioButton is not a direct child of RadioGroup. In your code the direct child of RadioGroup is LinearLayout. Try with the RadioGroup tag inside of LinearLayout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtKelaminMurid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jenis Kelamin"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioGroupMurid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/radioMuridLaki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Laki-Laki" />
<RadioButton
android:id="#+id/radioMuridPerempuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Perempuan" />
</RadioGroup>
</LinearLayout>
The return type of radioGroup.getCheckedRadioButtonId() (as in the docs) is an int. An int cannot be null, so I think you should rely on -1.
Edit: If you're not sure how an Android component works, just check the source code :) For the RadioGroup, see for example: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/RadioGroup.java#RadioGroup.clearCheck%28%29
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroupMurid);
RadioButton rbSelected = (RadioButton) findViewById(sexMurid);
RadioButton radioMuridPerempuan = (RadioButton) findViewById(radioMuridPerempuan);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.sexMurid) {
jenisKelamin = rbSelected.getText().toString();
else if (checkedId == R.id.radioMuridPerempuan) {
jenisKelamin = radioMuridPerempuan.getText().toString();
}
}
});
I would like to ask on how to achieve to wrap a content in alertdialog?
Because the current output of my dialog has an excess white field.
Hope someone can help me to understand this problem thanks.
Here is my activity_dialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:minWidth="10dp"
android:minHeight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.bloxofcode.toggle.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:background="#color/colorHeaderDialog"
android:textColor="#android:color/white"
android:text="#string/select_gender"
android:padding="15dp"
android:gravity="center_horizontal|left"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:padding="15dp"
android:text="Sample"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonMale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toggleButtonFemale"
android:textOn=""
android:textOff=""
android:focusable="false"
android:background="#drawable/check_male"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="#+id/btnCancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textSize="20dp"
android:padding="30dp"
android:textColor="#android:color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accept"
android:textSize="20dp"
android:padding="30dp"
android:background="#color/colorDialogOK"
android:textColor="#android:color/white"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is some of my implementation in the MainActivity.java:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.activity_dialog, null);
.....
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
Try Creating a dialog like this. this works perfect and then right away your dialog logic is seperated from you activity logic
public class CustomDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public CustomDialog d;
public Button yes, no;
public CustomDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//dosomething
}
});
}
}
Hope it is not correct to wrap the content of dialog as it might disrupt the view in tablets and larger devices.
Anyhow, basing upon your requirement.. hope this link works
AlertDialog with custom view: Resize to wrap the view's content
my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});
I have a button that brings up a dialog with an OK button, and I'm getting a nullpointerexception when I click on the OK button. Does anyone know what's wrong?
Here's my java code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
myListView = (ListView)findViewById(R.id.list);
new GetStuff().execute();
Button importButton = (Button)findViewById(R.id.doButton);
importButton.setEnabled(false);
importButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog pwdDialog = new Dialog(context);
pwdDialog.setContentView(R.layout.pwdentry);
pwdDialog.setTitle("Enter password");
TextView pwdText = (TextView)pwdDialog.findViewById(R.id.pwdText);
pwdText.setText("Enter password");
Button okBut = (Button)findViewById(R.id.okBut);
okBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pwdDialog.dismiss();
}
});
pwdDialog.show();
}
});
}
Here's the XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/pwdText"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Enter password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/pwdEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/pwdText"
android:ems="10"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/okBut"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_below="#+id/pwdEntry"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:text="OK" />
</RelativeLayout>
Thanks!
The problem is okBut will be null
Button okBut = (Button)findViewById(R.id.okBut);
you need to make this as
Button okBut = (Button)pwdDialog.findViewById(R.id.okBut);
Here you are retrieving a Button by using the id, R.id.okBut from your main layout R.layout.mylist.
Button okBut = (Button)findViewById(R.id.okBut);
pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.