I'm wondering if it's possible to build a custom button layout with different appearance depending on state so it acts like a button. For example, a LinearLayout with an ImageView and TextView, with these states.
When the button is in normal state, display image_normal.png and text in red.
When the button is pressed, display image_pressed.png, background red and text in white.
When the button is disabled, display image_disabled.png and text in grey.
Thank you!
Any view can have any background and any view can be made clickable, so there is nothing preventing you from having a LinearLayout with the background you described. It's worth noting that you don't need a LinearLayout to have an image next to text, you can use the fact that TextView supports drawables on any side of the text using the drawable[Left|Top|Right|Bottom] attributes.
If your question is about the syntax of a selector drawable, I would refer you to the documentation. Note that Android evaluates states top to bottom, so choose the order wisely. The last item should be the "default" state when none of the ones above apply. For the example you gave, you would probably have something like
<selector>
<item android:state_pressed="true" android:drawable="..." />
<item android:state_enabled="true" android:drawable="..." />
<item android:drawable="..." /> <!-- disabled -->
</selector>
Related
I m beginner of android . .
I have an application in which there are various activities on which different text is appearing. In my application i need all the text in this app. I got 60% of the text in the app that is set using setText() method . But still some text is visible but i dont know how they have displayed this text.
SO what i want is that, the methods or any other ways in android that enables us to display the text in our activity.
For more clarification i show u an image of app:
I need to know how these texts are been set . .
It will be helpful if list out methods which are used to set text .
you can display text in a textview, via java or xml, or you can use canvas.drawText().
these are the simple two... there is also the Toast for quick pop-up text, and dialogs which are toasts with buttons... there are several ways you can display text. the image above, is an image of text set to the background of a tab, or the src/background of an imageview...
Mainly you have to ways of showing text:
Using an element that allows showing text: Button, TextView
for these guys, you'll normally use the .setText setter that you probably already know.
You can show an image or use an image as background for a element: ImageView, ImageButton
for setting an image to an object, you'll normally use
public void setImageResource (int resId)
public void setImageBitmap (Bitmap bm)
public void setImageDrawable (Drawable drawable)
The buttons above, mostly probably are set with an image.
For showing different images for different states (on tap, on select, etc) you'll have to define properties like the ones in the example below for the ImageButton:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
More information at:
http://developer.android.com/reference/android/widget/ImageView.html
http://developer.android.com/reference/android/widget/ImageButton.html
http://developer.android.com/reference/android/widget/TextView.html
I have ImageView with some icon. Icon, for example, have size: 32 x 32 dip. ImageView have background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/simple_button_focused_holo" />
<item android:state_pressed="true"
android:drawable="#drawable/simple_button_pressed_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
Then user click on icon, we can see some hightlight on click. All ok: work on 4 and 2 android version. But size 32 is so little for clicking. Therefore, I add hidden view and add onClick for this hidden view. This view have ~ 50dip and user can easy click on icon. But in this case, user don't see highlight on click. I cann't increase size source icon, because parent view have fix size and near icon also exists other views: textviews, progressbar (which not need response on click).
Can you change the background color of the view when it receives a click? Use some transparent color so the user can still see the Button. Then you can setup a Thread to sleep for some certain amount of time, and then use runOnUiThread() and pass a Runnable that will set the background back to transparent?
I have a lot of custom views and I have style for state_pressed. Basically its a rectangle with
solid android:color="#DC2D5A8C"
What I am trying to do is simulate the blue background color that comes with the standard views/controls. For example: when you click on a button or list view item, the background changes to blue (on_pressed).
I got that to work with the above style, but the problem is let's call it the tint effect. In a button, the text caption is black. When you press, the background is blue and the text color changes to white.
Now how can I achieve such a so called "tint" change in my custom control's view?
Your response is much appreciated.
Thanks!
You can use selector xml file to do this.You must be setting background to the button.Instead of that set the xml file to its background.Create selector.xml file in your drawable folder as shown below and set that xml file as background to that button just like : android:background="#drawable/selector"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use blue -->
<item android:drawable="#drawable/btn_blue"
android:state_pressed="true" />
<!-- When not selected, use black-->
<item android:drawable="#drawable/btn_black"/>
</selector>
By doing this you will get so called tint effect to your button.Hope this will help you.
I am currently using an ImageButton, and I want to have the effect like radio button once you select it stays selected, until someone picks another image button. I have setup custom selector like below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonimagesel" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/buttonimagesel" /> <!-- focused -->
<item android:drawable="#drawable/buttonimage" /> <!-- default -->
</selector>
But this just shows the selected image for as long as the key is pressed down. The effect i want is for it to stay selected like a radio button until the request is processed after which the whole activity including the button is redrawn. So I want one click to put the button in a selected state and unclick does not change this. Also I do not want the other buttons to be selectable after this happens, and I don't certainly don't want them to change images or anything like that.
Thanks
If you need to use an ImageButton, you can add an android:state_selected="true" item and use setSelected() in your onClick() logic. You would have to take care of deselecting all the other buttons when selecting a new one. This question might be useful: Android ImageButton with a selected state?
However you could also just use RadioButtons and customize their look (with android:background and android:button - these and all CompoundButtons have a checked state that work in a toggling way).
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.