Android UI shows RadioButton as selected, although it is not - android

When my UI is recreated on orientation change, I use super.onCreate(savedInstanceState) and getLastNonConfigurationInstance() to access custom data I stored to fill the dynamic parts of my layout.
I have a RadioGroup which has two RadioButtons and is already defined in the XML file. The XML automatically makes the first one selected.
When an orientation change happens and the SECOND RadioButton is selected, everything seems to work fine; the second RadioButton is still selected in the UI.
But RadioGroup.getCheckedRadioButtonId() says the first RadioButton is selected. And I actually WANT the first one to be selected. But nothing changes when I call rb1.setChecked(true) - second one still shown as selected, and still the RadioGroup tells me the first one is selected (now it would make sense, but it's not shown).
This is REALLY strange behavior, does anyone have tips?
(edit)
Parts of my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.itemselected);
(...)
radioGroupServingType = (RadioGroup) findViewById(R.id.radioGroupServingType);
(...)
RadioButton radioOwnServing = (RadioButton) findViewById(R.id.radioOwnServing);
RadioButton radioUseServing = (RadioButton) findViewById(R.id.radioUseServing);
radioOwnServing.setOnClickListener(this);
radioUseServing.setOnClickListener(this);
//FIXME WTF
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
radioOwnServing.setChecked(true);
Log.d("", radioOwnServing.isChecked()+"/"+radioUseServing.isChecked()+" own/use checked");
If I select radioUseServing in the UI and change the orientation, the log says true/false own/use checked both times - although radioUseServing is shown as selected in the UI.
By the way, logcat also outputs
W/asset(4040): deep redirect failure from 0x0103003e => 0x02060007, defStyleAttr=0x0101007e, defStyleRes=0x0103001a, style=0x00000000
when changing the orientation, sometimes multiple times. I haven't found anything with google on what that means.

I already spoke with you on IRC, but I believe that if you get the view, and post a runnable to it that will call .setChecked(), this will in effect cause the setChecked call to occur at the proper time, and thus avoiding having called setChecked before the view hierarchy was ready for it.
Something like this:
final View myRadioButton = findViewById(R.id.myradiobutton);
myRadioButton.post(
new Runnable() {
#Override
public void run() { myRadioButton.setChecked(true); }
}
);

Related

Android radio button click already checked button

I've got an activity that lets users choose a "type" of a thing using radio buttons, but when modifying that thing I want to pre-check the radio button for the type the thing already is, and allow the user to click that checked radio button again if they don't want to change the type before moving to the next activity. I want to pre-check because users might not remember which type the thing was when they go to modify it.
The problem is that the listener only triggers when the option changes, not when the user clicks the option that's already checked. That makes sense, seeing as the listener is called "OnCheckedChangeListener," but when I try adding an OnClick listener to catch the click of a checked button, nothing happens. It simply doesn't trigger, and I don't see any other listeners on the RadioGroup object that seem like they'd do what I want.
Here's the checked change listener, which works fine:
rgThingType.setOnCheckedChangeListener { radioGroup, i ->
val checkedRadioButton = radioGroup?.findViewById(i) as? RadioButton
when(checkedRadioButton?.id) {
// ...
}
goToActivity()
}
And here's the on click listener, which never triggers no matter what:
rgThingType.setOnClickListener {
goToActivity()
}
Now, I can add an OnClick to each radio button to accomplish this, ie:
rbThingType1.setOnClickListener {
thingType = 1
if(/* extra logic to prevent double activity start */) {
goToActivity()
}
}
but that is awkward and seems like an anti-pattern, given I already have the checked change listener, and in fact it screws things up as it causes the next activity to be triggered twice since both listeners get called, forcing me to keep/check extra state to avoid. Is there a proper way to do this?
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnClickListener(v -> {
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = findViewById(selectedId);
String slectedValue=radioButton.getText()
});

android - Clearing cache of CheckBoxPreference

I am developing a PreferenceActivity where I can add CheckBoxPreference instances according to some dynamic content. I also maintain a count of the checked preferences in the activity. Everything is good when I load the application for the first time. But when I select some 2-3 check boxes and deploy the application code again on emulator, the previously selected check boxes just remain checked when I come back to same activity. The same happens when I move to and from some activities within the same application!
To prevent this, every time when I create an instance of CheckBoxPreference, I explicitly call .setChecked(false)!!! But still those previously checked preferences remain checked only!
I can't understand the behavior! Does it bring those values checked from cache! But I explicitly call .setChecked(false), what about that then?
Thank you very much,
Ketan
EDIT : Here is my code:
String content = null;
for (String title : titles) {
checkBoxPref = new CheckBoxPreference(this);
checkBoxPref.setKey(title);
checkBoxPref.setTitle(title);
<b>checkBoxPref.setDefaultValue(Boolean.FALSE); //Line # 1 set default to false
checkBoxPref.setChecked(false);//Line # 2 set checked = false </b>
content = config.getString(title, "Yet to define!");
if (content.length() > 30) {
content = content.substring(0, 30);
}
checkBoxPref.setSummary(content);
checkBoxPref.setOnPreferenceChangeListener(new MyCheckBoxChangeListener());
checkBoxPreferenceList.add(checkBoxPref);
inlinePrefCat.addPreference(checkBoxPref);
}
This code is executed everytime I app comes back to this activity! But still had I checked 2-3 checkboxes before leaving this activity, they will remain in the same state when app comes back to this! Please have a look at the code, I am already setting setChecked(false) explicitly, but still can't get them unchecked!
Please suggest some solution! I am stuck!
You will probably have to post some code to get a better answer. I had this same issue when I had my checkbox inside my adapter. I got around the issue by handling the checkbox with an onClickListener:
CheckBox menuOption = (CheckBox) v.findViewById(R.id.list_checkbox);
menuOption.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
CompoundButton cb = (CompoundButton) v;
// code to set preference goes here
// can check if true or false with cb.isChecked()
}});

android setBackgroundColor in runtime and other general confusion help?

i have a row of buttins created like this
i want to change the background colour at runtime in code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout track1 = (LinearLayout)findViewById(R.id.my_toggle_container);
for (int i = 0; i<32; i++) {
ToggleButton tgl = new ToggleButton(this);
tgl.setId(i);
...
track1.addView(tgl);
this names the id of the togglebuttons 1, 2, 3... (i presume?)
i have an int variable called 'xBtn' that changes 1, 2,..
this is how i get a reference to the button using xBtn
String buttonID = ""+xBtn;
int resID = getResources().getIdentifier(buttonID, "id", "com.thing");
//find the button
ToggleButton tb = (ToggleButton) findViewById(resID);
//change its colour
tb.setBackgroundColor(Color.BLUE);
it crashes on the setBackgroundColor line.
it may be obvious to someone whats wrong and thats what im hoping
any help would be totaly ace ta
thanks
main.xml
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="#+id/my_toggle_container" android:orientation="vertical">
The id of your togglebuttons is gonna be a number from 1 to 32... However, trying to find the toggle button by id will return null because simply instantiating a new toggle button and giving an id wont help you. findViewById looks in the parent view for a child view with the specified id. If you havent added that toggle button with that id to the view, then findViewById will return null. I am 99.99% sure even without looking at the log, that it crashes because you are calling setBackgroundColor on a null object.
In other words, the id that you set a view to is only relevant once the view is actually added to a parent view. In your case you are probably trying to add these toggle buttons to your main content view, in which case you need grab hold of that view that you used for setContentView and call addView on that view and pass in each new toggle button. Note that this will probably not look right unless you also specify layoutparams for the togglebuttons.
EDIT
If thats your entire main.xml, then you've got other issues. Post the full xml file. In any event, you still are going to have to do what I've said, which is to grab hold of the view or a child view of that view and then add the toggle buttons to it via addView (after giving the togglebuttons their proper ids). Once the button has been added, then you can find it. Note though that if you're gonna add the toggle buttons to a child view of your main view, then you'll likely have to grab hold of that child view and call findViewById on THAT.
For example, you can do a nested call like this. findViewById(1) <--- gets you the LinearLayout or whatever inside of your main content view, then once you have that you can call addView on it. So LinearLayout ll = (LinearLayout)findViewById(someNumber); ll.addView(tb);
Try to use the method setTag() , and then you can get all your ToggleButton by using : findViewByTag();
Perhaps tb is null? Could you check that out?
To expand on what LuxuryMode said... What gets an ID INTO your java is inflating it via setContentView and setting it as content. That's why it's ok to have overlapping (duplicate) IDs in different layouts. You can have #+id/submit_button in layout1.xml and in layout2.xml and the Activity will get you the object via findViewById(R.id.submit_button) based on which one you have loaded into setContentView() at any given moment.
So, we're all guessing that you're probably not setting the content view and hoping that the code will find your object in your non inflated XML, which it won't. Which would lead (as everyone has guessed) to you now dealing with a null object, which you obviously can't set a background color on.
I know it gets confusing cause you have the XML RIGHT THERE!!! But the reality is that the xml isn't "alive". It's just stuff for you to look at until you have tasked the Application with inflating it and converting all of it into Android objects of some kind. A lot of the time this is done mostly transparently to you, so, it's easy to forget that none of these things really exist.
It's very likely that tb is null, because findViewById() didn't go as you expected.
You can verify this by surrounding the erroneous line with try.. catch block:
try {
tb.setBackgroundColor(Color.BLUE);
} catch (Exception e){
}
and watch for the message of e. It's likely to be null pointer exception.
In fact, I think you should not use getResources().getIdentifier(buttonID, "id", "com.thing") in the first place. It seems to me that all these resources are continuously numbered in R file, thus you should simply get the first id (as an integer), and then increment on that.
That is, you should do things like:
// The following code is not tested; I just wrote it here on SO.
for (int resID = R.id.button1; resID <= 32; resID++) {
ToggleButton tb = (ToggleButton) findViewById(resID);
tb.setBackgroundColor(Color.BLUE);
}
this should make all 32 buttons blue.

ListView item backgrounds not changing

I have an unusual issue with my ListView. I currently have a "deselectAll()" method which iterates through the items in my ListView and sets them to unchecked (the items implement the Checkable interface). The "checked" variable gets changed correctly (the view reports as not being checked), but the visual indicator (in this case, a background change) does not show the view as unchecked (the background stays the color of a checked item).
I am iterating and deselecting through my listview like so (I also added my declerations):
private ListView vw_entryList;
private void deselectAll() {
for (int i = 0; i < sAdapter.getCount(); i++) {
((Entry)vw_entryList.getItemAtPosition(i)).setChecked(false);
}
}
The code for my implemented setChecked() is as follows:
public void setChecked(boolean checked) {
_checked = checked;
if (checked) {
setBackgroundResource(R.drawable.listview_checked);
}
else {
setBackgroundResource(R.drawable.listview_unchecked);
}
invalidate();
}
It should be noted that when the items are clicked, they are toggled between checked and unchecked in the OnItemClickListener, and this works ok, with the background change and everything. The code for toggling is very similar:
public void toggle() {
_checked = !_checked;
setBackgroundResource(_checked ?
R.drawable.listview_checked : R.drawable.listview_unchecked);
invalidate();
}
The only difference I can see is where the methods are called from. toggle() is called from within the OnItemClickListener.onClick() method, while my deselectAll() is called from within a button's standard OnClickListener, both in the same class. Does anyone have any ideas as to why the background doesn't change when I call my deselectAll() function?
Do you have custom, non-standard color for the background? If so you might take a look at http://www.curious-creature.org/2008/12/22/why-is-my-list-black-an-android-optimization/ - it boils down to setting android:cacheColorHint attribute of your list to the background color. Maybe that will help.
Edited after further discussion:
I think you need to call getAdapter().notifyDataSetChanged() on the List rather than invalidate(). List is really build in the way that it is relying on adapter to provide the data. What you are doing in fact you have an implicit adapter - Entry is really kept in the adapter and by setting checked, you are changing the data model really, but if you do not call notifyDataSetChanged() the list does not really know that the model has changed and will not recreate the views (invalidate() will only redraw the existing ones).
After trying everything (thanks for your help Jarek), I found a solution that works for my purposes. Instead of implicitly calling the setChecked() within the view that was clicked, I leave it up to the setItemChecked() method within the ListView class.
My updated code:
private void deselectAll() {
for (int i = 0; i < sAdapter.getCount(); i++) {
vw_entryList.setItemChecked(i, false);
}
}
My best guess is that the ListView knows that its items implement the Checkable class, and thus requires itself to be the handler of all item operations. Something along those lines. If anyone can explain in more detail why this solution works while the others did not, I'll reward them with the answer and an upvote.

How to correctly use TextSwitcher in ListView?

My TextSwitcher for each record in ListView should display first value (text1) and then another value (text2), then first value again and so on. It should happen only if text2 not empty. Otherwise text1 should be always shown (without any changes and animation).
I've created Runnable(), which changes boolean variable (time2) to then call items.notifyDataSetChanged(). It works as expected and in result setViewValue() for my ListView is called.
Here is the code:
items.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.timetext:
TextSwitcher itemTime = (TextSwitcher) view;
if (itemTime.getChildCount() != 2) {
itemTime.removeAllViews();
itemTime.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
TextView t = new TextView(MyActivity.this);
t.setTextSize(18);
t.setTypeface(null, Typeface.BOLD);
t.setTextColor(Color.WHITE);
return t;
}
});
itemTime.setAnimateFirstView(true);
itemTime.setInAnimation(AnimationUtils.loadAnimation(MyActivity.this,
R.anim.push_up_in));
itemTime.setOutAnimation(AnimationUtils.loadAnimation(MyActivity.this,
R.anim.push_up_out));
}
if (!text2.equals("")) {
if (!time2) {
itemTime.setText(text1);
} else {
itemTime.setText(text2);
}
} else {
itemTime.setCurrentText(text1);
}
return true;
}
return false;
}
} );
It works almost as expected. With one minor item - when text2 should be shown, it changes displayed value to some other value first (from another record!) and then animation is played. Change of text2 to text1 happens correctly.
My understanding that the reason is the following - before displaying text2, all views of itemTime are removed and hence it is recreated and that is why some other value is shown for a second. But why does it show value from some other record?
Actually text2 and text1 are values from the database, for ex.
text2 = cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_TIME_2)), probably, something is wrong here and setViewValue called with wrong parameters?
Upd. text1 and text2 are read from the database at setViewValue. Here is example of the full code:
itemTime.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_CLOSE_TIME_1)) + " - " + cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_OPEN_TIME_1)));
I know this might not answer the question directly, but I'm going to respond to your comment about creating a Runnable() to do the work of switching for you because I suspect that it is probably messing with your data (hard to tell when you cant see the full code).
I advise you to use a ViewFlipper instead of a TextSwitcher. The reason for doing that is that once you added the TextView's inside your ViewFlipper, you can just set your flip interval and then start the flipping and it will do it automatically for you.
As simple as this:
/* Add your items to your ViewFlipper first */
myViewFlipper.setFlipInterval(1000); //time in millseconds
myViewFlipper.startFlipping();
In your current method that you described, when you call items.notifyDataSetChanged() you incur a huge performance hit because all items of your database are going to be re-read and your list will be "re-drawn" again. You should only do that if your actual data really changed rather than using it to switch between text that you already have and doesn't change from creation time.
As a nice surprise, you might notice that your problem goes away because you don't have to re-read everything from you DB again and reduces the chances of mix-up of item1 and item2 since you will only need to read them once when the row is created in your ListView
Just my 2 cents.
Let me know how it goes.
I think I see what's going on here, and it's because of the way ListView works.
ListView recycles all of its views internally so that you only have as many views created as can be displayed on the screen. However, this also means that when you bind values to a view in your setViewValue method, you are not always given the view that was in the same position in the list before.
Say you have three list items: itemA, itemB, itemC in that order. Each contains text1, text2, and text3 respectively at first.
When you call items.notifyDataSetChanged(), ListView recycles all those list items however it feels like, so you may get a new order of itemC, itemA, itemB; and the text would then read text3, text1, text2.
As a result, when you change the text of the first list item to "text2", you will in fact see "text3" change to "text2" instead of a transition from "text1" to "text2" like you are expecting.
Are text1 and text2 stored in the resources file (res/values/strings.xml)? If so, Android will sometimes confuse variables. Simply running Project > Clean on this project may fix the problem.
This worked for me :
myViewFlipper.setFlipInterval(1000);
myViewFlipper.startFlipping();

Categories

Resources