0

i want to do for user interface which will look like tiles in windows 8. I have something like this:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" 
            android:background="@drawable/exit"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

and the second button when is pressed is change a little bit. It is a little bit darker. How do the same for my button where is image background?

2 Answers 2

1

You need to set different drawables for each pressed state.

For example, if the normal state image is normal.png and pressed is pressed.png, disabled is disabled.png, focused is focused.png, disabled and focused is disabled_focused.png (doesn't have to be an image - it can be a shape, or whatever):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:state_window_focused="false"
        android:state_enabled="true"
        android:drawable="@drawable/normal" />

    <item
        android:state_window_focused="false"
        android:state_enabled="false"
        android:drawable="@drawable/disabled" />

    <item
        android:state_pressed="true"
        android:drawable="@drawable/pressed" />

    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/focused" />

    <item
        android:state_enabled="true"
        android:drawable="@drawable/normal" />

    <item
        android:state_focused="true"
        android:drawable="@drawable/disabled_focused" />

    <item
         android:drawable="@drawable/disabled" />

</selector>

and if the file name the selector is in is called tile.xml, you can now use android:background="@drawable/tile".

Sign up to request clarification or add additional context in comments.

2 Comments

It's ok and helpful but in this situation i have to create another images, yes? It isn't possible to do this without creating new images? Have android not in itself something to do that? I just want to make a picture in August slightly changed, for example, was darkened.
Of course… Even the dark effect of the default button is another image.
0

do you want to change the button background when it is clicked???

if that is your intention (im sorry it was a bit unclear from the question) you can add a selector as the button background and customise the button as you wish.

add a new xml file custom_button.xml to your drawable and set it as the background of the button : android:background="@drawable/custom_button"

you can change the colors start and end for the different button modes as you like

this is an example of a custom button xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="@color/yellow1"
            android:endColor="@color/yellow2"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/Transparent" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="@color/orange4"
            android:startColor="@color/orange5"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/Transparent" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:endColor="@color/GreenYellow"
            android:startColor="@color/Green"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/Transparent" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

2 Comments

I have a image and i want change it to button. It works, but user have to see that he click it. When i click button he is a little bit darknes darker. I want the same when my button is image
oh, in that case i would go for something like jyoon answer with two, images one for normal mode and one slightly darker for when pressed. but for that you would need to have to create another image, like you said. I don;t know if there is something built in in android for this case. sorry...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.