4

I used one LinearLayout in my activity for changing two views. Here, i used two textviews. When user clicks on linear layout i change views as per my requirnments. xml file is here,

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/customswitchselector"
            android:textStyle="bold"
            android:layout_below="@+id/linearTab"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:showText="true"
            android:id="@+id/switch1"
            android:checked="false"
            android:weightSum="2">
            <TextView
                android:id="@+id/tv_switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:textStyle="bold"
                android:textColor="#272351"
                style="bold" />
            <TextView
                android:id="@+id/tv_switch_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1"
                android:textStyle="bold"
                android:layout_gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:textColor="#ffffff"
                style="bold"
                />
        </LinearLayout>

first image when not clickedsecond image after click

These two images show my two conditions. I have done enough to change this. Now i want to do animation while i am changing background for textviews on layout clicks. I have checked transitiondrawable example but it not helpful to me.

customswitchselector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
            <gradient android:angle="270" android:endColor="#ffffff" android:startColor="#ffffff" />
            <corners android:radius="30dp" />
        </shape>
    </item>
    <item android:state_checked="false">
        <shape android:dither="true" android:shape="rectangle" android:useLevel="false" android:visible="true">
            <gradient android:angle="270" android:endColor="#ffffff" android:startColor="#ffffff" />
            <corners android:radius="30dp" />
        </shape>
    </item>
</selector>

custom_track.xml This is used for textview with blue background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:shape="rectangle"
    android:useLevel="false"
    android:visible="true">
    <gradient
        android:angle="270"
        android:endColor="#292850"
        android:startColor="#292850" />
    <corners android:radius="30dp" />
    <size
        android:width="100dp"
        android:height="30dp" />

</shape>
Lalit Jadav
  • 1,409
  • 1
  • 15
  • 38
  • try this link http://stackoverflow.com/questions/2614545/animate-change-of-view-background-color-in-android?rq=1 – Durgesh Feb 04 '16 at 05:34
  • Why don't you use Translate Animation ? I think it will be more suitable for your feature. Reference : http://stackoverflow.com/questions/10276251/how-to-animate-a-view-with-translate-animation-in-android – user5716019 Feb 04 '16 at 05:35
  • Here i am using custom_track drawable for look like blue background with round corner and after click event i change background with Color.TRANSPARENT. I already mention Mr. Durgesh transition animation not works in my work. I am not moving my view from one place to another place that's why i don't use translate animation. – Lalit Jadav Feb 04 '16 at 05:47
  • did u try adding <<>> to your parent linear layout. – Ismail Iqbal Mar 24 '17 at 18:31
  • Post also `@drawable/customswitchselector`. – azizbekian Mar 26 '17 at 10:48
  • @IsmailIqbal yes i have tried that – Lalit Jadav Mar 27 '17 at 05:34
  • It will be better to use custom radio button in this scenario this Example:http://www.limbaniandroid.com/2014/05/custom-radio-buttons-example-in-android.html – Burhanuddin Rashid Mar 28 '17 at 06:45

1 Answers1

1

A possibility would be to set the white drawable as background of a FrameLayout and add a View for the blue oval and two TextViews for the titles to it.

To still acchieve that the views takes exactly the half of the container you could use PercenFrameLayout (read more about it in the official docu). To use it you need to add this dependency to your build.gradle: compile 'com.android.support:percent:25.2.0'

After creating the layout you just animate the gravity of the blue oval from left to right or the other way round.

So your layout would look like this:

<PercentFrameLayout
    android:id="@+id/container"
    android:animateLayoutChanges="true"
    android:background="@drawable/white_oval"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!-- blue oval -->
    <View
        android:id="@+id/blue_oval"
        android:gravity="start"
        android:background="@drawable/blue_oval"
        app:layout_widthPercent="50%"
        android:layout_height="wrap_content"
        android:transformPivotX="0dp" />

    <!--2 textviews-->
    <TextView
        app:layout_widthPercent="50%"
        android:layout_height="wrap_content" 
        android:text="left"
        android:gravity="start"/>

    <TextView
        app:layout_widthPercent="50%"
        android:layout_height="wrap_content"
        android:text="right"
        android:gravity="end"/>
</FrameLayout>

Then in your code animate the gravity change of the blue oval like this:

private void animateGravity(int toGravity) {
    LayoutTransition layoutTransition = container.getLayoutTransition();
    layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
    layoutTransition.setDuration(YOUR_ANIMATION_DURATION);
    layoutTransition.setInterpolator(LayoutTransition.CHANGING, new AccelerateDecelerateInterpolator());

    FrameLayout.LayoutParams layoutParams = (LayoutParams) blueOvalView.getLayoutParams();
    layoutParams.gravity = toGravity;
    blueOvalView.setLayoutParams(layoutParams);
}
Katharina
  • 1,612
  • 15
  • 27