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();
}
}
});
Related
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 ?
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();
i'm using setColorFilter to color some button... the code is this:
final Button falso = (Button) findViewById(R.id.falso);
final Button vero = (Button) findViewById(R.id.vero);
vero.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
vero.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x00FF0FF));
falso.getBackground().clearColorFilter();
esame.set("V");
}
});
falso.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
falso.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x00FF0FF));
vero.getBackground().clearColorFilter();
esame.set("F");
}
});
when i click the button "vero" i want reset the colour of "falso" and viceversa.
i tried this code on android ics and all work good, but when i tried it on android 2.3 i have a bad surprise.
when i click the button the colour don't reset and i don't understand why.
i find the solution:
use button.invalidate();
after i clear background
Setting the ColorFilter to 0 will do thee job by clearing the filter.
vero.setInt(vero.getBackground(), "setColorFilter", 0);
so I tried android:selectAllOnFocus and of course I'm using android:hint.
The app loads, requestFocus triggers and the full text is selected.
The problem is that when I click in the EditText the selection is lost.
I've already read:
Select all text inside EditText when it gets focus
For some reason, it worked (on Jelly Bean) only if I posted it with a Handler:
new Handler().post(new Runnable() {
#Override
public void run() {
paidView.selectAll();
}
});
Set the selection in an View.OnClickListener like so:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(0, editText.getText().length() - 1);
}
}
Set the selection in an View.OnClickListener like so: (with the 0 and text length opposite to the previously approved response) - this will ensure that when the user starts typing the content will be overridden.
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setSelection(editText.getText().length() - 1, 0);
}
}
I realize this is an old post, but I had this same problem. For me, the reasoning was that I like to move the keyboard out of the way (dismiss it) to get my bearings (or hit buttons to add or remove rows of data) and when i touched back on the EditText I was previously editing, it was annoying to have the keyboard pop back up and the text un-select, forcing me to either work to get the cursor to where I wanted to start deleting, or touch another EditText and then touch back on the original to re-select everything. I just want to have the keyboard pop back up and have the text selected and ready to overwrite whenever I go to that EditText.
There are easy solutions to this:
1) Long tap does this for you on most occasions.
2) If you're already using setSelectAllOnFocus(true), you can just throw a simple clearFocus() and requestFocus() in your onClick listener:
etMyEditText.setSelectAllOnFocus(true);
etMyEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.clearFocus();
view.requestFocus();
}
});
This way the EditText has everything selected when you tap on it, regardless of soft keyboard status.
Additional bonus:
Add android:windowSoftInputMode="adjustPan" inside your <activity .../> tag in your AndroidManifest.xml file to force the selected EditText to stay in sight when the soft keyboard pops up.
try This
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.selectAll();
//or this.selectAll();
}
To select all the text.
Do the code below after you did findViewById and you are good to go:
edtProductPrice.setSelectAllOnFocus(true);
edtProductPrice.requestFocus();
edtProductPrice.selectAll();
when I make a custom view class and add a clickListener it fires anywhere on the screen I click, even where the custom view is not. If I use the same code with a button from a layout it only fires when I click the button not anywhere on screen. Any ideas how to just only listen for when my custom class is directly clicked?
button only fire when pressed
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(DEBUG_TAG, "button clicked");
}
});
stroke object fires when you press anywhere on screen, even outside of stroke's bounding box
Stroke stroke = new Stroke(this);
mainLayout.addView(stroke);
stroke.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// fires on every screen click :>(
Log.d(Main.DEBUG_TAG, this.toString()+"shape clicked");
}
});
I think your custom view just fills the whole screen. That's why it reacts on every click on the screen. You need to make it smaller and everything will work fine.