Android: keep focus when other view gets it - android

I have 4 buttons for switching displayed content. Any time one of them is pressed, it's highlighted:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bottom_selected" android:state_pressed="true"></item>
<item android:drawable="#drawable/bottom_selected" android:state_pressed="false" android:state_focused="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
My problem is that I need last pressed button to keep its focus even if some other view is focused (f.e., I have EditText view, and when it's pressed, all buttons lose their focuses and none of them is highlighted). Is there any easy way to do that? Remembering last pressed button and rehighlighting it when other view is pressed doesn't sound like good solution.
UPD: It can be done by adding lastPressedButton.requestFocus() to other view's OnFocusChangeListener. Not too bright if there are many other views, maybe some other solution?

Try to add android:focusable="false" attribute to all the other fields that you don't want to be focusable.

Related

Disable a button without using setEnabled property

I have a button where the selector is like
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/bg_circle_selected"/>
<item android:drawable="#drawable/bg_circle_disabled" />
</selector>
So when I click the button, the background will show a red colored circle.
I need to disable this button based on condition, so the highlight should not be shown.
If I so it as setEnabled false it will work
But there is one more case where the disabled button should give auditory feedback.
So when I give setEnabled as false the other requirements will not work because touch is disabled.
Is there any method to disable the button other than setEnabled ()?
You can achieve the above by custom selector where the button appears to be disabled but the fact it is still enabled and trigger click actions.
you can use
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/bg_circle_disabled />
<item android:state_pressed="true" android:drawable="#drawable/bg_circle_disabled"/>
<item android:state_enabled="false" android:drawable="#drawable/bg_circle_disabled" />
also don't forget to keep track to the button status (enable/disabled)
You might want to consider setting the attributes of the button programmatically from the java file, not XML.
Give the button an id from your XML layout file, then reference it from the java file. In that way, you will have more control over how it behaves.
For example, android:id="#+id/my_button" in the XML.
Then Button button = findViewById(R.id.my_button); in the onCreate method.
Afterward, give it whatever attributes you want.

Text moving upwards in Android Studio

How to move textView and CardViews upwards when it gets focused?
I have designed cards using CardView and have added some details inside a cardView like name, date. If I focus on those cards , I need to move it upwards. When it moves upwards, the cardView colors should be changed. So that user will be knowing those cards have been focused!
The same thing needed in a textview too.
How can I do this in Android Studio?
The text and cardViews (ie. Kindly see hack your software career from the below link)is moving upwards!
Link to the documentation (see states): http://developer.android.com/guide/topics/ui/controls/button.html
Basically, you define your background as a drawable xml,there you can define different styles of background for each state (pressed, focused, default):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused"
android:state_focused="true" />
<item android:drawable="#drawable/button_default" />
</selector>
So you just need to make the background you want http://developer.android.com/guide/topics/resources/drawable-resource.html

How to enable the "pressed" look on LinearLayout

I have the following UI presented when my ListView is empty:
Now, I want that when the user will press this "New Reminder" layout, it'll change to a "highlighted" state (with the blue focus background in ICS and the yellow color in GB)
The layout is clickable and the onClick method is called, but there is no indication for the user while he press his finger down.
I tried setting focusable to true, but it didn't do the trick.
What can I do to give any view the default "pressed" effect?
Thank you!
With your layout, you can set background with drawable like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:drawable/star_big_on" />
<item android:state_pressed="true" android:drawable="#android:drawable/star_big_on" />
<item android:drawable="#android:drawable/star_big_off" />
</selector>
Draw-able like below
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/left_radio_selected"/>
<item android:state_enabled="false" android:drawable="#drawable/left_radio_inactive"/>
<item android:state_enabled="true" android:state_selected="true" android:drawable="#drawable/left_radio_selected"/>
<item android:drawable="#drawable/left_radio_active"/>
and set your linerlayout.setSelected(true);
I don't know if I understand you correctly. If you want to create pressed state for this layout, you have to prepare state drawable xml (more info). Up there you can set what should your layout looks like while in pressed state.
I'm not sure but your question seems a little vague. From my understanding what you want might be suggested in this post Force a ListView item to stay "pressed" after being clicked?

How to shift text on a button which is currently pressed?

I want to shift a little bit the text on the button that is currently pressed. In my case the button has custom background and the button will look much better if the text will be shifted.
I made two NinePath images with different paddings:
And set them using a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn"/>
</selector>
This doesn't work, the text remains on its position when the button is pressed (and the background is swapped).
Are my NinePath images OK? Is there any way to shift the text without setting touch listeners?
P.S. Same question was asked already but the "answer" is exactly the solution I tried: https://stackoverflow.com/a/7677098/694770
Your 9-patch images look good. That said, I came across this question because I had exactly the same problem, and my images didn't work either.
I ended up subclassing Button and messing with padding. More info here
Try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:text="Pressed state text" android:state_pressed="true"/>
<item android:text="Default text"/>
</selector>

Button draws normal state after pressing

I'm new to Android and just starting the very basics. I implement my custom button skin using .9.png images for norma/focus/pressed states. It works fine, but I noticed that after a pressed the focussed button it visually "lost" focus and draws the normal state frame. I planned to use different state images to highloght what button is selected right now, but it seems that it would not work. I noticed also that the same happens with the default LAF button. Is it OK, or it's just emulator issue? What the good workaroud can be used?
Thanks
I think the following may help. I wanted to have one of the buttons in a list of button to be coloured differently, to highlight the fact you were already in that section.
My buttons android:background field was set to the following drawable (drawable/my_btn.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="true" android:drawable="#drawable/btn_focused" />
<item android:state_selected="true" android:drawable="#drawable/btn_selected" />
<item android:drawable="#color/transparent" />
</selector>
You'll noticed i've got an item with the android:state_selected="true" attribute set.
Then in code you can have
Button mybtn = (Button)findViewById(R.id.my_btn_1);
mybtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Button btn = (Button)findViewById(R.id.nav_secondary_1);
btn.setSelected(true);
}
});
I'm not sure if you can set the selected stat of a Button through a property in the xml. Not sure you would want to.
The order of the item's are also important as it can change the visibility of the other states. The current order will allow you to see the pressed and focused states. however, if you moved the selected item to the top you would find that your pressed and focused states would not be displayed.
I am not sure if you can combine the pressed, focused and selected states to allow for more customised graphics. I haven't tried it but the following would allow for more complicated state based graphical layouts.
<item android:state_selected="true" android:state_focused="true" android:drawable="#drawable/btn_selected_focused" />
Read up on Selectors here http://developer.android.com/guide/topics/resources/drawable-resource.html
This is the default behavior in touch mode, and you should not seek to tamper with it. This is how your users will expect for your app to behave. If you set the focus without touching the screen, such as when using the trackball that's available on most devices, it will indeed remain in focus, but in touch mode there's no visual representation for the state of having focus.

Categories

Resources