Android/Kotlin Material Button Group default selection - android

I have a Material Button Toggle Group in a Fragment layout (with single selection and selection required), but on load of my app I want one of the button to already be selected. I thought adding this to my Main Activity onCreate() would have done the trick (the id of my button is paletteA):
paletteA.isPressed = true
but instead my app just crashes on loadtime and no errors are displayed in the Run window. Feels like something is not loading in time or is null, even though all of my Fragments have already been loaded in, etc, and this is the very last thing in my onCreate(). Any ideas?

The MaterialButtonToggleGroup widget has an XML attribute named app:checkedButton that can be used to load a button in the group as checked.
To set a particular button inside the MaterialButtonToggleGroup to load as checked by default, pass the id of the button to be checked as the value to the app:checkedButton attribute.
<com.google.android.material.button.MaterialButtonToggleGroup
...
app:singleSelection="true"
android:id="#+id/activity_main_togglebutton"
app:checkedButton="#id/activity_main_button2">

Related

Android multi state toggleButton

I using this library to make multi state toggleButton
https://github.com/jlhonora/multistatetogglebutton
Everything works fine now.
My issue is how can make the toggle button clicked based on a value given?
Example there are three values in toggle, A,B,and C, which is in Activity B.
I'm passing value A from Activity One to Activity Two. When it comes to Activity Two, the toggle which has value A should be clicked.
It looks like you set the chosen button by it's position. You could retrieve all the texts of the buttons, find the index of the text that matches your text, and then set the chosen button with that index.
Pseudo Code:
val dataFromActivityOne = intent.getString("blah")
multiStateToggle.setValue(multiStateToggle.texts.indexOf(dataFromActivityOne))

Radiobutton drawable ends up in incorrect state when in ViewPager

When my RadioButton is on the second page of my ViewPager (which is offscreen), it ends up looking like this when programmatically checked (in onViewCreated, onStart, and/or onResume)
Ideally, the radiobutton should look like this
How can I force the drawable to be in the correct state? I have tried invalidating the radio buttons but have had no luck. Clicking another radio button and re-clicking the correct one appears to fix the issue
After more testing, the issue appears to only be on API 25 and is caused by the animation that occurs when you click a RadioButton. I have fixed it by adding:
AnimatedStateListDrawable animatedStateListDrawable = (AnimatedStateListDrawable) radioButton.getButtonDrawable();
animatedStateListDrawable.jumpToCurrentState();
Here instead of radio button you can use simply imageview and replace the image onclick of imageview and set the one of two images(one selected and other one deselected) accordingly. that is how you can maintain the state and its just look like radio button.

Add views dynamically and get value from those views

I need help regarding adding new set of views and get value from those views.
In my example, I want to add 4 TextViews on Button click and open TimePicker from each view click and display new selected time on respective TextView.
Below is screenshot of view.
If these 4 views are fixed you could just create the xml and add them all to a single holder that you set invisible. If you mean by dynamic that it could be either 4 or 99 views, I'd recommend a RecyclerView. Plenty of examples on the internet. If you create a recyclerview with a custom adapter it is very easy to get the respective data per view.
For the future, please add more context to your question like what you've tried, what the result was and why this isn't your expected result. This is a very broad question.
Assuming that on clicking the clock button under the time section (on the right) you would want to set the selected time into the text field. You can simply call view.getParent() on click of the button and from the parent you can get the first child i.e child at 0 and set the text into the text field. This will work provided the button and textfield are within the same layout.

ListView row with EditText, clickability, and context menu -- how to put it together?

I have a ListActivity-based activity that uses a context menu for the items. After adding an EditText to the row of a ListView, the context menu stopped working, and also the item does not react on a click. It seems that it is blocked somehow by the focus of the EditText. I can enter the EditText value, but I cannot get the earlier context menu, and I cannot start another activity via clicking the item.
I have possibly found the related comment that says:
Android doesn't allow to select list items that have focusable elements (buttons). Modify the button's xml attribute to:
android:focusable="false"
It should still be clickable, just won't gain focus...
... so I did the same for the EditText (I am not sure if the button case can be generalized for the EditText). Anyway, the item is clickable again, the context menu appears... However, the EditText part of the text stopped working now. (Actually, I did not implement the reaction to the EditText -- the keyboard simply does not appear.)
Is it possible to have the clickability of the list item and also make the EditText work the expected way?
I don't know if this would help you but it is a post I've found when I was trying to use a ListView with buttons inside.
ListView Tips & Tricks #4: Add Several Clickable Areas
Hope this helps.

Android Spinner does not close after using setSelection()

I have a scenario where I want to programmatically change the value of my spinner using setSelection(). This works great, except that if the spinner is open (expanded?) an the time, the spinner does not close. Eg, the menu stays up, despite calling setSelection().
How can I programmatically close the spinner? I have tried performClick() to no avail.
edit: more details:
The reason I'm trying to do this is that my spinner actually uses a compound layout for each selection row. Namely, I have a linearlayout which contains an image, text, and button. The idea was that the button serves as an "edit" button (which opens an activity), while pressing on the image/text select the row (per usual).
The problem came when I added the button. Suddenly, the image & text no longer captured the press event to change the combo. In other words, adding a button to the row destroyed the touch-handling capacity of the entire row. So I tried to manually implement a click handler for the image/text, which subsequently executed a setSelection... which is where I ran into this problem.
You say that after adding the button you lost the click handle on the entire row. Try adding this android:descendantFocusability="blocksDescendants" to your row layout, and see if you can get the clicks to work properly.

Categories

Resources