I am making an app in which I want to design the options in 2*2 radio buttons.For this I am doing the following thing but getting "android.widget.RelativeLayout cannot be cast to android.widget.RadioButton" error
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</TableRow>
<TableRow>
<TextView android:id="#+id/textMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="100dp"
android:gravity="center|left"/>
</TableRow>
<TableRow>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/group"
android:layout_marginLeft="25dp"
android:orientation="horizontal"
android:layout_span="2"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btn1"/>
<RadioButton android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn1" />
<RadioButton android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn2"
android:layout_toRightOf="#id/btn3"/>
</RelativeLayout>
</RadioGroup>
</TableRow>
<Button android:id="#+id/submitbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:visibility="gone"
android:layout_below="#id/group" />
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/submitbtn"/>
</TableLayout>
Please help in this I am stuck since morning.
First, it depends on Your needs. But You can do it like this: Juste don´t use RadioGroup, instead use two LinearLayouts:
<LinearLayoutLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RadioButton android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayoutLayout>
<LinearLayoutLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1">
<RadioButton android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton android:id="#+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayoutLayout>
Then You can refer it programatically and deselect all other buttons, if one is pressed:
yourFirstRadioButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
YourSecondRadioButton.setChecked(false);
YourThirdRadiobutton.setChecked(false);
YourFourthRadioButton.setChecked(false);
}
});
You have to do this for all Your radio buttons. This is just a simple example, You can do it in a better way, for example with a for loop. For example, after creating RadioButtons, initialize a RadioButtons Array:
private RadioButton[] radioButtons;
initialize after initialize all radioButtons:
radioButtons = {radioButton1,radioButton2,radioButton3,radioButton4};
And the, make a setSelected(RadioButton button) method:
private void setSelected(RadioButton button){
for(int i=0;i<radioButtons.length;i++){
if(radioButtons[i]!=button){
radioButtons[i].setChecked(false);
}
}
}
and call this in Your onClick():
yourFirstRadioButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
setSelected(yourFirstRadioButton);
}
});
It´s possible that I have some little wrong coding here, because I can´t test it at the moment, have no IDE here. But just give it a try and come back if it doesn´t work.
1.remove radiogroup.
2.take parent layout as relative
3.take 4 radio button as required
4.write onclicklistener for all radio button
5.In radio button-1 onclicklistener event disable other button and do that for other(set check for particular button and for other set unchecked)
Remove Radio Group, implements onClickListener for RadioButtons and maintain the active Radio Button Id. Like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/btn1"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn1"
android:onClick="onClickButton" />
<RadioButton
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/btn2"
android:layout_toRightOf="#id/btn3"
android:onClick="onClickButton" />
</RelativeLayout>
Then, In ur activity implements "onClickButton" method mentioned to the OnClick attribute of RadioButtons:
private RadioButton checkedRadioButton;
// OnClick listener for Radio Buttons
public void onClickButton(View view) {
RadioButton radioButton = (RadioButton) view;
if (checkedRadioButton != null) {
checkedRadioButton.setChecked(false);
}
radioButton.setChecked(true);
checkedRadioButton = radioButton;
}
// Get the Active radio button ID
private int getCheckedRadiButtonId() {
if (checkedRadioButton != null) {
return checkedRadioButton.getId();
}
return 0;
}
U can get the current active radioButton Id using getCheckedRadiButtonId.
Related
I need your help to solve this problem:
I have a linear layout with horizontal orientation. Inside this layout I have 5 textviews with visibility:gone. Above this layout I have a radio group with 5 radio button aligned horizontally.
When I check a radio button the corresponding textviews changes his visibility in visible. I want to show a text view below every radio button. Actually they are alle in the same position.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView />
<TextView />
<TextView />
<TextView />
<TextView />
</LinearLayout>
How can I do that?
Set LinearLayout weigthSum to 1.0. Make each textview weight to 0.2 & visibility to invisible. View with gone visibility doesn't take any space.
I made second textview visible. But textview with text "1" will occupy space before textview with text of "2".
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.techdesire.HomeActivity"
tools:showIn="#layout/app_bar_home">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_layout">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/value_group">
<RadioButton
android:text="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton1"
android:layout_weight="0.2"/>
<RadioButton
android:text="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="0.2"/>
<RadioButton
android:text="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_weight="0.2"/>
<RadioButton
android:text="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton4"
android:layout_weight="0.2"/>
<RadioButton
android:text="5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/radioButton5"
android:layout_weight="0.2"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_container"
android:orientation="horizontal"
android:layout_below="#+id/radio_layout"
android:weightSum="1.0">
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="1"/>
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="visible"
android:text="22345678dsada"
android:maxLines="1"
android:ellipsize="marquee"/>
<TextView
android:id="#+id/text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="3"/>
<TextView
android:id="#+id/text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="4"/>
<TextView
android:id="#+id/text5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:visibility="invisible"
android:text="5"/>
</LinearLayout>
</RelativeLayout>
On Radio group set onCheckChangeListener then compare id of checked radio button and according to it hide/show textview.See following
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/*hide all*/
TextView tv;
if(checkedID==R.id.radioButton1)
tv=(RadioButton)findViewById(R.id.text1);
elseif(checkedID==R.id.radioButton2)
tv=(RadioButton)findViewById(R.id.text2);
/* Similarly other else conditions*/
tv.setVisibility(View.VISIBLE);
}
});
Use visibility:Invisible instead of gone, because with gone they don't use any screen so the rest are not really there when the selected one appears, and it will appear at the first position.
Hope this helps.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// set visibility in visible according to Id by using if condition Or switch
}
});
I have created radio group with custom layout i.e. custom button.
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
android:id="#+id/radio_group_rating"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="3" >
<RadioButton
android:id="#+id/radio_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/platinum" />
<RadioButton
android:id="#+id/radio_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/gold" />
<RadioButton
android:id="#+id/radio_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#null"
android:clickable="true"
android:drawablePadding="10dp"
android:drawableTop="#drawable/star_small_dis"
android:gravity="center"
android:text="#string/silver" />
</LinearLayout>
</RadioGroup>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/radio_group_rating"
android:text="#string/submit" />
And in Activity, I have below code,
final Dialog dialog = new Dialog(mActivity);
dialog.setContentView(R.layout.rating_cust_dialog_layout);
radioGroupRating = (RadioGroup) dialog.findViewById(R.id.radio_group_rating);
Button btnSubmit = (Button) dialog.findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int selectedRadioId = radioGroupRating.getCheckedRadioButtonId();
View radioButton = radioGroupRating.findViewById(selectedRadioId);
Integer selctedPosition = radioGroupRating.indexOfChild(radioButton);
dialog.dismiss();
}
});
dialog.show();
My issue is radio buttons are not getting clicked. I thought it's because of android:button="#null" So I replaced it with android:button="#drawable/star_dis" but still not getting clicked.
Its the android:button="#null", please remove it from the xml file completely.
Try using this simple xml layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radio_group_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio_platinum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="platinum" />
<RadioButton
android:id="#+id/radio_gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gold" />
<RadioButton
android:id="#+id/radio_silver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="silver" />
</RadioGroup>
<Button
android:id="#+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="submit" />
</LinearLayout>
Only difference to my code is that I don't put clickable="true" in xml, if RadioGroup handles touch event by itself to manage child radio buttons you should remove that attribute as clickable is generally set to true automatically when you set an OnClickListener.
I have some buttons and a Spinner in my Layout. I Play Music on button click. When Button is pressed then button text gets aligned to left. I don't know why its happening. If I comment button click listener and then if I change items in Spinner , then also it gets Left Aligned.
Here is the part of my code.
<?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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Spinner
android:id="#+id/spnList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="5dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnCall"
android:layout_weight="1"
android:text="C" />
<Button
android:id="#+id/btnCut"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btn1"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Here is Activity Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
spnList = (Spinner) findViewById(R.id.spnList);
List<String> list = new ArrayList<String>();
list.add("First");
list.add("Second");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
BabyMobile1.this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnList.setAdapter(adapter);
spnList.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
activeItem = (String) arg0.getItemAtPosition(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are not defining width and height attributes on your buttons. Try with adding below attributes.
android:layout_width="0dp"
android:layout_height="wrap_content"
Example:
<Button
android:id="#+id/btnCall"
android:layout_weight="1"
android:text="C"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
Edit: Answer above gives a solution to keep texts of buttons center horizantal but their texts are shifted up when I tested. I searched this a while and found similar problem (not answered yet) at stack. I guess there is a small bug when using Spinners(or dialogs) inside a RelativeLayout. I tried to change your layout below(changed root to LinearLayout) and it worked as expected. If you have a chance to replace your layout with mine(below) your problem will be solveld. But I have no idea why using RelativeLayout causes a problem like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="#+id/spnList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnCall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<Button
android:id="#+id/btnCut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</LinearLayout>
Check the alignments of the button and spinner, if you are using Custom items in spinner then you should check the custom item alignment also.
better understanding purpose give me the code what u are writeing and where you are getting problem?
check this link it may helps you.
http://developer.android.com/reference/android/widget/Spinner.html#attr_android:gravity
here there is a tag in Spinner
android:gravity=" "
using this align the selected item in required position.
<RadioGroup>
<TableRow>
<RadioButton
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="lolo"
/>
<RadioButton
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="sh"
/>
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="s"
/>
<RadioButton
android:id="#+id/d"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="cannot be determined"
/>
</TableRow>
</RadioGroup>
i want to select one rdio button out of four it is acing like 4 seperate radio button??? ,all r getting selected,i'm new in android develoment so kindly guide me thanx
The RadioButton views must be immediate children of the RadioGroup, for the group-effect to work.
So, if you add 'TableRow' under RadioGroup, the group nature that should be seen on RadioButton will not function.
For RadioButton's grouping to work, try something like this.
<?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:gravity="center" >
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="lolo" />
<RadioButton
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="sh" />
<RadioButton
android:id="#+id/c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="s" />
<RadioButton
android:id="#+id/d"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="cannot be determined" />
</RadioGroup>
</RelativeLayout>
If you are so concerned about the row-column structure, then use your own layout XML file itself and implement setOnCheckedChangeListener(listener) on the radio buttons. When any of the radiobutton gets clicked, corresponding listener block will be called and then uncheck other radio buttons.
I think you have used radio group and define radio button inside that group this is correct but refer this link Radio Button Group this might be useful
try this:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<RadioButton
android:id="#+id/a"
android:text="lolo"
android:button="#drawable/btn_touch"/>
<RadioButton
android:id="#+id/b"
android:text="sh"
android:button="#drawable/btn_touch"/>
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/c"
android:text="s"
android:button="#drawable/btn_touch"/>
<RadioButton
android:id="#+id/d"
android:text="cannot be determined"
android:button="#drawable/btn_touch"/>
</TableRow>
</RadioGroup>
where btn_touch in drawable folder is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="#drawable/selected1"
android:state_checked="true"/>
<!-- When not selected, use white-->
<item android:drawable="#drawable/unselected1"/>
</selector>
Maybe this could help you if you want to keep the buttons on split rows:
1. Remove the group from your xml since you don't need it :
<TableRow>
<RadioButton
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="lolo" />
<RadioButton
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="sh" />
</TableRow>
<TableRow>
<RadioButton
android:id="#+id/c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="s" />
<RadioButton
android:id="#+id/d"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="cannot be determined" />
</TableRow>
2. Use this in your activity to make it work:
private RadioButton a;
private RadioButton b;
private RadioButton c;
private RadioButton d;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
ButtonChange clRadio = new ButtonChange();
a = (RadioButton) findViewById(R.id.a);
b = (RadioButton) findViewById(R.id.b);
c = (RadioButton) findViewById(R.id.c);
d = (RadioButton) findViewById(R.id.d);
a.setOnCheckedChangeListener(clRadio);
b.setOnCheckedChangeListener(clRadio);
c.setOnCheckedChangeListener(clRadio);
d.setOnCheckedChangeListener(clRadio);
...
}
public class ButtonChange implements OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton radio, boolean isChecked) {
if(isChecked)
switch(radio.getId()) {
case R.id.a:
case R.id.b:
case R.id.c:
case R.id.d:
a.setChecked(false);
b.setChecked(false);
c.setChecked(false);
d.setChecked(false);
radio.setChecked(true);
break;
default :
break;
}
}
}
}
This should do the trick.
I am having an issue registering a call when a button is clicked.
I have 2 EditTexts, one Button and one CheckBox. I would like to get the onClick() function called when the Button is pressed. Unfortunately, it only happens when the CheckBox is pressed, but it's not the correct ID so I am confused.
Here is the java code:
Button connect = (Button) findViewById(R.id.connectToServer);
connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
....
}
As you can see, I look for the id "connectToServer".
This is my .xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffb6b6b6"
android:textColor="#ffffffff" android:padding="2px" android:text="Settings" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="13px">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="25px"
android:textColor="#ffffffff" android:text="Set server settings" />
<TableLayout android:layout_marginTop="10px"
android:stretchColumns="1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView android:text="IP address:" />
<EditText android:id="#+id/serverIP" android:maxLength="15" />
</TableRow>
<TableRow>
<TextView android:layout_marginTop="4px" android:text="Port:" />
<EditText android:id="#+id/serverPort" android:maxLength="15" />
</TableRow>
</TableLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_marginTop="10px" android:orientation="horizontal"
android:layout_height="fill_parent" android:gravity="center">
<Button android:text="Connect" android:id="#+id/connectToServer"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffb6b6b6"
android:textColor="#ffffffff" android:padding="2px"
android:layout_marginTop="10px" android:text="Availability" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="13px">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="25px"
android:textColor="#ffffffff" android:text="Set device available" />
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TableRow>
<TextView android:layout_marginTop="10px"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Uncheck to disallow anyone from\nremotely connecting to the device." />
<CheckBox android:id="#+id/checkbox" android:gravity="right"
android:checked="true" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="fill_parent" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
The important line I believe would be this:
<Button android:text="Connect" android:id="#+id/connectToServer"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
but the code only gets called when the CheckBox button is checked or unchecked. I am not sure why that is.
Thank you very much!
Try this,
Button connect = (Button) findViewById(R.id.connectToServer);
connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
there is much simpler way to handle click events
1) add android:onClick to assign the function like that
<Button android:id="#+id/connectToServer"
android:text="Connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick_connectToServer"
/>
2) implement onClick inside your activity
public void onClick_connectToServer(View v) {
String tag = "" + v.getTag();
// here you can handle the action
}
in this case, you don't need to add onClickListener explicitly.
Social Coding # AspiroTV
I ran into the same problem. I changed the button order within XML and the onClickListener started calling the wrong event. The view looked correct from the device but the View IDs in R.java were incorrect.
Using Eclipse: I simply did Project->Clean...
The event listeners started working correctly.