listView Resize animation

Search This thread

Jaws1992

Member
Apr 22, 2014
13
0
I have this custom listView:

<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableRow">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/imdDesc" />

<TextView
android:id="@+id/titleList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="25sp"
android:autoText="false"
android:gravity="center_vertical|center|center_horizontal" />
</TableRow>

This adapter:

public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] title;
private final int[] colors;
private final Integer[] imageId;
public CustomList(Activity context,String[] title,int[] colors, Integer[] imageId) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.imageId = imageId;
this.colors=colors;
}
@override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.titleList);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(title[position]);
txtTitle.setTextColor(Color.parseColor("#FFffffff"));
txtTitle.setBackgroundColor(colors[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}

and this is how I bind them:

final CustomList adapter = new
CustomList(MainActivity.this, title, colors, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);

My list has 12 items. I need to resize the list when user click one item. If the user choses an Item for the first time, the list has to change its width to 1/3 of its original size and to double the height of the selected item. If an item is already selected in the list and the user selects another the old item has to go back to the original height and the new selected one has to double its height without any width changes.
I want to animate all the resizing for nice looking.

This is the animation for changing width:

public class ShrinkList extends Animation
{
int fromWidth;
View view;
int def;
public ShrinkList(View view) {
this.view = view;
this.fromWidth = view.getWidth();
def=fromWidth/3;
}
@override
public boolean willChangeBounds() {
return true;
}

@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
newWidth = (int) ((def - fromWidth) * interpolatedTime + fromWidth);
if (newWidth > def/3) {
view.getLayoutParams().width = newWidth;
view.requestLayout();
}
else{
view.getLayoutParams().width = def/3;
view.requestLayout();
}
}
}

and this one is for changing height:

public class Grow extends Animation
{
int fromHeight;
View view;
int defaultHeight;

public Grow(View view,int height) {
this.view = view;
this.fromHeight = view.getHeight();
defaultHeight=height;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
newHeight = (int) (fromHeight / (1 - interpolatedTime));
if (newHeight < defaultHeight * 2) {
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
else{
view.getLayoutParams().height = defaultHeight*2;
view.requestLayout();
}
}
}

and I play them like that:

if(h==0) { //first time the user selects an item =>change width and height
Animation a = new ShrinkList(list);
a.setInterpolator(new LinearInterpolator());
a.setDuration(1000);
list.setAnimation(a);
list.startAnimation(a);
}
Animation a1 = new Grow(view,height); //change only height
a1.setInterpolator(new LinearInterpolator());
a1.setDuration(300);
view.setAnimation(a1);
view.startAnimation(a1);


My problem is that when the list changes its width, the animation is not smooth at all and it only changes the width, not the height too.

How can I make the animation smoother and make the two animations play at once?

I have tried scaling the list, but that scales all the list. basically I need only to resize the textView within ListView nd changing layoutParams does that.

I have tried to insert the code from Grow.java into ShrinkList.Java in orefer to play both animations once and it only played the one from Grow.java.
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
I have this custom listView:

<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableRow">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/imdDesc" />

<TextView
android:id="@+id/titleList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="25sp"
android:autoText="false"
android:gravity="center_vertical|center|center_horizontal" />
</TableRow>

This adapter:

public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] title;
private final int[] colors;
private final Integer[] imageId;
public CustomList(Activity context,String[] title,int[] colors, Integer[] imageId) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.imageId = imageId;
this.colors=colors;
}
@override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.titleList);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(title[position]);
txtTitle.setTextColor(Color.parseColor("#FFffffff"));
txtTitle.setBackgroundColor(colors[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}

and this is how I bind them:

final CustomList adapter = new
CustomList(MainActivity.this, title, colors, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);

My list has 12 items. I need to resize the list when user click one item. If the user choses an Item for the first time, the list has to change its width to 1/3 of its original size and to double the height of the selected item. If an item is already selected in the list and the user selects another the old item has to go back to the original height and the new selected one has to double its height without any width changes.
I want to animate all the resizing for nice looking.

This is the animation for changing width:

public class ShrinkList extends Animation
{
int fromWidth;
View view;
int def;
public ShrinkList(View view) {
this.view = view;
this.fromWidth = view.getWidth();
def=fromWidth/3;
}
@override
public boolean willChangeBounds() {
return true;
}

@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
newWidth = (int) ((def - fromWidth) * interpolatedTime + fromWidth);
if (newWidth > def/3) {
view.getLayoutParams().width = newWidth;
view.requestLayout();
}
else{
view.getLayoutParams().width = def/3;
view.requestLayout();
}
}
}

and this one is for changing height:

public class Grow extends Animation
{
int fromHeight;
View view;
int defaultHeight;

public Grow(View view,int height) {
this.view = view;
this.fromHeight = view.getHeight();
defaultHeight=height;
}
@override
public boolean willChangeBounds() {
return true;
}
@override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
newHeight = (int) (fromHeight / (1 - interpolatedTime));
if (newHeight < defaultHeight * 2) {
view.getLayoutParams().height = newHeight;
view.requestLayout();
}
else{
view.getLayoutParams().height = defaultHeight*2;
view.requestLayout();
}
}
}

and I play them like that:

if(h==0) { //first time the user selects an item =>change width and height
Animation a = new ShrinkList(list);
a.setInterpolator(new LinearInterpolator());
a.setDuration(1000);
list.setAnimation(a);
list.startAnimation(a);
}
Animation a1 = new Grow(view,height); //change only height
a1.setInterpolator(new LinearInterpolator());
a1.setDuration(300);
view.setAnimation(a1);
view.startAnimation(a1);


My problem is that when the list changes its width, the animation is not smooth at all and it only changes the width, not the height too.

How can I make the animation smoother and make the two animations play at once?

I have tried scaling the list, but that scales all the list. basically I need only to resize the textView within ListView nd changing layoutParams does that.

I have tried to insert the code from Grow.java into ShrinkList.Java in orefer to play both animations once and it only played the one from Grow.java.

Do it like this
Code:
<RelativeLayout
      android:id="@+id/myRelativeLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      [B]android:animateLayoutChanges="true"[/B]

      <ListView
            android:id="@+id/myListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            [B]android:animateLayoutChanges="true"[/B]/>

</RelativeLayout>

Then you only need to change the width and height. And you don't have to worry about the animation ;)
 
Last edited:

Jaws1992

Member
Apr 22, 2014
13
0
Do it like this
Code:
<RelativeLayout
      android:id="@+id/myRelativeLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      [B]android:animateLayoutChanges="true"[/B]

      <ListView
            android:id="@+id/myListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            [B]android:animateLayoutChanges="true"[/B]/>

</RelativeLayout>

Then you only need to change the width and height. And you don't have to worry about the animation ;)

I have tried what you suggested and the relative layout works great, but the list doesn't animate when it changes its witdh
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
I have tried what you suggested and the relative layout works great, but the list doesn't animate when it changes its witdh

You should add the animate layout changes to the first parent with match_parent as width ;)

Sent from my SM-N9005 using XDA Premium 4 mobile app
 
Last edited:

Jaws1992

Member
Apr 22, 2014
13
0
You should add the animate layout changes to the first parent with match_parent as width ;)

Sent from my SM-N9005 using XDA Premium 4 mobile app

I created a relative layout as in your example and added animate layout changes to both the listview and that layout and changed the witth of the layout and it doesn't animate the list getting smaller. Only the next relative layout getting bigger after the list is small
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
I created a relative layout as in your example and added animate layout changes to both the listview and that layout and changed the witth of the layout and it doesn't animate the list getting smaller. Only the next relative layout getting bigger after the list is small

Okey. AnimateLayoutChanges applies to the chields. So if you want to animate the relative layout the listview is in. You need to add animateLayoutChanges to his parent

Sent from my SM-N9005 using XDA Premium 4 mobile app
 

Jaws1992

Member
Apr 22, 2014
13
0
Okey. AnimateLayoutChanges applies to the chields. So if you want to animate the relative layout the listview is in. You need to add animateLayoutChanges to his parent

Sent from my SM-N9005 using XDA Premium 4 mobile app

I added animate layout changes to all the listView ancestors and it still doesn't animate the listView.
This is the java code for changing width. Maybe I doing something wrong here:

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) linLay.getLayoutParams();
if(sdkVersion>=Build.VERSION_CODES.HONEYCOMB) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
lp.width =size.x/3;
}
else{
Display display = getWindowManager().getDefaultDisplay();
lp.width = display.getWidth()/3;
}
linLay.setLayoutParams(lp); //linLay is the listView parrent and is a relativeLayout
 
Last edited:

Rick Clephas

Senior Member
Jul 17, 2014
763
528
I added animate layout changes to all the listView ancestors and it still doesn't animate the listView.
This is the java code for changing width. Maybe I doing something wrong here:

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) linLay.getLayoutParams();
if(sdkVersion>=Build.VERSION_CODES.HONEYCOMB) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
lp.width =size.x/3;
}
else{
Display display = getWindowManager().getDefaultDisplay();
lp.width = display.getWidth()/3;
}
linLay.setLayoutParams(lp); //linLay is the listView parrent and is a relativeLayout

You have to set the animateLayoutChanges to the paretn of linLay

Sent from my SM-N9005 using XDA Premium 4 mobile app
 

Jaws1992

Member
Apr 22, 2014
13
0
You have to set the animateLayoutChanges to the paretn of linLay

Sent from my SM-N9005 using XDA Premium 4 mobile app

Yes, I have already done that.As I said, I added animate layout changes to all of the listview ancestosr meaning linLay, parent of linLay, parent of parent of linLay and so on until the root of layouts, but the listView still doesn't animate
 

Jaws1992

Member
Apr 22, 2014
13
0
Okey could you post your xml

Sent from my SM-N9005 using XDA Premium 4 mobile app

<LinearLayout
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:id="@+id/titleLayout"
android:visibility="visible"
android:animateLayoutChanges="true">


<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imagew"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true" />

<LinearLayout
android:eek:rientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/share"
android:layout_alignEnd="@+id/share"
android:layout_toRightOf="@+id/imagew"
android:layout_toEndOf="@+id/imagew"
android:gravity="center_horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/title"
android:textSize="40sp"
android:gravity="center|right"
android:layout_gravity="left"
android:textColor="#ffffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/date"
android:textSize="30sp"
android:gravity="clip_horizontal|center|center_horizontal"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:textColor="#ffffffff"
android:textStyle="italic" />
</LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/share"
android:adjustViewBounds="false"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:contentDescription="@string/share" />

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linLay"
android:animateLayoutChanges="true">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:headerDividersEnabled="false"
android:dividerHeight="0dp"
android:animateLayoutChanges="true"/>
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_toRightOf="@id/linLay"
android:id="@+id/contentLayout"
android:animateLayoutChanges="true">





<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
android:textColor="#ffffffff"
android:typeface="normal"
android:gravity="left|fill" />

<LinearLayout
android:eek:rientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"
android:background="#ff647ab1"
android:visibility="gone">

<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:background="@drawable/fb" />

<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/FB"
android:id="@+id/fb"
android:textColor="#ffffffff"
android:textStyle="bold"
android:textSize="50sp" />
</LinearLayout>



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>


</RelativeLayout>
</RelativeLayout>

</LinearLayout>
 

Rick Clephas

Senior Member
Jul 17, 2014
763
528
<LinearLayout
android:eek:rientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:id="@+id/titleLayout"
android:visibility="visible"
android:animateLayoutChanges="true">


<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imagew"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true" />

<LinearLayout
android:eek:rientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/share"
android:layout_alignEnd="@+id/share"
android:layout_toRightOf="@+id/imagew"
android:layout_toEndOf="@+id/imagew"
android:gravity="center_horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/title"
android:textSize="40sp"
android:gravity="center|right"
android:layout_gravity="left"
android:textColor="#ffffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/date"
android:textSize="30sp"
android:gravity="clip_horizontal|center|center_horizontal"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:textColor="#ffffffff"
android:textStyle="italic" />
</LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/share"
android:adjustViewBounds="false"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentEnd="false"
android:layout_centerVertical="true"
android:contentDescription="@string/share" />

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linLay"
android:animateLayoutChanges="true">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:headerDividersEnabled="false"
android:dividerHeight="0dp"
android:animateLayoutChanges="true"/>
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_toRightOf="@id/linLay"
android:id="@+id/contentLayout"
android:animateLayoutChanges="true">





<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:visibility="visible"
android:textColor="#ffffffff"
android:typeface="normal"
android:gravity="left|fill" />

<LinearLayout
android:eek:rientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"
android:background="#ff647ab1"
android:visibility="gone">

<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:background="@drawable/fb" />

<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/FB"
android:id="@+id/fb"
android:textColor="#ffffffff"
android:textStyle="bold"
android:textSize="50sp" />
</LinearLayout>



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>


</RelativeLayout>
</RelativeLayout>

</LinearLayout>

Does the width change?

Sent from my SM-N9005 using XDA Premium 4 mobile app