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

Why Domain-Specific AI Agents Beat One Big Agent

Everyone is building agents right now. Real estate firms. Independent insurance brokers. Fortune 500 companies with budgets big enough to hire an army of consultants. Ask around and you'll hear the same story everywhere: "we're building our own agent." And yet almost nobody is asking the obvious question: why does the default approach keep failing? One large, general-purpose agent gets wired up to every tool the business owns. It impresses in the demo. Then it quietly stalls before production. There's a gap between what businesses want and what they're actually getting. They want AI woven into their data, their workflows, their day-to-day operations. What they get instead is one oversized agent trying to be a sales rep, a compliance officer, and a customer support line, all at once. That gap is an architecture problem, not a model problem. Key Takeaways The default "one big agent" pattern breaks down on context bloat, cost, fragility, and portability...

Don't Ship AI Agent Skills Without Evals

At the AI Engineer World's Fair on July 1, 2026, Philipp Schmid, a Staff Engineer on the Gemini API and agents team at Google DeepMind, asked a room full of engineers a simple question: who uses skills with their coding agents? Every hand went up. Who has evals for those skills? Almost none. That gap is the entire talk, which Schmid also wrote up on his own blog, philschmid.de/testing-skills . His team indexed the skills ecosystem through SkillsBench and found the same pattern everywhere: people ship a SKILL.md file after two manual test runs and move on. It looks fine in a demo. It quietly corrupts outputs in production, because bad skills don't crash. They just make the agent confidently wrong. If you've already read our breakdown of Matt Pocock's Claude Code skills library , think of this as the other half of that story: what happens once you've installed a skill and it's actually running against real prompts. Key Takeaways SkillsBench indexed 47,000+ un...

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 :