RadioGroup not responding in onCheckedChanged - android

I have the following code :
private void inflateCustomView()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customNav = inflater.inflate(R.layout.custom_view, null);
//Bind to its state change
RadioGroup radioGrp = ((RadioGroup)customNav.findViewById(R.id.radio_nav));
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.moneyRadioBtn:
isInCashMode = true;
break;
case R.id.unitRadioBtn:
isInCashMode = false;
break;
}
setCashOrUnits(isInCashMode);
}
});
//Attach to the action bar
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
The custom view seems to be correctly added but onCheckedChanged is never called. I tried inserting a breakpoint inside onCheckedChanged , but it was never reached. What am I doing wrong?
edit:
Here's the XML of the custom_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center_vertical"
android:orientation="horizontal"
>
<RadioGroup
android:id="#+id/radio_nav"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="#+id/moneyRadioBtn"
android:text="Kr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
/>
<RadioButton
android:id="#+id/unitRadioBtn"
android:text="Enheder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
/>
</RadioGroup>
</LinearLayout>

Check with below code...
RadioGroup radioGrp = ((RadioGroup)customNav.findViewById(R.id.radio_nav));
int checkedRadioButtonID = radioGrp.getCheckedRadioButtonId();
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.moneyRadioBtn:
isInCashMode = true;
break;
case R.id.unitRadioBtn:
isInCashMode = false;
break;
}
setCashOrUnits(isInCashMode);
}
});
Notice,
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
Hope, this helps..

You can check like this also.
radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
RadioButton radioButton = (RadioButton) findViewById(radioGroup
.getCheckedRadioButtonId());
if (radioButton.getText().equals("radiobutton name")) {
isInCashMode = true;
} else {
isInCashMode = false;
}
}
});
Just try. it may help you.

You should probably paste in your XML layout, sometimes there can be problems there that stop the Radio Group from picking up the buttons within.
It's been a while since I worked with them, but I believe in the XML that Buttons have to be directly under the parent RadioGroup in XML, so if you have for example, a RadioButton within a LinearLayout within a RadioGroup, the RadioGroup won't pick up the button because it's not a direct child.
Edit: An alternative is to get a reference to each individual RadioButton in your code, define a CompoundButton.OnCheckedChangeListener to handle the event, and then call
myRadioButton.setOnCheckedChangeListener(myOnCheckChangeListener);
for each of them. A bit clunky (you'll have to manually get the id of the checked view in the onCheckedChanged() method) but it works!

I ran into this problem today and as nonsensical as it seems:
It requires CheckBox.OnCheckedChangeListener. RadioGroup blatantly refuses to accept RadioGroup.OnCheckedChangeListener but works fine with the CheckBox one.
I don't even KNOW what is going on but it works like this. Barely.

Related

How to Get Parent Radio Group of the checked Radio Button?

How can I retrieve radio group of the radio button that is checked? I have two radio groups with some radio buttons and I would like to know which group they belong to.
void populateSpinner(#ArrayRes int radioId, RadioGroup radioGroup) {
adapter = ArrayAdapter.createFromResource(SecondActivity.this,
radioId, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_districts.setAdapter(adapter);
}
public void onRadioButtonClick(View view)
{
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.radio_1:
if (checked)
populateSpinner(R.array.radio_1_array, <RADIO BUTTON GROUP>);
break;
// so on
How can I get the Radio Button Group?
You can set tag for each of your RadioGroup in your xml code , and when you click any RadioButton, you can get the Parent RadioGroup's Tag and easily you can identify the parent RadioGroup
Your activity_layout XML code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="FirstRadioGroup"
android:id="#+id/firstRG">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="#+id/firstRB"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="#+id/secondRB"/>
</RadioGroup>
JAVA Activity Code
RadioGroup firstRG = (RadioGroup) findViewById(R.id.firstRG);
firstRG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.firstRB:
Toast.makeText(MainActivity.this, "Parent RadioGroup is --"+radioGroup.getTag(), Toast.LENGTH_SHORT).show();
break;
case R.id.secondRB:
break;
}
}
});
Like this when you will check the first RadioButton it will show you the Prent RadioGroup Tag Value

Android : flowlayout inner button with close icon onclick event

I have a requirement to create a button with close icon like as below:
i have use button and i have fatch problem when i click on button remove button.
but my requirement is onclick close icon when remove form list.
so please help me which control use so my problem solution.
Use this code it will solved your problem
buttondelete.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (buttondelete.getRight() - buttondelete.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
return true;
}
}
return false;
}
});
i would add to flow layout customLaout which contains of grey background and linear layout with 2 elements inside. TextView and ImageView.
Both has own ClickListeners
something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="Example Text"
android:id="#+id/buttonTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/buttonExitIcon"
android:src="#drawable/your_image_source"
/>
</LinearLayout>
and add Click listeners for both imageView and TextView
{
RelativeLayout mainView = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
Button bt_close = (Button)child.findviewById(R.id.btnclose);
bt_put.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout pView = (RelativeLayout)v.getParent();
mainView.removeView(pView);
}
});
mainView.addView(child);
}
Create a child.xml using the RelativeLayout with childs (TextView for text and Button as Remove button)
Inflate the xml layout , add each views into your main_layout and
setonclicklistner to button.
when clicked on button , get the parent view from the clicked view and remove the view from main_layout.
this may help you.

How to set value of a radio button inside a radio group android

I'm trying to use radio groups where it has 2 radio buttons inside. I wanted to set a value for each radio buttons like for example the value is male and female. After that when I have already click on of my radio button it will display the value selected. How can you achieve this? Thank you for the help.
i have tried something like this just to test on how will i set a value
public void onRadioButtonClicked(View radioButton) {
int count = radioGroup.getChildCount();
for (int i = 0; i < count; i++) {
View o = radioGroup.getChildAt(i);
if (o instanceof RadioButton) {
RadioButton radioBtn = (RadioButton)o;
// get the state
boolean isChecked = radioBtn.isChecked();
// to set the check
radioBtn.setChecked(true);
}
}
}
but i think its not what i'm looking for.
i have already tried radioGroup.setId() assuming that the radio buttons have values already but it just display nonsense number.
You should use a RadioGroup and an OnCheckedChangeListener in your code to detect when a RadioButton is selected. In your layout XML:
<RadioGroup
android:id="#+id/radioGroup"
... >
<RadioButton
android:id="#+id/radioButton1"
... />
<RadioButton
android:id="#+id/radioButton2"
... />
</RadioGroup>
In your Activity/Fragment, set up like so:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangedListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
<type> value = <default value>;
switch (checkedId) {
case R.id.radioButton1:
value = ...;
break;
case R.id.radioButton2:
value = ...;
break;
}
// do something with value
}
});
Make sure that your radio buttons are wrapped in a radio group like below:
<RadioGroup
android:id="#+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioGroupButtonMale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radioGroupButtonFemale"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</RadioGroup>
And in code, to set the value,
RadioGroup mySelection = (RadioGroup)findViewById(R.id.myRadioGroup);
int radioButtonId = mySelection.getCheckedRadioButtonId();
String selectedValue;
switch (radioButtonId)
{
case R.id.radioGroupButtonMale:
selectedValue = "Value for Male";
break;
case R.id.radioGroupButtonFemale:
selectedValue = "Value for Female";
break;
}

RadioButtons in RadioGroup arent working as intended

I have a RadioGroup with two RadioButton's:
<RadioGroup
android:id="#+id/main_radiogroup_lock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/main_layout_lock_dummy"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/main_radiobutton_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="110dp"
android:background="#drawable/custom_radiobutton_lock"
android:button="#null"
android:contentDescription="#string/imageview_description" />
<RadioButton
android:id="#+id/main_radiobutton_unlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="230dp"
android:background="#drawable/custom_radiobutton_unlock"
android:button="#null"
android:checked="true"
android:contentDescription="#string/imageview_description" />
</RadioGroup>
I initialize the RadioButton's and set an OnCheckedChangeListener:
...
this.radioButtonLock = (RadioButton) view.findViewById(R.id.main_radiobutton_lock);
this.radioButtonLock.setOnCheckedChangeListener(this);
this.radioButtonUnlock = (RadioButton) view.findViewById(R.id.main_radiobutton_unlock);
this.radioButtonUnlock.setOnCheckedChangeListener(this);
...
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
if(view.getId() == R.id.main_radiobutton_lock) {
Log.v("MainFragment", "Lock");
this.imageButtonLanguage.setEnabled(false);
this.buttonOpen.setEnabled(false);
this.buttonAutomatic.setEnabled(false);
this.buttonClose.setEnabled(false);
this.buttonFog.setEnabled(false);
this.checkBoxRed.setEnabled(false);
this.checkBoxGreen.setEnabled(false);
this.checkBoxBlue.setEnabled(false);
return;
} else if(view.getId() == R.id.main_radiobutton_unlock) {
Log.v("MainFragment", "Unlock");
this.imageButtonLanguage.setEnabled(true);
this.buttonOpen.setEnabled(true);
this.buttonAutomatic.setEnabled(true);
this.buttonClose.setEnabled(true);
this.buttonFog.setEnabled(true);
this.checkBoxRed.setEnabled(true);
this.checkBoxGreen.setEnabled(true);
this.checkBoxBlue.setEnabled(true);
return;
}
}
So when i now start the app my radioButtonUnlock is checked. When i know chec the radioButtonLock nothing happens and no log output but the checked image is set so the checking is working. When i after that again check the radioButtonUnlock the following output appears:
02-04 09:59:31.355: V/MainFragment(533): Lock
02-04 09:59:31.355: V/MainFragment(533): Unlock
So both RadioButton's are fired. After that everytime i press one of the RadioButton's all events are fired. After pressing the radioButtonLock the following happens:
02-04 09:59:32.850: V/MainFragment(533): Unlock
02-04 09:59:32.850: V/MainFragment(533): Lock
Whats the point of that? I set different ID's so why are always both RadioButtons's fired?
Try this hope will work -
1)set check change listener to radio group
RedioGroup rg =(RadioGroup)findviewByid(R.id.main_radiogroup_lock);
rg.setcheckchangedlistener(true);
2)handle on check change listener event
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rdb1:
radio_selected = R.id.rdb1;
Toast.makeText(getApplicationContext(),"first",Toast.LENGTH_LONG).show();
case R.id.rdb2:
radio_selected = R.id.rdb2;
Toast.makeText(getApplicationContext(),"second",Toast.LENGTH_LONG).show();
}
}

Android: How to display an image file (building's floor map) and update the file?

I have read ImageView. I want to use it to display the image file. Also, I have plan to provide functionality to update the file.
Would you teach me on how to display the image file. Then add group of radio buttons on the top to represent the floors. When I click 1st Floor, the imageview source change to image1.png, then click 2nd floor radio button, change to image2.png and so on..
I am using Android Platform 2.1-update1 and eclipse ADT for this thesis.
Here's my current code: (although there is some undesirable appearance of the radio buttons)
public class DisplayImageMapActivity extends Activity {
ImageView iv;
private final String FLOOR = "F";
private final String storagePath = Environment.getExternalStorageDirectory() + "/keitaiAppH23";
private final String localMapsPath = storagePath + "/localMaps";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.storageimage);
RadioGroup levelRGroup = (RadioGroup) findViewById(R.id.level_rgroup);
int levelSize = 8;
for (int i = 0; i < levelSize; i++) {
RadioButton levelRButton = new RadioButton(this);
if(i==0) {
levelRButton.setText(new StringBuffer(i+1).append(FLOOR).append("(start)"));
} else if (i==7) {
levelRButton.setText(new StringBuffer(i+1).append(FLOOR).append("(end)"));
}
levelRButton.setTag((i+1) + FLOOR);
levelRButton.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
levelRGroup.addView(levelRButton);
}
levelRGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, final int checkedId) {
iv.setImageURI(Uri.parse(new StringBuffer(localMapsPath)
.append("/").append(group.findViewById(checkedId).getTag()).append(".gif").toString()));
iv.invalidate();
}
});
levelRGroup.getChildAt(0).performClick();
}
}
main.xml
<?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"
android:id="#+id/main_layout"
>
<RadioGroup android:id="#+id/level_rgroup"
android:layout_width="wrap_content" android:orientation="horizontal"
android:layout_height="wrap_content">
</RadioGroup>
<ImageView android:id="#+id/storageimage" android:src="#drawable/icon"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
UPDATE:
Requirements:
image file are set dynamically which obtain from external source
(e.g. DB,url). - Resolved
radiobuttons are need to be dynamically created because it is not fix by 8F. It should be the number of target floors (e.g. start is 5F, end is 7F, so it should be 3 radiobuttons only 5F,6F,7F). - Resolved
Problems Encountered:
Image cannot be updated. - Resolved
All radiobuttons are not displayed as expected (Maybe due to wrong layout). - Not Yet Resolved
When I click the radiobuttons to toggle the selection, it doesn't change on the 1F radiobutton. Note: It is OK for 2F to 8F. It switches the selection as expected. - Resolved
Screen shot:
These modifications should do it (there may be some typos):
Layout:
<?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"
android:id="#+id/main_layout"
>
<ImageView android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/iView" android:src="#drawable/icon"></ImageView>
<RadioGroup android:id="#+id/level_rgroup"
android:layout_width="fill_parent" android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton android:id="#+id/rb1"
android:width="106dip" android:height="80dip" android:text="Floor 1"/>
<RadioButton android:id="#+id/rb2"
android:width="106dip" android:height="80dip" android:text="Floor 2"/>
<RadioButton android:id="#+id/rb3"
android:width="106dip" android:height="80dip" android:text="Floor 3"/>
</RadioGroup>
</LinearLayout>
Code:
public class DisplayImageMapActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup levelRGroup = (RadioGroup) mainLayout.findViewById(R.id.level_rgroup);
final ImageView iView = (ImageView) findViewById(R.id.iView);
levelRGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, final int checkedId) {
switch (checkedId) {
case R.id.rb1:
iView.setImageResource(R.drawable.floor_1);
iView.invalidate();
break;
case R.id.rbM2:
iView.setImageResource(R.drawable.floor_2);
iView.invalidate();
break;
case R.id.rb3:
iView.setImageResource(R.drawable.floor_3);
iView.invalidate();
break;
}
}
});
}
}
You can add those buttons programatically if you want to, remember to set layout_width and other attributes for them if you want them to be seen (I put them in the XML since I had that code laying around).
Comment if you have any questions.

Categories

Resources