I am trying to implement expandable list view. When the list is expanded, it is not scrollable anymore. The header got misplaced.
Here is the layout for the lisltview in my main activity:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/dashboard_scrollview">
<ExpandableListView
android:id="#+id/leaderboard_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:transcriptMode="alwaysScroll" />
</RelativeLayout>
and after the list view is tapped, it expanded but is unable to scroll:
I'm wondering why the header and the first element are not showing up in the list view.
MainActivity:
public void setupLeaderboardListView(){
leaderboardDetail = ExpandableLeaderboardData.getData();
leaderboardListView.setStackFromBottom(true);
leaderboardtitle = new ArrayList<String>(leaderboardDetail.keySet());
leaderboardAdapter = new LeaderboardAdapter(this, leaderboardtitle, leaderboardDetail);
leaderboardListView.setAdapter(leaderboardAdapter);
leaderboardListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
leaderboardtitle.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
leaderboardListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
leaderboardtitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
leaderboardListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
leaderboardtitle.get(groupPosition)
+ " -> "
+ leaderboardDetail.get(
leaderboardtitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
});
}
List datasource:
List<String> cricket = new ArrayList<String>();
cricket.add("India");
cricket.add("Pakistan");
cricket.add("Australia");
cricket.add("England");
cricket.add("South Africa");
cricket.add("India");
cricket.add("Pakistan");
cricket.add("Australia");
cricket.add("England");
cricket.add("South Africa");
I tried setting layout_height=match_parent for the expandable list, but it did not work
This can happen when your ExpandableListView has wrap_content for a width and height. To fix, make it the same as the parent RelativeLayout
<ExpandableListView
android:id="#+id/leaderboard_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:transcriptMode="alwaysScroll" />
First of all: ListView is depricated, It should have been dead long ago and yet, i keep seeing post on it!!!
Second:
Move to recyclerView, here are some resources that will help ya:
1. Nice introduction to it: https://developer.android.com/training/material/lists-cards.html
2. My dialog cheetSheet app (something like demo app), one of the dialogs uses expandable recyclverview library, give it a try https://github.com/WithoutCaps/DialogsCheatSheet
Use this code, it's working fine.
This is main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/expandable_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
</LinearLayout>
Your Parent Item Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tv_parent_item"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="20sp"
/>
</LinearLayout>
Your child item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/iv_child_item"
android:background="#drawable/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="100dp"
android:layout_marginTop="5dp" >
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tv_child_item1"
android:layout_marginLeft="10dp"
android:textSize="15sp"
android:text="Test"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/tv_child_item2"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:text="Test"
/>
</LinearLayout>
Custom Adapter class:
import java.util.HashMap;
import java.util.List;
import com.example.expandablelistview.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
public class ExpandableAdapter extends BaseExpandableListAdapter{
private Context context;
private List<String> parentList;
private HashMap<String , List<Songs>> childList;
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
public ExpandableAdapter(){
}
public ExpandableAdapter(Context context,List<String> parentList,
HashMap<String,List<Songs>> childList)
{
this.context=context;
this.parentList=parentList;
this.childList=childList;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(options)
.build();
imageLoader.getInstance().init(config);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.parentList.size();
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.childList.get(this.parentList.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.parentList.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.childList.get(this.parentList.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parent_item, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.tv_parent_item);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
ExpandableListView eLV = (ExpandableListView) parent;
eLV.expandGroup(groupPosition);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Songs childText = (Songs) getChild(groupPosition, childPosition);
if(convertView==null)
{
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item , null);
}
TextView artist = (TextView) convertView.findViewById(R.id.tv_child_item1);
TextView song = (TextView) convertView.findViewById(R.id.tv_child_item2);
ImageView image = (ImageView) convertView.findViewById(R.id.iv_child_item);
artist.setText(childText.getArtist());
song.setText(childText.getSong());
String imgUrl=childText.getArtwork();
imageLoader.displayImage(imgUrl, image);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
For Main Activity OnCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expListView = (ExpandableListView) findViewById(R.id.expandable_listview);
listParent = new ArrayList<String>();
listChild = new HashMap<String, List<Songs>>();
new GetData().execute();
listAdapter = new ExpandableAdapter(this, listParent, listChild);
expListView.setAdapter(listAdapter);
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listParent.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listParent.get(groupPosition)
+ " : "
+ listChild.get(
listParent.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
In Expandable List view xml used android:nestedScrollingEnabled="true"
Related
I'm a newbie in android programming, i want to create an app with ListView and multiple subitems Need some advice, link for samples.
Application may look like this.
Thanks
enter image description here
For this layout you should have notion of adapters and custom adapters .
Then go through the following link -
https://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
Expandable ListView will surely solve your problem !
Follow this link :
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
and let me know if this works for you!!
This is simple expandable list example.
first-
1-create ExpandableListView in activity_main.xml
2-Intialize and set Adapter on MainActivity.java file.
3-create your adapter.
4-create parent and child layout xml file.
my code below........
activity_main.xml..............................
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expandable List"
android:layout_gravity="center"
android:textColor="#30b171"
android:background="#dde3ec"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
MainActivity.java.........
package com.example.expandblelistapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
ExpandableListView explist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explist=(ExpandableListView) findViewById(R.id.expandableListView1);
MyExpandableListAdapter adapter=new MyExpandableListAdapter(this);
explist.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyExpandableListAdapter.java..
package com.example.expandblelistapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
String[] groups={"Android","Apache_tomcat","Apple","Microsoft","Oracle"};
String[][] children={
{"JellyBean","HoneyComb","Kitkat","CupCake","Lollipop",},
{"tomcat server","xampp server","wamp server","lamp server"},
{"iphone","ipad","iwatch"},
{".net","sqlServer","msoffice","xbox","explorer",},
{"sap","business suite"},
};
Integer[] img={R.drawable.android,R.drawable.apachetomcat,R.drawable.apple,
R.drawable.microsoft,R.drawable.oracle};
Integer[][] childrenimg={
{R.drawable.jellybean,R.drawable.gingerbread,R.drawable.kitkat,R.drawable.cupcake,R.drawable.lollipop},
{R.drawable.images,R.drawable.images,R.drawable.images,R.drawable.images,},
{R.drawable.iphone,R.drawable.ipad,R.drawable.iwatch},
{R.drawable.net,R.drawable.outlook,R.drawable.office,R.drawable.xbox,R.drawable.explore},
{R.drawable.osap,R.drawable.oserver},
};
Context ctx;
public MyExpandableListAdapter(Context context) {
ctx=context;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return children[groupPosition][childPosition];
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return children[groupPosition].length;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.child_layout,parent,false);
ImageView i=(ImageView) convertView.findViewById(R.id.imageView2);
i.setImageResource(childrenimg[groupPosition][childPosition]);
TextView dis=(TextView)convertView.findViewById(R.id.childtext);
dis.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groups[groupPosition];
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groups.length;
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.parent_layout,parent,false);
ImageView i=(ImageView) convertView.findViewById(R.id.imageView1);
i.setImageResource(img[groupPosition]);
TextView dis=(TextView)convertView.findViewById(R.id.parenttext);
dis.setText(groups[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
parent_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/parenttext"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
child_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/childtext"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Large Text"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I am developing an ExpandableListview with textview and editext like follwing.
Now I am using this link http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
But the most important thing that I want is to get all editext value should get on
SAVE button click.
My code are as follows:-
MainActivity.java
package com.recoveryreminder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.adapter.ExpandableListAdapter;
import com.constants.RecordData;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Button Save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entry_record);
// record = new RecordData();
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
Save = (Button) findViewById(R.id.btn_save);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RecoverData record1=new RecoverData();
Toast.makeText(getApplicationContext(),
"Name is:" + record1.getName(), Toast.LENGTH_SHORT)
.show();
Toast.makeText(getApplicationContext(),
"Purpose is:" + record1.getPurpose(), Toast.LENGTH_SHORT)
.show();
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Basic Info");
listDataHeader.add("Addtional Info");
// Adding child data
List<String> basic = new ArrayList<String>();
basic.add("Name");
basic.add("Purpose");
List<String> additional = new ArrayList<String>();
additional.add("Payment Date");
additional.add("Payment Due Date");
additional.add("Payment Due Time");
listDataChild.put(listDataHeader.get(0), basic); // Header, Child data
listDataChild.put(listDataHeader.get(1), additional);
}
}
ExpandableListAdapter.java
package com.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.constants.RecordData;
import com.recoveryreminder.R;
import com.recoveryreminder.RecoverData;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter implements TextWatcher{
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<EditText> editTextList = new ArrayList<EditText>();
String name,purpose;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
EditText editetext = (EditText) convertView
.findViewById(R.id.lblListItemEditext);
editetext.addTextChangedListener(this);
editTextList.add(editetext);
/**/
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
name=editTextList.get(0).getText().toString();
purpose=editTextList.get(1).getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Name is"+name);
System.out.println("purpose is"+purpose);
//Toast.makeText(_context, "Name"+name, Toast.LENGTH_LONG).show();
//Toast.makeText(_context, "Purpose"+purpose, Toast.LENGTH_LONG).show();
}
}
list-item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textColor="#000000"
android:textSize="17dip" />
<EditText
android:id="#+id/lblListItemEditext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
list-group.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="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
</LinearLayout>
entry-record.xml
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/btn_clear"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cancel_button"
android:textSize="#dimen/text_title_font_size"
android:textStyle="bold" />
<Button
android:id="#+id/btn_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/save_button"
android:textSize="#dimen/text_title_font_size"
android:textStyle="bold" />
</LinearLayout>
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_above="#+id/linearLayout2"
android:layout_below="#+id/linearLayout1"
android:layout_centerInParent="true"
android:layout_margin="#dimen/record_fields_margin"
android:cacheColorHint="#00000000" />
</RelativeLayout>
Any help is appretiated.
Add a focuschangelistener for the edittext and where u will get the position and value inside each edittext and then save those value to hashmap and then iterate that map on save button click.
I have gone through many tutorials and implemented expandablelistview with one textview as a child of groupview. Now i need to implement expandablelistview with 6-7 textviews and an imageview as childs of every group.
Can anyone help..
Sorry for poor painting
https://copy.com/IdpK0BLDUHck
please follow this code
import java.util.ArrayList;
import android.app.Activity;
import android.app.ExpandableListActivity;
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.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.parsedemoapp.ParseMainActivity;
import com.rstm.quizappsforiit.R;
public class ExpandableListMainActivity extends ExpandableListActivity {
// Create ArrayList to hold parent Items and Child Items
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
private ArrayList<Integer> childimage = new ArrayList<Integer>();
ImageView imageView2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create Expandable List and set it's properties
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
// Set the Items of Parent
setGroupParents();
// Set The Child Data
setChildData();
// Create the Adapter
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
childItems, childimage);
adapter.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
// Set the Adapter to expandableList
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
// method to add parent Items
public void setGroupParents() {
parentItems.add("Chemistry");
parentItems.add("Mathematics");
parentItems.add("Physics");
}
// method to set child data of each parent
public void setChildData() {
// Add Child Items for Fruits
ArrayList<String> child = new ArrayList<String>();
child.add("Some Basic Concepts");
child.add("States of Matter");
child.add("Atomic Structure");
child.add("Solutions");
child.add("Chemical Energetics and Thermodynamics");
child.add("Chemical and Ionic Equilibria");
child.add("Redox Reactions and Electrochemistry");
childItems.add(child);
childimage.add(R.drawable.buy1);
childItems.add(childimage);
// Add Child Items for Animals
child = new ArrayList<String>();
child.add("Physics and Measurement");
child.add("Kinematics");
child.add("Laws of Motion");
child.add("Work, Energy and Power");
child.add("Rotational Motion");
child.add("Gravitation");
child.add("Solids and Fluids");
childItems.add(child);
childItems.add(childimage);
}
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private ArrayList<Integer> childimage;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents,
ArrayList<Object> childern, ArrayList<Integer> childimage) {
this.parentItems = parents;
this.childtems = childern;
this.childimage = childimage;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
#SuppressWarnings("unchecked")
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_view, null);
}
textView = (TextView) convertView.findViewById(R.id.textViewChild);
String a = child.get(childPosition);
textView.setText(child.get(childPosition));
imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
imageView2.setBackgroundResource(childimage.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
}
});
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_view, null);
}
((CheckedTextView) convertView).setText(parentItems
.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#SuppressWarnings("unchecked")
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return parentItems.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
}
Source Code
https://drive.google.com/open?id=0BzBKpZ4nzNzUVkExUDlqRFhtcGc
package com.keshav.expandablelistviewindrawerkeshav;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class DrawerActivityExpandable extends AppCompatActivity
{
Toolbar toolbar;
private DrawerLayout drawer;
private ExpandableListView drawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ImageView profile_image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_expandable);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("ABC");
// getSupportActionBar().setDisplayShowHomeEnabled(true);
initDrawer();
// Listview Group click listener
drawerList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
drawerList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
drawerList.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
if(groupPosition==0 ||groupPosition==1||groupPosition==4){
drawer.closeDrawer(drawerList);
toolbar.setTitle(""+listDataHeader.get(groupPosition));
}
}
});
// Listview Group collasped listener
drawerList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Todo Listview on child click listener
drawerList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : keshav : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
Log.e("keshav","Child Data 1->"+listDataHeader.get(groupPosition));
Log.e("keshav","Child Data 2->"+listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
toolbar.setTitle(""+listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
// if(groupPosition==2 ||groupPosition==3)
drawer.closeDrawer(drawerList);
return false;
}
});
profile_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e("Keshav", "Clck Profile Image");
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionCheck = ContextCompat.checkSelfPermission(DrawerActivity.this,
Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
//showing dialog to select image
selectImage();
Log.e("permission", "granted");
} else {
ActivityCompat.requestPermissions(DrawerActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA}, 1);
}
} else {
selectImage();
}*/
}
});
}
private void initDrawer() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ExpandableListView) findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.nav_header_main, null);
drawerList.addHeaderView(header);
drawer.setDrawerShadow(R.mipmap.drawer_shadow, GravityCompat.START);
profile_image = (ImageView) header.findViewById(R.id.user_profile_image);
profile_image.setBackgroundResource(R.mipmap.keshav);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setHomeButtonEnabled(true);
// preparing list data
prepareListData();
drawerList.setAdapter(new NavigationDrawerExpandableAdapter(this, listDataHeader, listDataChild));
// drawerList.setOnChildClickListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Dashboard");
listDataHeader.add("Enter Manually");
listDataHeader.add("Profile");
listDataHeader.add("Reports");
listDataHeader.add("Logout");
// TODO Adding child data
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("My Profile");
nowShowing.add("Change Password");
List<String> blank = new ArrayList<String>();
List<String> report = new ArrayList<String>();
report.add("Batch Wise Report");
report.add("Location Wise Report");
report.add("Date Wise Report");
listDataChild.put(listDataHeader.get(0), blank);
listDataChild.put(listDataHeader.get(1), blank); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), report);
listDataChild.put(listDataHeader.get(4), blank);
}
}
-------------------------------
Adapter
------------------------------
package com.keshav.expandablelistviewindrawerkeshav;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by root on 11/12/15.
*/
public class NavigationDrawerExpandableAdapter extends BaseExpandableListAdapter {
public LayoutInflater minflater;
public Activity activity;
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public NavigationDrawerExpandableAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
//TODO set Hard Code Child item
ImageView iv_ListChild = (ImageView) convertView
.findViewById(R.id.iv_ListChild);
txtListChild.setText(childText);
Log.e("Keshav", "childText 1 -> " + childText);
if (childText.equals("My Profile")){
iv_ListChild.setBackgroundResource(R.mipmap.youtuben);
}else if (childText.equals("Change Password")){
iv_ListChild.setBackgroundResource(R.mipmap.facebook);
}else if (childText.equals("Batch Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.googleplus);
}else if (childText.equals("Location Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.linkedin);
}else if (childText.equals("Date Wise Report")){
iv_ListChild.setBackgroundResource(R.mipmap.twitter);
}else {
iv_ListChild.setBackgroundResource(R.mipmap.ic_launcher_round);
}
//TODO set Hard Code Child item
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.NORMAL); // TODO Set Text Color BOLD Here
lblListHeader.setText(headerTitle);
Log.e("keshav","groupPosition "+groupPosition);
ImageView iv_ListHeader = (ImageView) convertView
.findViewById(R.id.iv_ListHeader);
if(groupPosition==0)
iv_ListHeader.setBackgroundResource(R.mipmap.changepassword_48);
else if(groupPosition==1)
iv_ListHeader.setBackgroundResource(R.mipmap.barcode_48);
else if(groupPosition==2)
iv_ListHeader.setBackgroundResource(R.mipmap.changepassword_48);
else if(groupPosition==3)
iv_ListHeader.setBackgroundResource(R.mipmap.enter_manualy_48);
else
iv_ListHeader.setBackgroundResource(R.mipmap.ic_launcher);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
------------------------------------
activity_drawer.xml
-------------------------------------
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/fllContent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:groupIndicator="#null"
android:dividerHeight="1dp"
android:divider="#color/list_divider"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background" />
<!--android:groupIndicator="#null"-->
</android.support.v4.widget.DrawerLayout>
------------------------------------
listgroup.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="40dp"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/iv_ListHeader"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/lblListHeader"
style="#style/TextViewSmallBlack"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:gravity="left"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="list group"
android:textSize="14sp"
/>
</LinearLayout>
------------------------------------
listitem.xml
------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/iv_ListChild"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="10dp"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/lblListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
style="#style/TextViewSmallBlack"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:gravity="left"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="list group"
/>
</LinearLayout>
------------------------------------
navheader.xml
------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:gravity="start"
android:background="#color/colorPrimary"
android:orientation="vertical"><!--android:background="#color/com_facebook_button_background_color_disabled"-->
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/user_profile_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginLeft="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
app:border_color="#color/click_tick" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_profile_image"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="Keshav Gera"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="bold" /> <!--android:textSize="14sp"-->
<TextView
android:id="#+id/usertype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:textSize="10sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/userName"
android:layout_alignStart="#+id/userName" /> <!--android:textSize="14sp"-->
</LinearLayout>
<!--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio#android.com" />
</LinearLayout>
-->
------------------------------------
toolbar.xml
------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I am trying to write an application which takes items from a database and populates rows within a ListView. I can't click on the items after tapping on the rows and the dpad won't go to any of the rows either. I am using a custom Adapter.
Tweet.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="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="NAME"/>
<TextView
android:id="#+id/textDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#333333"
android:text="DATE" />
</LinearLayout>
<TextView
android:id="#+id/textTweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TWEET" />
</LinearLayout>
Activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:context=".MainActivity" >
</ListView>
Oncreate Method
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView view = (ListView) findViewById(R.id.listView);
MyArrayAdapter theAdapter = new MyArrayAdapter(tweetDb,this);
//setContentView(theAdapter.getView(0, null, null));
view.setAdapter(theAdapter);
view.setOnItemClickListener( new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id)
{
//Tweet theTweet = (Tweet)parent.getAdapter().getItem(position);
//saved.insert(theTweet);
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
Log.v("SCHEMA", "onItemClick fired!");
}
} );
}
Adapter Class getView
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
View tweetView = inflater.inflate(R.layout.tweet, null);
TextView textTweet = (TextView) tweetView.findViewById(R.id.textTweet);
textTweet.setText(items.get(position).getTweet());
textTweet = (TextView)tweetView.findViewById(R.id.textUser);
textTweet.setText(items.get(position).getName());
textTweet = (TextView)tweetView.findViewById(R.id.textDate);
textTweet.setText(textFormat.format(items.get(position).getCreated()));
return tweetView;
}
MyArrayAdapter Class
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyArrayAdapter implements ListAdapter
{
private ArrayList<Tweet> items;
private Context context;
private SimpleDateFormat textFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
public MyArrayAdapter(TweetDbSource db, Context context)
{
items = new ArrayList<Tweet>();
this.context = context;
Cursor cursor = db.getReadableDatabase().query("Tweet", null, null, null, null, null, null);
while (!cursor.isLast())
{
cursor.moveToNext();
items.add(new Tweet(cursor.getLong(0),cursor.getLong(1),cursor.getString(2),cursor.getString(3),
cursor.getString(4),new Date(),cursor.getInt(6)));
}
}
#Override
public int getCount()
{
return items.size();
}
#Override
public Object getItem(int position)
{
return items.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int arg0)
{
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
OnClickListener saveTweet = new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(v.getContext(), "Saved", Toast.LENGTH_LONG).show();
}
};
View tweetView = inflater.inflate(R.layout.tweet, null);
TextView textTweet = (TextView) tweetView.findViewById(R.id.textTweet);
textTweet.setText(items.get(position).getTweet());
textTweet = (TextView)tweetView.findViewById(R.id.textUser);
textTweet.setText(items.get(position).getName());
textTweet = (TextView)tweetView.findViewById(R.id.textDate);
textTweet.setText(textFormat.format(items.get(position).getCreated()));
return tweetView;
}
#Override
public int getViewTypeCount()
{
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean hasStableIds()
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEmpty()
{
// TODO Auto-generated method stub
return items.size() == 0;
}
#Override
public void registerDataSetObserver(DataSetObserver arg0)
{
// TODO Auto-generated method stub
}
#Override
public void unregisterDataSetObserver(DataSetObserver arg0)
{
// TODO Auto-generated method stub
}
#Override
public boolean areAllItemsEnabled()
{
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEnabled(int arg0)
{
// TODO Auto-generated method stub
return false;
}
}
Toast or log is not working, that's how I can tell it's not working
Updated getView Method
public View getView(int position, View convertView, ViewGroup parent)
{
OnClickListener SaveView = new OnClickListener() {
#SuppressLint("NewApi")
public void onClick(View v)
{
Toast.makeText(v.getContext(), "Saved", Toast.LENGTH_LONG).show();
v.callOnClick();
}
};
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
View tweetView = inflater.inflate(R.layout.tweet, null);
TextView textTweet = (TextView) tweetView.findViewById(R.id.textTweet);
textTweet.setText(items.get(position).getTweet());
textTweet = (TextView)tweetView.findViewById(R.id.textUser);
textTweet.setText(items.get(position).getName());
textTweet = (TextView)tweetView.findViewById(R.id.textDate);
textTweet.setText(textFormat.format(items.get(position).getCreated()));
tweetView.setOnClickListener(SaveView);
return tweetView;
}
UPDATE
The issue was solved and I figured it out :D I had to return true instead of false in these two methods within my adapter class!
#Override
public boolean areAllItemsEnabled()
{
return true;
}
#Override
public boolean isEnabled(int arg0)
{
return true;
}
Usually this happens because the items in your listview are in focus. Try adding
android:descendantFocusability="blocksDescendants"
in your custom listview row
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
I usually put the click listener in the adapter itself:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
OnClickListener yourClickListener = new OnClickListener() {
public void onClick(View v) {
//put your desired action here
v.callOnClick();
}
};
...
// then add the listener to your view
tweetView.setOnClickListener(yourClickListener);
add inside onItemClick method:
v.setSelected(true);
Add the line below to your ListView xml
android:choiceMode="singleChoice"
Tried this?
#Override
public boolean isEnabled(int position) {
//Set a Toast or Log over here to check.
return true;
}
I am implementing Expandable List view in android and i am getting the above titled error. Please help me.
Main activity is -
package com.expand;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
public class MyExpandableListViewActivity extends Activity {
/** Called when the activity is first created. */
static final String groupElements[]= {
"India",
"Australia",
"England",
"South Africa"
};
static final String[][] childElements=
{
{
"Sachin Tendulkar",
"Raina",
"Dhoni",
"Yuvi"
},
{
"Ponting",
"Adam Gilchrist",
"Michael Clarke"
},
{
"Andrew Strauss",
"kevin Peterson",
"Nasser Hussain"
},
{
"Graeme Smith",
"AB de villiers",
"Jacques Kallis"
}
};
DisplayMetrics metrics;
int width;
ExpandableListView expandList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandList = (ExpandableListView)findViewById(R.id.expandList1);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
//ExpAdapter adapter = new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements);
expandList.setAdapter(new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements));
expandList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
Log.e("onGroupExpand", "OK");
}
});
expandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
Log.e("onGroupCollapse", "OK");
}
});
expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//getting the item that is selected
//String s = (String) expandList.getItemAtPosition((int) id);
Toast.makeText(MyExpandableListViewActivity.this, "You have selected :" , Toast.LENGTH_SHORT).show();
return false;
}
});
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
ExpAdapter class is -
I have implemented the adapter in other class and have called it in mt main activity
package com.expand;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpAdapter extends BaseExpandableListAdapter {
public Context myContext;
public String[][] childElements;
TextView childValues;
public String[] groupElements;
public ExpAdapter(Context context, String[] group, String[][] childs)
{
this.myContext=context;
this.groupElements = group;
this.childElements = childs;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childElements[groupPosition][childPosition];
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.child_rows, parent);
}
childValues = (TextView)convertView.findViewById(R.id.rowValues);
childValues.setText(childElements[groupPosition][childPosition]);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return groupElements[groupPosition].length();
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupElements[groupPosition];
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupElements.length;
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.group_rows, null);
}
TextView group = (TextView)convertView.findViewById(R.id.groupId);
group.setText(groupElements[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
main.xml-
this is the xnl that is displayed at the first by the main activity
<?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" >
<ExpandableListView
android:id="#+id/expandList1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</ExpandableListView>
</LinearLayout>
group_row.xml
this is the xml for the group elements
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gropu_name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<TextView
android:id="#+id/groupId"
android:layout_height="40dp"
android:layout_width="wrap_content"
android:paddingLeft="30dp"
android:gravity="center_vertical"
android:textSize="16dp"
android:textStyle="bold"
/>
</LinearLayout>
child_row.xml
this is the xml for the child elements
<?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="40dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/rowValues"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center_vertical"
android:paddingLeft="50dp"
android:textSize="12dp" />
</LinearLayout>
Seems like Adapterview does not allow adding new view,
I encountered same problem
Solve it by replacing following line
convertView = inflator.inflate(R.layout.child_rows, parent);
to
convertView = inflator.inflate(R.layout.child_rows, null);
UPDATE
Instead of not using a parent at all, you should simply tell the Inflater not to attach the inflated view to the parent with
convertView = inflator.inflate(R.layout.child_rows, parent, false);
See also this answer.
The reason is that adapter takes care of attaching views to parent itself.
Note that you can also get this error when your layout xml is invalid.
As were noted above,
Instead of not using a parent at all, you should simply tell the
Inflater not to attach the inflated view to the parent with
convertView = inflator.inflate(R.layout.child_rows, parent, false);
See also this answer.
The reason is that adapter takes care of attaching views to parent itself.
According to Android Lint your child view should always provide a reference to its parent view when inflated. I had the exact same error in my code. Is was occurring because the TextView was placed inside the ExpandableListView. When I rearranged my xml layout the error stopped appearing.
This error can also be caused because of instant run feature. I was working on listview and because of this error app kept crashing. Uninstalling the app and running again resolved the error.