I have a RelativeLayout. In that I create several ToggleButton views. The user can set those ToggleButtons on and off.
When the user opens the Activity's OptionMenu I want all those ToggleButtons to become OFF. To do this I am setting programmatically the ToggleButtons to OFF in the onPrepareOptionsMenu code.
I have also a PopupMenu registered to a Imagebutton. I want also when the users opens the PopupMenu by clicking the Imagebutton all the ToggleButtons to become OFF. So, I am turning the togglebuttons to off in the Imagebutton's setOnClickListener code.
My issue is that the updates to the Togglebuttons' state (to Off) are shown only after the OptionsMenu or the PopupMenu is closed. Instead I want all the ToggleButtons become Off as soon the user opens the menus. I thought I have to use some OnFocusChangeListener on some view. I tried to use it on the Activity's top layout but it doesn't work.
How could I get the result I want?
I found the solution myself by overriding the onWindowFocusChanged method, like this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
//code to turn off togglebuttons goes here
}
}
Related
I have a Spinner in my action bar which gets initialized only once in onCreateActionMode();
Issue: When the spinner drop down is visible and I try to rotate the screen, the drop down state is not maintained after orientation change.
How could I maintain the state so that if in Portrait mode the Spinner dropdown is visible then after orientation change in Landscape mode too the dropdown should be visible.
Also let me know the API's by which we check whether the drop down is visible or not at a given point. Also an API to show the drop down forcefully.
I have also specified configChanges="orientation" in my Manifest file.
I found a very ugly solution to this (the 'action' takes place in the onConfigurationChanged method):
Before calling setContentView, check if the dropdown view is shown (*) and if so, save the position that is currently selected in the spinner (int pos = spinner.getSelectedItemPosition()).
After calling setContentView and setting the spinner adapter, if the dropdown view was shown in step 1, force the dropdown view to show by calling performClick on the spinner:
spinner.setSelection(pos);// this way we make sure that the same item
// remains selected after rotating the device
spinner.performClick(); //show the dropdown view
(*) Checking if the dropdown view is shown is the trickier part. I haven't found (yet) a method that lets me know whether the dropdown view is shown, so I had to do the following:
Hold the spinner's pressed state in a boolean variable (named, for example, isClicked).
Set an onTouchListener for the spinner and in the onTouch method set isClicked to true (when tapping the spinner, the dropdwon view opens, so isClicked == true means that the dropdown view is shown).
Override onKeyDown or onKeyUp and when back button is pressed, if isClicked is true, set it to false (I assumed that pressing back with isClicked==true means closing the dropdown view).
Use the value of isClicked in onConfigurationChanged method to check if the dropdown view is shown.
Like I said, it's an ugly fix, but it's the only one I could come up with until now. If anybody has other ideas, please post them.
Humbly taken from here
There are other solutions too which are as follows
SO answer 1
SO answer 2
Bottom Line is you need to capture the status(some boolean variable) when the spinner is opened and when on activity configChange need to intialize the spinner state from the status
Hope it helps!!
I want to implement a button that keeps its state after I clicked it and until I click another part of the screen. I don't know how to implement this. I've seen the methods setSelected and setPressed. Do you know if I have to use any of them? Thanks!
You can use ToggleButton with style of regular Button and state checked same as click. Then you can control checked state with setChecked(boolean checked) method.
I am trying to cope with one (seemed to be) smalll thing. In my app I've got one activity with two EditText fields.
I want one of them to be normall field (etNormal), and the other (etButton) behave more like button, so when you touch it, the keybord is not shown but instead the sliding drawer is opened. If sliding drawer is opened and you will press the normall edittext sliding drawer will hide.
I've tried adding OnClickListener and OnTouchListener (not in same tries) to both with condition if etButton was clicked/touched open sliding drawer, if not then close.
The outcome was strange. When it was OnTouchListener test it was more like toggle, so when I pressed one drawer opens and on another close. When it came to OnClickListener I needed to press each edtitext twice to get action done.
And to hide keybord in etButton I am using setInputType(InputType.TYPE_NULL);. I've tried also setEnabled(false); but then I was even unable to click/touch it. The one defect of currently used method is when I am changing click from etNormal to etButton, the keyboard is still shown and it doesn't hide.
So, can anyone tell me what I can do to achive my goal?
EDIT:
I've erad your current suggestions and modified a little my code, but still it is not working.
This is a part of it where I am assigning OnTouchListener:
OnTouchListener touchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent ev) {
if(v==etButton && ev.getAction()==MotionEvent.ACTION_DOWN) {
slidingDrawer.animateOpen();
}else {
slidingDrawer.animateClose();
}
return false;
}
};
etNormal1.setOnTouchListener(touchListener);
etNormal2.setOnTouchListener(touchListener);
etButton.setOnTouchListener(touchListener);
Also in etButton declaration in XML layout file I have:
android:focusable="false"
But now, on etButton touch nothing hapens (only sliding drawer hides if was opened), and when etNormal1 or 2 is touched sliding drawer shows up or hides depending what was first (in other words toggel).
Any idea, what is wrong here?
Got an editText working like that with
android:focusable="false"
android:clickable="true"
Then an OnClickListener to override the action
In addition to above answers, I hide cursor using cursorVisibility!
android:focusable="false"
android:clickable="true"
android:cursorVisible="false"
android:focusableInTouchMode="false"
cursorVisible To show/hide the cursor on clicking the EditText
focusable To gain focus when user is touching the view, like EditText
focusableInTouchMode To keep a view selected. (select and click are different)
For detailed understanding, refer here
In your layout, add the following attribute to the EditText
android:focusable="false"
android:focusableInTouchMode="false"
Next, write method to handle the click on the EditText and add your application logic .
If you are using onTouch event, when you click the edittext, you will get two events with action as MotionEvent.Action_down and action Up. so basically it will give the effect of clicking the edit text twice. can you please provide the code so that we can have a deep look.
ReWrite your code as :
OnTouchListener touchListener = new OnTouchListener() {
boolean isOpen=false;
#Override
public boolean onTouch(View v, MotionEvent ev) {
if(v==etButton && ev.getAction()==MotionEvent.ACTION_UP) {
if(!isOpen){
slidingDrawer.animateOpen();
}else{
slidingDrawer.animateClose();
}
isOpen=!isOpen;
}
return false;
}
};
If etButton needs to be an EditText (why not a button, if it should behave like one?), maybe you could set an onFocusChangeListener. Once it gets the focus, you can show the drawer...?
Not sure about not showing the keyboard...
EditTexts are tricky. The reason why you have to press twice when you have use an OnClickListener is that the first time around the EditText gets focus and this consumes the touch event, in that case the OnFocusListener is triggered. When you touch the second time, the EditText already has Focus so now a click event is triggered.
I would suggest you try to do this without EditTexts. That would in any case yield a cleaner and simpler solution. Why exactly do you want to use EditTexts instead of Buttons?
I have ListView with extendable items. Everything is working great until I trigger the dialog popup with EditText. Right after it animation just stop working. If I tap other button to animate the ListView container, everything coming back to normal.
In my ListViewAdapter I got item view in extends FrameLayout class and Animation is trigger when is called override onMeasure method with is not trigger after Soft Keyboard appear.
I think this is causing because there is no focus on particular item on the list?
Off course regular click on the list item is working fine.
Strange is also this on that when I hide soft keyboard by pressing back button everything also working as it should be.
Thank you for any suggestions.
When you trigger the dialog popup with EditText, save the focus of the list view:
int focusedIndex = lstView.SelectedItems[0].Index;
You can then restore it by using:
lstView.Items[focusedIndex].Selected = true;
Dialogs are modal, which means that they take away all the focus and get the user to only focus on their own UI. You need to not use a dialog, instead you need to use a Fragment or a Activity with wrap_content and a RelativeLayout to keep it small and in the center.
I have a layout that has two children in it. My goal is to have the whole layout function as a button. Each of the children has a different color to show pressed state. When I press them individually they show their pressed state fine. However when one is pressed, I would like them both to show pressed state.
My first thought was to use duplicateParentState, but the parent never seems to be in the pressed state because the Views fill the layout. My next thought was to use AddStatesFromChildren, but all this did was make the parent show pressed, but not the children. Finally I tried using both, but that caused an error.
How can I get sibling Views to share a pressed state?
I'm only 4 years late to this, but you were close:
set them both inside a parent layout
set your click listener on the parent
then set android:duplicateParentState="true" on each of the children.
Works like a charm!
You could possibly do something like:
Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button otherButton = (Button) findViewById(R.id.other_button);
otherButton.setPressed(true);
}
})
But, of course, have the buttons called as you need to.