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 - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

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