android custom radio button not getting checked - android

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.

Related

EditText can't edit in android ListView

Currently working with listview which contains edittext in each row.
The row item xml is below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/result_solution_list_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/demo_product_background"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/twohundred"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/contentLinearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:descendantFocusability="afterDescendants"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/add_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:textColor="#color/home_dark_blue"
android:textSize="#dimen/list_text_size"
android:textStyle="bold" />
<ImageView
android:id="#+id/img_camera"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/ic_pre_camera"
android:contentDescription="#string/app_name" />
<ImageView
android:id="#+id/drag_handle"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:layout_margin="2dp"
android:background="#drawable/drag_icon"
android:contentDescription="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#color/cus_container" >
</LinearLayout>
In this xml, LinearLayout - contentLinearLayout can be inflated a view dynamically.
The inflating view (xml) contains the EditText views.
The following is the inflated xml .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="#dimen/five"
android:paddingRight="#dimen/five"
>
<TextView
android:id="#+id/artist_name_textview"
android:layout_width="#dimen/hundreadfifty"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/home_dark_blue" />
<LinearLayout
android:layout_width="#dimen/home_hundred"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/home_five"
android:layout_marginRight="#dimen/home_five"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_quan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="QUANTITY"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_quan"
android:layout_width="#dimen/home_eighty"
android:layout_height="match_parent"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
<LinearLayout
android:layout_width="#dimen/nav_drawer_uf_image_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_name_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true"
android:text="PRICE"
android:textStyle="bold"
android:textSize="#dimen/list_text_size"
android:textColor="#color/home_dark_blue" />
<EditText
android:id="#+id/txt_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edt_blue_background"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#color/home_dark_blue" />
</LinearLayout>
My problem is , I want both events. 1. ListView's OnItemClickListener 2. EditText should be editable.
I have tried the solutions posted in below links
Trying to catch a click on android ListView item: android:descendantFocusability="blocksDescendants" not working
Android EditText in ListView - keyboard
Focusable EditText inside ListView
http://androidtechnicalblog.blogspot.in/2014/04/quick-trick-of-week-edittext-inside.html
Most of the solutions stressed on android:descendantFocusability attribute.I have tried with different values and set to the listview and listview's root layout alone.
Still the EditTexts are not editable.
Any solutions ?
I have done this with the Imageview earlier.
At that time i put the click listner of Imageview at the Adapter.
I am not sure but may be this will help you.
set this at your adapter
viewHolder.txt_price.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your Code
}
});
your ListView click event
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
});
Not Sure might be help You. Good Luck

Android TextView inside RadioGroup Not Exactly Fitting

I do try to get text datas from JSON, i do. But when it is time to write they are not exactly fitting. My XML file is here...
<?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" >
<TextView
android:id="#+id/txt_questionAdjective"
android:layout_width="fill_parent"
android:layout_height="139dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp" />
<RadioGroup
android:id="#+id/grp_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rdb_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rdb_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
And what it looks like after that.
How can i set C and D options next to radiobuttons ? Thanks in advance.
EDIT:SOLVED I deleted the textView parts in XML and used the for loop which #Gabriella Angelova provided me. That really helped. Thanks.
You could use the following code snipet to set the text of your radio buttons dynamically:
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.grp_options);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(*your value from JSON*);
}
radioButton.setText("your text");

how to arrange 2*2 radiogroup

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.

Android Button loses its Text Alignment

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.

View.OnClickListener() in Android responding for the wrong button

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.

Categories

Resources