ExpandableTxtVewUpdated.java:
package org.example.Dharani;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ExpandableTxtVewUpdated extends Activity{
TextView toptext;
String[] items={"Atlanta lasdh asjdl asl jdlas ldkj asldkjlasjdljjasdas ljjds;jf;s dsfk;sd kdsf;woe p wre", "Boston", "Chicago dfds ddskl jfljfljdslfl jdslfsff dsfdsfds fdsfdsf dsf dsffds dsf sds", "Dallas"};
private CityListAdapter mListOfCities;
ListView listview;
int positionList=-1;
boolean mAtlantaListExpanded;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mAtlantaListExpanded = false;
listview=(ListView)findViewById(R.id.list);
mListOfCities=new CityListAdapter(this,items);
listview.setAdapter(mListOfCities);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Log.d("ListView", String.valueOf(position));
//here "positionList" variable holding the previously selected rowID..
//below line is important,because it handles when ever the user clicks a row,without compressing the previously selected row
//(i.e user clicks two rows immediatly one after other..)
if(positionList>-1){
if(positionList!=position){
positionList=position;
mAtlantaListExpanded = !mAtlantaListExpanded;
listview.setAdapter(mListOfCities);
}
}
//code to get the selected list row
positionList=position;
mAtlantaListExpanded = !mAtlantaListExpanded;
listview.setAdapter(mListOfCities);
Log.i("SpecialList", "Atlanta");
}
});
}//onCreate..
class CityListAdapter extends ArrayAdapter<String> {
Context Context=null;
String[] Items;
public CityListAdapter(Context context, String[] items) {
super(ExpandableTxtVewUpdated.this,R.layout.one_city_row,items);
System.out.println(" Array length "+items.length);
Context = context;
Items=new String[items.length];
for(int i=0;i<items.length;i++){
System.out.println(" Array element "+items[i]);
Items[i] = items[i];
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
System.out.println(" mAtlantaListExpanded "+mAtlantaListExpanded);
View row = null;
LayoutInflater inflater = getLayoutInflater();
if (position == positionList && mAtlantaListExpanded)
{
System.out.println(" position && mAtlantaListExpand " +position +" : "+mAtlantaListExpanded);
row = inflater.inflate(R.layout.expandable_city_row, parent, false);
TextView city = (TextView) row.findViewById(R.id.City_TxtViewExpand);
city.setText(items[position]);
TextView cityZone1 = (TextView) row.findViewById(R.id.moreBtnExpand);
cityZone1.setText("Less");
TextView cityZone2 = (TextView) row.findViewById(R.id.editBtnExpand);
cityZone2.setText("Edit");
cityZone1.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("SpecialList", "Zone 1");
//code to compress the expanded listView
mAtlantaListExpanded = !mAtlantaListExpanded;
listview.setAdapter(mListOfCities);
Log.i("SpecialList", "Atlanta");
}
}
);
cityZone2.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("SpecialList", "Zone 2");
Intent intent = new Intent();
intent.setClass(ExpandableTxtVewUpdated.this, EditScreen.class);
startActivity(intent);
}
}
);
}
else
{
System.out.println(" %%%%% else part");
row = inflater.inflate(R.layout.one_city_row, parent, false);
TextView city = (TextView) row.findViewById(R.id.City_TextView01);
TextView moreBtn=(TextView)row.findViewById(R.id.moreBtn);
TextView editBtn=(TextView)row.findViewById(R.id.editBtn);
//code to visible/hide More,Hide buttons based on Text size
if(Items[position].length()<=30){
city.setText(items[position]); //display only 30-chars if string length<=30
moreBtn.setText("Less");
}
else
{
city.setText(items[position].toString().substring(0, 29));
}
editBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ExpandableTxtVewUpdated.this, EditScreen.class);
startActivity(intent);
}
});
}
return(row);
}
}
}
one_city_row.xml:-ewrrwe
<?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="fill_parent"
android:padding="6dip"
android:orientation="vertical" >
<TextView
android:id="@+id/City_TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="City_TextView01 dsjfl dsflkds lfdsfjds jkl fdsjf kldsfldsf dsflkdsjfkllds flldslf ldslfldsfdsfl dslfdsklf ldsfldsllfldslfdsfdsfdslfdsfdslfdsfldsflds" />
<TextView
android:id="@+id/moreBtn"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_below="@+id/City_TextView01"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="More"
android:background="#C0C0C0"
android:textColor="#000000"
android:gravity="center"
/>
<TextView
android:id="@+id/editBtn"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_below="@+id/City_TextView01"
android:layout_marginTop="15dip"
android:layout_marginRight="30dp"
android:background="#C0C0C0"
android:gravity="center"
android:text="Edit"
android:textColor="#000000" />
</RelativeLayout>
main.xml:-------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="PavanTilakPavanTilakPavanTilak" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="false"
/>
</LinearLayout>
expandable_city_row.xml:----------------------
<?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="fill_parent"
android:padding="6dip"
android:orientation="vertical" >
<TextView
android:id="@+id/City_TxtViewExpand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="City_TextView01" />
<TextView
android:id="@+id/moreBtnExpand"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_below="@+id/City_TxtViewExpand"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="More"
android:background="#C0C0C0"
android:textColor="#000000"
android:gravity="center"
/>
<TextView
android:id="@+id/editBtnExpand"
android:layout_width="60dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_below="@+id/City_TxtViewExpand"
android:layout_marginTop="15dip"
android:layout_marginRight="30dp"
android:background="#C0C0C0"
android:gravity="center"
android:text="Edit"
android:textColor="#000000" />
</RelativeLayout>
Note:--EditScreen.class will be any Activity,create ur own Activity.i just used it temporarilyyy..
(Used in LegalPlex project)
Comments
Post a Comment