pearmak
pearmak

Reputation: 5027

android button with round color, background image, and selector

Now the button is round corner and filled with solid green when not pressed, and become solid blue with round corner when pressed. Codings are as follows:

I would like to ask how could the following be modified if I want

  1. set background images for pressed and not pressed for the button instead of solid blue and green
  2. how could I add paddings to the button because now all the buttons are sticking to each other.. I have declared padding in xml but not successful

In xml:

    <Button
        android:id="@+id/buttonC"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_span="4"
        android:background="@drawable/story_btn"
        android:onClick="buttonC_click"
        android:padding="2dp"
        android:text="abc"
        android:textSize="20dp" />

In story_btn.xml

<item android:state_pressed="true" >         
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="5dp" android:color="@color/red" />
        <solid android:color="@color/blue"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="15dp" /> 
    </shape>    
</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <stroke android:width="2dp" android:color="@color/black" />
        <solid android:color="@color/green"/>
        <padding android:left="5dp" android:top="2dp" 
            android:right="5dp" android:bottom="2dp" /> 
        <corners android:radius="15dp" /> 
    </shape>
</item>

Upvotes: 2

Views: 2984

Answers (2)

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10622

You can place rounded color image itself, you no need to create round color. Then you can apply style for state:pressed and state:selected place the image you want, defaultly you have another image in item tag

Upvotes: 0

Neil
Neil

Reputation: 468

  1. In your story_btn.xml, instead of specifying a shape for each item, specify a drawable like android:drawable="@drawable/activity_active".
  2. Use Layout_margin to specify the margin between the buttons.

Upvotes: 2

Related Questions