Android multi state toggleButton - android

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))

Related

Android/Kotlin Material Button Group default selection

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">

Display a specific spinner depending on radio button selection

I am working on an Android application where I want a user to select a RadioButton corresponding to one category, and add a spinner within the same layout, where the displayed spinner depends on the radio button that was selected. This means for each RadioButton I have a different spinner. I know how to add view to the layout but I was wondering how I might go about storing each of the individual spinners and requesting them when the corresponding category is selected. Is it possible to store them within a layout activity and select the correct one using findViewById?
Agree to Trushit
add all the spinners in the view & on performing the Radio Button event set the visibility of the spinners.
yourSpinner.setVisibility(View.VISIBLE);
&
yourSpinner.setVisibility(View.GONE);

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.

Passing text value from one Button to another Button

My application is having two activities. In first activity, I have one button and in second activity I have 3 buttons and wheel view.
If I click the button from first activity it moves to second activity and if it selects any value from wheel view,that current value should be displayed in first button.
Using stored Preferences, it works fine, then if again when I select any value from wheel view that first selected value is displayed in second button and current selected item should be displayed in first button.
Can anyone help?
here,
i am using getter and setter for store the first selected value in setter and get the value into second button, here my code
setSecondBrandValue(btnCurrentSelection.getText().toString());
PreferenceConnector.writeString(WheelActivity.this, itemKeyName,
wheelMenu1[getWheel(R.id.p1).getCurrentItem()]);
and here i set the value to second button
btnSecondSelection.setText(getSecondBrandValue());

How to traverse a list with up and down buttons?

I have implemented one "up" button and one "down" button in my layout.xml which I want to use in order to navigate through a ListView of strings. How can I accomplish this?
I do not want to use the "scroll" property of the ListView nor to use a LinearLayout with vertical orientation within a Scrollview in order to traverse the list. I simply want to move up and down my ListView by pressing the corresponding buttons.
Any help is appreciated!
You could have a variable int i = 0; in your class body.
Then when the activity loads, the item at location 0 would be selected. If your user presses the down button, then do:
i++;
setSelection(i);
And similarly for going back up:
i--;
setSelection(i);
Of course you'd have to add logic to the code for when you're already at selection 0 or at the maximum selection, but that's the gist of how I'd do it.
To note, I've never used ListView as I've not needed to, but the above should work. I got all my information from The Android Documentation

Categories

Resources