Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Using same xml file for multiple buttons

My main navigation menu has four buttons all of these four buttons contains an image. I have created the following xml file to deal with the click event animation:

profile.xml

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/profileicon" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/profileicon" />

And the following xml file to set the effect:

gradient.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap android:src="@drawable/profileicon" />
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
            <gradient
                android:angle="90"
                android:centerColor="#880d0d0f"
                android:endColor="#885d5d5e"
                android:startColor="#880f0f10" />
        </shape>
    </item>
</layer-list>

is there any way i can avoid creating a new file pr. button i have?

This is what my main_layout looks like:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".Main" >

    <Button
        android:id="@+id/btn_yourPreferences"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignBottom="@+id/btn_signUp"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="275dp"
        android:background="@drawable/profile"
        android:onClick="onClickViewPreferences" />

    <Button
        android:id="@+id/btn_yourCompetitions"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_above="@+id/btn_yourPreferences"
        android:layout_marginBottom="54dp"
        android:layout_marginRight="230dp"
        android:layout_toLeftOf="@+id/btn_yourPreferences"
        android:background="@drawable/signedicon" />

    <Button
        android:id="@+id/btn_signUp"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignLeft="@+id/btn_yourCompetitions"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="36dp"
        android:background="@drawable/yourcompeteicon" />

    <Button
        android:id="@+id/btn_listCompetitions"
        android:layout_width="256dp"
        android:layout_height="256dp"
        android:layout_alignBaseline="@+id/btn_yourCompetitions"
        android:layout_alignBottom="@+id/btn_yourCompetitions"
        android:layout_alignLeft="@+id/btn_yourPreferences"
        android:background="@drawable/listicon"
        android:onClick="onClickViewSearch"
         />

</RelativeLayout>

Upvotes: 0

Views: 773

Answers (1)

stinepike
stinepike

Reputation: 54742

Do the following. I just made some minor changes of your codes

profile.xml

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />

gradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:angle="90"
        android:centerColor="#880d0d0f"
        android:endColor="#885d5d5e"
        android:startColor="#880f0f10" />
</shape>

main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".Main" >

<ImageButton
    android:id="@+id/btn_yourPreferences"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignBottom="@+id/btn_signUp"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="26dp"
    android:layout_marginRight="275dp"
    android:background="@drawable/profile"
    android:background="@drawable/profileicon"
    android:onClick="onClickViewPreferences" />

<ImageButton
    android:id="@+id/btn_yourCompetitions"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_above="@+id/btn_yourPreferences"
    android:layout_marginBottom="54dp"
    android:layout_marginRight="230dp"
    android:layout_toLeftOf="@+id/btn_yourPreferences"
    android:background="@drawable/profile"
    android:src="@drawable/signedicon" />

<ImageButton
    android:id="@+id/btn_signUp"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignLeft="@+id/btn_yourCompetitions"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="36dp"
    android:background="@drawable/profile"
    android:src="@drawable/yourcompeteicon" />

<ImageButton
    android:id="@+id/btn_listCompetitions"
    android:layout_width="256dp"
    android:layout_height="256dp"
    android:layout_alignBaseline="@+id/btn_yourCompetitions"
    android:layout_alignBottom="@+id/btn_yourCompetitions"
    android:layout_alignLeft="@+id/btn_yourPreferences"
    android:background="@drawable/profile"
    android:src="@drawable/listicon"
    android:onClick="onClickViewSearch"
     />

Upvotes: 2

Related Questions