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

MCP vs CLI: Which One Fits Your AI Agent's Job?

  Every AI agent that touches the outside world does it one of two ways: it runs a shell command, or it calls a tool through the Model Context Protocol. Both get the job done, but they don't cost the same or fail the same way, which is why "MCP vs CLI" keeps resurfacing without resolving. I want to answer it for the cases that matter most: plain developer tooling, data-heavy systems like banking, forecasting pipelines, and transaction automation like billing or booking. Neither wins outright. Where you draw the line is the actual skill. Key Takeaways CLI is the default for anything the model already knows cold, git, file ops, text processing, since there's no schema tax and commands chain through pipes. MCP earns its cost when a system needs per-user auth, audit trails, or access to sources the model can't otherwise reach, exactly what regulated systems like banking require. Transaction automation works best split: deterministic code drives the repeatable steps, a...

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); // s...

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...