sankettt
sankettt

Reputation: 2417

Using selectors for image view in android

I have around 6 images which i am using as tabs in my application.Its just images and they aren't the tabs that android provides.
So my question is that when i click on a image i want replace the image with a another image so that it can let user know that which tab is selected.
What i am did was creating different xml for this it in my layout.But its tedious to do so since I can't be creating different xml's for every image.
What i am trying to do now is using for this but not getting the result.

How is it possible to use for this ?

Upvotes: 0

Views: 495

Answers (1)

AkashG
AkashG

Reputation: 7888

Its quite simple.You dont have to make different xml for each image/tab.you just have to make one xml and include this xml in every xml which needs these images..in the xml set only one image as selected and the rest unselected.Suppose for the image1 keep image1 selected and the rest unselected.by clicking image2 will redirect to activity2.In activity2 set image2 selected and the rest unselected and the same for the remaining activities.This way will be the easiest way.Trust me i have implemented this and will help you too.any sort of queries you can ask me.

i have done with 4 images but in your case there will be 6.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button 
        android:id="@+id/btn1"
        style="@style/toolButton"
        android:background="@drawable/image1"           
        android:gravity="center"
        />

    <Button 
        android:id="@+id/btn2"
        style="@style/toolButton"
        android:background="@drawable/image2"
        android:gravity="center"
        />

    <Button 
        android:id="@+id/btn3"
        style="@style/toolButton"
        android:background="@drawable/image3"
        android:gravity="center"/>

    <Button 
        android:id="@+id/btn4"
        style="@style/toolButton"
        android:background="@drawable/image4"
        android:gravity="center"
       />
    </LinearLayout>
</RelativeLayout>

And here is style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="toolButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">45dip</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:layout_gravity">center_vertical</item>
        <item name="android:layout_weight">5</item>
        <item name="android:layout_marginBottom">0dip</item>
        <item name="android:layout_marginTop">0dip</item>
    </style>

</resources>

Upvotes: 1

Related Questions