Expand/open a spinner on click of another item/widget? - android

I am trying to expand a Spinner when user click on another Button. as example : I have a Spinner with values and a 'OK' button when user click on 'ok' buttton without selecting any value from spinner, Spinner expands itself.
Is this possible to get a event to expand spinner without user interaction on spinner.

Just call Spinner.performClick() to expand Spinner without user interaction...
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
Button okButton = (Button) findViewById(R.id.yesButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(spinner.getSelectedItem() == null) { // user selected nothing...
spinner.performClick();
}
}
});

Put in your view.onClick
YourSpinner.PerformClick();

Related

How to add OnClickListener for disabled spinner in android?

I have a spinner in android and disabled it using,
Spinner SPMenuItem = (Spinner) findViewById(R.id.SPMenuItem);
SPMenuItem.setAlpha(0.7f);
SPMenuItem.setEnabled(false);
I want to display the following toast message on click of this disabled spinner.
SPMenuItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"This is a readonly field",Toast.LENGTH_SHORT).show();
}
});
But I get the following exception on load,
I just want to display toast message to indicate the spinner is disabled. How can I achieve this ?

changing the button text from the edittext state

I have one button and 1 edittext. The button is set to "change", and EditText is enable-false. When you click on a button, its text is changed to "save", and edit text is available for input. After a second press, the text changes to "change" again, and the button becomes Enable-false.
how to implement it?
Use this onClickListener on your button -
Button yourButton = (Button) view.findViewById(R.id.button)
EditText yourEditText = (EditText) view.findViewById(R.id.edittext)
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!yourEdittext.isEnabled()){
yourButton.setText("Save");
yourEditText.setEnabled(true);
counter++;
}else if (yourEdittext.isEnabled()){
yourButton.setText("Change again");
yourButton.setEnabled(false);
}
}
});
Make sure to set the initial button's text and the edit text disabled for the starting point

Enabling/Disabling all buttons in a Grid Layout

I am trying to create a word game, where all the letters in a word will be added to a grid layout and presented to the user. The user then has to form the word by clicking the letter buttons. The buttons will be disabled once pressed. If the user decides to reset the game, the same grid layout has to be presented again to the user with all the buttons enabled. Is there any function that can be used to set all the buttons as Clickable? Below is the code, I used.
for(final String letter : shuffled_word){
final Button button = new Button(this);
button.setText(letter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setClickable(false);
}
});
this.game_grid.addView(button);
}
int id = getResources().getIdentifier("button"+i, "id", getActivity().getPackageName());
Button b=(Button) view.findViewById(id);
and then b.setClickable(falsE)

Opening a spinner dropdown menu from an ImageButton on Android

My layout contains a spinner and an ImageButton.
The spinner is setup properly. Pressing on it displays the drop down menu.
Now I'm trying to add an onClick action on my button so it can open the Spinner
The following code:
this.imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
works fine on the genymotion emulator, but not on my Nexus 4 & 6 devices.
On those devices the dropdown menu opens then closes automatically. Every 10-20 attempts it might remain open but that's it.
Any idea what's going on ?
How can I prevent this ?
Edit if I enable android:spinnerMode="dialog" instead of thde default 'dropdown' it works fine, as if something was taking the focus from the spinner... What's strange is that opening the dropdown menu by pressing on the spinner works fine
Here is Source Hope so It works,
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
Button okButton = (Button) findViewById(R.id.yesButton);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(spinner.getSelectedItem() == null) { // user selected nothing...
spinner.performClick();
}
}
});

How to create a custom spinner?

I want to make a spinner that does not show the item i choose.For e.g i have a spinner with 3 rows 1 2 & 3. When i choose 2 the spinner should close and i see 2 on the spinner.I just want to make the spinner behave like a button that just open's spinner . any idea?
A spinner is just like any other view. If you want it to behave like a Button and start an action when you select an item, you can set a click listener to each of the views inside the spinner.
for (int i =0; i<spinAdapter.getCount(); i++){
spinAdapter.getView(i, null, null).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do whatever you want here
}
});
}
I have not tried this code, but you could give it a go.

Categories

Resources