Wednesday, 2 July 2014

Fill items botom to top




public void onClick(View arg0) {
x++;
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);

TextView textOut = (TextView) addView
.findViewById(R.id.textout);
textOut.setText(x + "");
Button buttonRemove = (Button) addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
((LinearLayout) addView.getParent())
.removeView(addView);
}
});

container.addView(addView);
}
});


row.xml



<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center" >

<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove" />

<TextView
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/remove" />

</LinearLayout>


activity_main.xml



<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|clip_vertical"
android:orientation="vertical" >
</LinearLayout>

</LinearLayout>


In the above example i inflate row.xml into activity main xml when i click button.it works fine.but i need to fill the items bottom to top.that means if i add 1 st element and then the next element should add to the top of that elemnt.In my method when i add second element id comes under the 1 element


No comments:

Post a Comment