Skip to main content

Android - Better alternative of expandable listview

This is alternate solution of Expandable ListView Android. 




Your Main Activity Class:

 package com.example.my_demo;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.Menu;  
 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.ListAdapter;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 public class MainActivity extends Activity {  
      private Button btn1;  
      private Button btn2;  
      private ListView list1;  
      ArrayList<String> a = new ArrayList<String>();  
      public boolean LI1 = false;  
      public boolean LI2 = false;  
      public boolean LI3 = false;  
      private ArrayAdapter<String> listAdapter;  
      private ListView list2;  
      private Button btn3;  
      private ListView list3;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           a.add(" Android");  
           a.add(" iPhone");  
           a.add(" Dhaval Sodha Parmar");  
           btn1 = (Button) findViewById(R.id.button1);  
           btn2 = (Button) findViewById(R.id.button2);  
           btn3 = (Button) findViewById(R.id.button3);  
           list1 = (ListView) findViewById(R.id.listView1);  
           list2 = (ListView) findViewById(R.id.listView2);  
           list3 = (ListView) findViewById(R.id.listView3);  
           // listAdapter = new ArrayAdapter<String>(this,  
           // android.R.layout.simple_list_item_1, a);  
           listAdapter = new ArrayAdapter<String>(this, R.layout.childrow, a);  
           list1.setAdapter(listAdapter);  
           btn1.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI1) {  
                          list1.setAdapter(listAdapter);  
                          Utility.setListViewChild(list1);  
                          list1.setVisibility(ListView.VISIBLE);  
                          LI1 = true;  
                     } else {  
                          list1.setVisibility(ListView.GONE);  
                          LI1 = false;  
                     }  
                }  
           });  
           btn2.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI2) {  
                          list2.setAdapter(listAdapter);  
                          Utility.setListViewChild(list2);  
                          list2.setVisibility(ListView.VISIBLE);  
                          LI2 = true;  
                     } else {  
                          list2.setVisibility(ListView.GONE);  
                          LI2 = false;  
                     }  
                }  
           });  
           btn3.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
                     if (!LI3) {  
                          list3.setAdapter(listAdapter);  
                          Utility.setListViewChild(list3);  
                          list3.setVisibility(ListView.VISIBLE);  
                          LI3 = true;  
                     } else {  
                          list3.setVisibility(ListView.GONE);  
                          LI3 = false;  
                     }  
                }  
           });  
           list1.setOnItemClickListener(new OnItemClickListener() {  
                @Override  
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                          long arg3) {  
                     // TODO Auto-generated method stub  
                     Toast.makeText(MainActivity.this,  
                               "You Select : " + a.get(arg2), Toast.LENGTH_LONG)  
                               .show();  
                }  
           });  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.activity_main, menu);  
           return true;  
      }  
      public static class Utility {  
           public static void setListViewChild(ListView list) {  
                ListAdapter listadapter = list.getAdapter();  
                if (listadapter == null) {  
                     return;  
                }  
                int totalHight = 0;  
                for (int i = 0; i < listadapter.getCount(); i++) {  
                     View listitem = listadapter.getView(i, null, list);  
                     listitem.measure(0, 0);  
                     totalHight += listitem.getMeasuredHeight();  
                }  
                ViewGroup.LayoutParams params = list.getLayoutParams();  
                params.height = totalHight  
                          + (list.getDividerHeight() * (listadapter.getCount() - 1));  
                list.setLayoutParams(params);  
           }  
      }  
 }  

Activity_main.xml

 <?xml version="1.0" encoding="UTF-8"?>  
 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/scrollView1"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:fadeScrollbars="false" >  
   <LinearLayout  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent" >  
     <LinearLayout  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       android:orientation="vertical" >  
       <Button  
         android:id="@+id/button1"  
         android:textSize="26dp"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:gravity="left|center"  
         android:text="list one" />  
       <ListView  
         android:id="@+id/listView1"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:visibility="gone" >  
       </ListView>  
       <Button  
         android:id="@+id/button2"  
         android:textSize="26dp"  
         android:gravity="left|center"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:text="list two" />  
       <ListView  
         android:id="@+id/listView2"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:visibility="gone" >  
       </ListView>  
       <Button  
         android:id="@+id/button3"  
         android:textSize="26dp"  
         android:gravity="left|center"  
         android:layout_width="match_parent"  
         android:layout_height="60dp"  
         android:background="#345ea8"  
         android:text="list three" />  
       <ListView  
         android:id="@+id/listView3"  
         android:layout_width="match_parent"  
         android:layout_height="40dp"  
         android:visibility="gone" >  
       </ListView>  
       <View  
         android:layout_width="match_parent"  
         android:layout_height="match_parent" />  
     </LinearLayout>  
   </LinearLayout>  
 </ScrollView>  


childrow.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/textView1"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   android:background="#c7e8fa"  
   android:text="Medium Text"  
   android:gravity="left|center"  
   android:textAppearance="?android:attr/textAppearanceSmall"  
   android:textColor="#345ea8"  
   android:textSize="26dp" />  

manifest file

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.my_demo"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="16" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.my_demo.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  


Enjoy Coding...... 

Comments

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l...

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :