I have a menu defined in xml:
<group android:checkableBehavior="single" android:id="#+id/group">
<item
android:id="#+id/grades"
android:icon="#drawable/ic_font_download_black_48dp"
android:checked="false"
android:title="Grades" >
<menu>
<item
android:id="#+id/mp1"
android:icon="#drawable/ic_looks_one_black_24dp"
android:checked="false"
android:title="MP1" />
<item
android:id="#+id/mp2"
android:icon="#drawable/ic_looks_two_black_24dp"
android:checked="false"
android:title="MP2" />
<item
android:id="#+id/mp3"
android:icon="#drawable/ic_font_download_black_48dp"
android:checked="false"
android:title="MP3" />
<item
android:id="#+id/mp4"
android:icon="#drawable/ic_font_download_black_48dp"
android:checked="false"
android:title="MP4" />
</menu>
</item>
<item
android:id="#+id/schedule"
android:icon="#drawable/ic_event_black_48dp"
android:checked="false"
android:title="Schedule" />
<item
android:id="#+id/attendance"
android:icon="#drawable/ic_assignment_ind_black_48dp"
android:checked="false"
android:title="Attendance" />
<item
android:id="#+id/assignments"
android:icon="#drawable/ic_assignment_black_48dp"
android:checked="false"
android:title="Assignments" />
<item
android:id="#+id/studentInfo"
android:icon="#drawable/ic_account_circle_black_48dp"
android:checked="false"
android:title="Student Details" />
</group>
<group android:id="#+id/group2" android:checkableBehavior="single">
<item
android:id="#+id/placeholder"
android:icon="#drawable/ic_android_black_48dp"
android:checked="false"
android:title="Placeholder" />
<item
android:id="#+id/placeholder2"
android:icon="#drawable/ic_android_black_48dp"
android:checked="false"
android:title="Placeholder" />
</group>
which adds a submenu which looks like this:
However, I am unable to make this submenu look like this: (collapsable)
I now know that I need to implement an expandableListView, but I am not sure how this would fit in with the menu I have already created.
Any advice is appreciated
You can create it using custom ListView.
See the code below activity_navigation_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<ExpandableListView
android:id="#+id/navigationmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="192dp"
android:background="#android:color/white">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
The adapter for expandable list view is as follows.
ExpandableListAdapter.java
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.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<ExpandedMenuModel> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<ExpandedMenuModel, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader, HashMap<ExpandedMenuModel, List<String>> listChildData, ExpandableListView mView) {
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList = mView;
}
#Override
public int getGroupCount() {
int i = mListDataHeader.size();
Log.d("GROUPCOUNT", String.valueOf(i));
return this.mListDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
int childCount = 0;
if (groupPosition != 2) {
childCount = this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
return childCount;
}
#Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
Log.d("CHILD", mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition).toString());
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageResource(headerTitle.getIconImg());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_submenu.xml is as follows.
<?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:id="#+id/submenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:padding="10dp"
android:textColor="#000000"
android:textSize="18sp"/>
</LinearLayout>
listheader.xml is as follows.
<?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="2dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/iconimage"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"/>
<TextView
android:id="#+id/submenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
In your navigation view activity, set the adapter for the expandable list view.
NavigationViewActivity.java
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
final ActionBar ab = getSupportActionBar();
/* to set the menu icon image*/
ab.setHomeAsUpIndicator(android.R.drawable.ic_menu_add);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList = (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
//Log.d("DEBUG", "submenu item clicked");
return false;
}
});
expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
//Log.d("DEBUG", "heading clicked");
return false;
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<ExpandedMenuModel>();
listDataChild = new HashMap<ExpandedMenuModel, List<String>>();
ExpandedMenuModel item1 = new ExpandedMenuModel();
item1.setIconName("heading1");
item1.setIconImg(android.R.drawable.ic_delete);
// Adding data header
listDataHeader.add(item1);
ExpandedMenuModel item2 = new ExpandedMenuModel();
item2.setIconName("heading2");
item2.setIconImg(android.R.drawable.ic_delete);
listDataHeader.add(item2);
ExpandedMenuModel item3 = new ExpandedMenuModel();
item3.setIconName("heading3");
item3.setIconImg(android.R.drawable.ic_delete);
listDataHeader.add(item3);
// Adding child data
List<String> heading1 = new ArrayList<String>();
heading1.add("Submenu of item 1");
List<String> heading2 = new ArrayList<String>();
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
//revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
}
ExpandedMenuModel class contains menu item details as follow.
ExpandedMenuModel.java
public class ExpandedMenuModel {
String iconName = "";
int iconImg = -1; // menu icon resource id
public String getIconName() {
return iconName;
}
public void setIconName(String iconName) {
this.iconName = iconName;
}
public int getIconImg() {
return iconImg;
}
public void setIconImg(int iconImg) {
this.iconImg = iconImg;
}
}
[Side note]:
Don't put import android.widget.ExpandableListAdapter; in NavigationViewActivity.java. This mistake can happen if you resolve import by Alt+Enter before create the file ExpandableListAdapter.java.
Put compile 'com.android.support:design:23.3.0' in app's build.gradle, it's for "NavigationView" and its "import android.support.design.widget.NavigationView;" After that (Might require rebuild first) you can do Alt-Enter to resolve import.
The file hierarchy should look like (3 .java and 3 .xml from above):
The output screenshot:
MainActivity.java
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
View view_Group;
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
//Icons, use as you want
static int[] icon = { R.drawable.ico1, R.drawable.ico1,
R.drawable.ico1, R.drawable.ico1,
R.drawable.ico1, R.drawable.ico1, R.drawable.ico1};
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expandableList.setIndicatorBounds(expandableList.getRight()- 80, expandableList.getWidth());
} else {
expandableList.setIndicatorBoundsRelative(expandableList.getRight()- 80, expandableList.getWidth());
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList = (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView expandableListView,
View view,
int groupPosition,
int childPosition, long id) {
//Log.d("DEBUG", "submenu item clicked");
Toast.makeText(MainActivity.this,
"Header: "+String.valueOf(groupPosition) +
"\nItem: "+ String.valueOf(childPosition), Toast.LENGTH_SHORT)
.show();
view.setSelected(true);
if (view_Group != null) {
view_Group.setBackgroundColor(Color.parseColor("#ffffff"));
}
view_Group = view;
view_Group.setBackgroundColor(Color.parseColor("#DDDDDD"));
mDrawerLayout.closeDrawers();
return false;
}
});
expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
//Log.d("DEBUG", "heading clicked");
return false;
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding data header
listDataHeader.add("menu1");
listDataHeader.add("menu2");
listDataHeader.add("menu3");
listDataHeader.add("menu4");
listDataHeader.add("menu5");
listDataHeader.add("menu6");
listDataHeader.add("menu7");
// Adding child data
List<String> heading1 = new ArrayList<String>();
heading1.add("Submenu");
heading1.add("Submenu");
heading1.add("Submenu");
List<String> heading2 = new ArrayList<String>();
heading2.add("Submenu");
heading2.add("Submenu");
heading2.add("Submenu");
heading2.add("Submenu");
List<String> heading3 = new ArrayList<String>();
heading3.add("Submenu");
heading3.add("Submenu");
List<String> heading4 = new ArrayList<String>();
heading4.add("Submenu");
heading4.add("Submenu");
List<String> heading5 = new ArrayList<String>();
heading5.add("Submenu");
heading5.add("Submenu");
heading5.add("Submenu");
List<String> heading6 = new ArrayList<String>();
heading6.add("Submenu");
heading6.add("Submenu");
List<String> heading7 = new ArrayList<String>();
heading4.add("Submenu");
heading4.add("Submenu");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
listDataChild.put(listDataHeader.get(2), heading3);
listDataChild.put(listDataHeader.get(3), heading4);
listDataChild.put(listDataHeader.get(4), heading5);
listDataChild.put(listDataHeader.get(5), heading6);
listDataChild.put(listDataHeader.get(6), heading7);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
/*if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}*/
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
ExpandableListAdapter.java
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by Administrator on 9/1/16.
*/
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context,
List<String> listDataHeader,
HashMap<String,
List<String>> listChildData
// ,ExpandableListView mView
)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
//this.expandList = mView;
}
#Override
public int getGroupCount() {
int i = mListDataHeader.size();
//Log.d("GROUPCOUNT", String.valueOf(i));
return i;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.mListDataChild.get(
this.mListDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
//Log.d("CHILD", mListDataChild.get(this.mListDataHeader.get(groupPosition))
// .get(childPosition).toString());
return this.mListDataChild.get(
this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
//lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageResource(MainActivity.icon[groupPosition]);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_submenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:layout_marginLeft="44dp"
android:textSize="14sp"
android:id="#+id/submenu"/>
</LinearLayout>
listheader.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="#+id/iconimage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:padding="10dp"
android:textColor="#000000"
android:textSize="16sp"
android:id="#+id/submenu"
android:gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_marginTop="160dp"
android:choiceMode="singleChoice"
android:id="#+id/navigationmenu">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Hope this will work and some other files are in this example... I think you can solve those matters... It will look like as you are wanted...
You can follow Priyank Patel answer above. Additionally, I wanted to hide the drop-down icon in front of the list item.
Just add android:groupIndicator="#null" to your ExpandableListView in xml tag.
<ExpandableListView
android:id="#+id/navigationmenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:groupIndicator="#null"
android:layout_marginTop="192dp"
android:background="#android:color/white">
</ExpandableListView>
You can do it by simple way without ExpandableListView
1- for showing down arrow u can customize menu item by actionLayout,
also I'm giving unique id for each group to show divider line
**Media has 3 sub items
<item
android:id="#+id/nav_media"
android:title="Media"
app:actionLayout="#layout/nav_arrow" />
<group
android:id="#+id/media_group"
android:checkableBehavior="single"
android:visible="false">
<item
android:title="Media1"
app:actionLayout="#layout/nav_arrow"
/>
<item
android:title="Media2"
app:actionLayout="#layout/nav_arrow"
/>
<item
android:title="Media3"
app:actionLayout="#layout/nav_arrow"
/>
</group>
2- this is nav_arrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="#+id/igv_arrow"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginStart="5dp"
android:gravity="center"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_16_arrow_right"/>
</LinearLayout>
3- then in Activity
private boolean isMediaVisible= false;
case R.id.nav_media:
if (!isMediaVisible) {
menuItemArrow.setRotation(90f);//to rotating arrow to down
navigationView.getMenu().setGroupVisible(R.id.media_group, true);
isMediaVisible= true;
} else {
menuItemArrow.setRotation(0f);
navigationView.getMenu().setGroupVisible(R.id.media_group, false);
isMediaVisible= false;
}
return;
Thanks #Priyank and #atabek it relay helped me
when i completed this code it was working fine except the item click event and it was not showing selected item for that i made a small changes
in layout ExpandableListView i have added listSelector (http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:listSelector)
<ExpandableListView
android:id="#+id/navigationmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="192dp"
android:background="#android:color/white"
android:choiceMode="singleChoice"
android:listSelector="#color/colorAccent"
/>
and in activity instead of setting up the click listener to NavigationView changed to expandableList (http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html)
------
------
private void setupDrawerContent(NavigationView navigationView) {
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
Toast.makeText(MainActivity.this, "clicked " + listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).toString(), Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawers();
return true;
}
});
/*
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
*/
}
------
------
Related
Good evening everyone !
I am currently working on an ExpandableListView.
The parent contains 1 ImageView.
The child contains 1 TextView and 3 Buttons.
In terms of display, no problem: everything works as I want.
However, I want to add actions to my buttons and I realize that when I put a SetOnclickListener in the getChildView I am not able to launch a new INTENT.
The examples I find on the internet seem to show only clicks on dynamic elements. However, the buttons are not dynamic in my case.
I planned to use a variable to do a SWITCH and thus launch the right INTENT.
But since it doesn't work, I wonder if my direction is the right one.
MaintActivity.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
public class jeux2 extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<Integer> headerData;
HashMap<Integer,ArrayList<ChildDataModel>> childData;
ChildDataModel childDataModel;
Context mContext;
ArrayList<ChildDataModel> logo0,logo1,logo2,logo3,logo4,logo5,logo6,logo7,logo8,logo9,logo10,logo11;
private int lastExpandedPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.jeux2);
mContext = this;
headerData = new ArrayList<>();
childData = new HashMap<Integer, ArrayList<ChildDataModel>>();
logo0 = new ArrayList<>();
logo1 = new ArrayList<>();
logo2 = new ArrayList<>();
logo3 = new ArrayList<>();
logo4 = new ArrayList<>();
logo5 = new ArrayList<>();
logo6 = new ArrayList<>();
logo7 = new ArrayList<>();
logo8 = new ArrayList<>();
logo9 = new ArrayList<>();
logo10 = new ArrayList<>();
logo11 = new ArrayList<>();
// get the listview
expListView = (ExpandableListView) findViewById(R.id.ExpandableListView);
// populate data
// Adding header data
headerData.add(R.drawable.logo0);
headerData.add(R.drawable.logo1);
headerData.add(R.drawable.logo2);
headerData.add(R.drawable.logo3);
headerData.add(R.drawable.logo4);
headerData.add(R.drawable.logo5);
headerData.add(R.drawable.logo6);
headerData.add(R.drawable.logo7);
headerData.add(R.drawable.logo8);
headerData.add(R.drawable.logo9);
headerData.add(R.drawable.logo10);
headerData.add(R.drawable.logo11);
// Adding child data
childDataModel = new ChildDataModel(1,"Test0","logo0");
logo0.add(childDataModel);
childData.put(headerData.get(0),logo0);
childDataModel = new ChildDataModel(1,"Test1","logo1");
logo1.add(childDataModel);
childData.put(headerData.get(1),logo1);
childDataModel = new ChildDataModel(1,"Test2","logo2");
logo2.add(childDataModel);
childData.put(headerData.get(2),logo2);
childDataModel = new ChildDataModel(1,"Test3","logo3");
logo3.add(childDataModel);
childData.put(headerData.get(3),logo3);
childDataModel = new ChildDataModel(1,"Test4","logo4");
logo4.add(childDataModel);
childData.put(headerData.get(4),logo4);
childDataModel = new ChildDataModel(1,"Test5","logo5");
logo5.add(childDataModel);
childData.put(headerData.get(5),logo5);
childDataModel = new ChildDataModel(1,"Test6","logo6");
logo6.add(childDataModel);
childData.put(headerData.get(6),logo6);
childDataModel = new ChildDataModel(1,"Test7","logo7");
logo7.add(childDataModel);
childData.put(headerData.get(7),logo7);
childDataModel = new ChildDataModel(1,"Test8","logo8");
logo8.add(childDataModel);
childData.put(headerData.get(8),logo8);
childDataModel = new ChildDataModel(1,"test09","logo9");
logo9.add(childDataModel);
childData.put(headerData.get(9),logo9);
childDataModel = new ChildDataModel(1,"test10","logo10");
logo10.add(childDataModel);
childData.put(headerData.get(10),logo10);
childDataModel = new ChildDataModel(1,"test11","logo11");
logo11.add(childDataModel);
childData.put(headerData.get(11),logo11);
listAdapter = new ExpandableListAdapter(this, headerData, childData);
// setting list adapter
expListView.setAdapter(listAdapter);
//--------------------------child click listener--------------------------
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
Log.d("TEST", "onChildClick: " + groupPosition + " " + childPosition + " " + id + " " + parent);
//Toast.makeText(getApplicationContext(), "heeeeeeeeerrrrreeeee!", Toast.LENGTH_SHORT).show();
return true;
}
});
//group expanded
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int headPosition) {
if (lastExpandedPosition != -1
&& headPosition != lastExpandedPosition) {
expListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = headPosition;
}
});
//group collapsed
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int headPosition) {
}
});
//--------------------------RETOUR--------------------------
Button retour = (Button) findViewById(R.id.retour);
retour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, MainActivity.class);
startActivity(Intent_next);
}
});
//--------------------------HISTO--------------------------
TextView histo = (TextView) findViewById(R.id.histo);
histo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent Intent_next = new Intent(jeux2.this, historique.class);
startActivity(Intent_next);
}
});
}
}
class ChildDataModel
{
long id;
String txt;
String aventure;
public ChildDataModel(int id, String explications, String aventure) {
this.setId(id);
this.setTxt(explications);
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTxt() {
return txt;
}
public void setTxt(String explications) {
this.txt = explications;
}
public String getTxt_aventure() {
return aventure;
}
#Override
public String toString()
{
return super.toString();
}
}
ExpandableListAdapter.java
package com.evo.tab2escape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<Integer> headerData; // Images
// child data in format of header , child
private HashMap<Integer, ArrayList<ChildDataModel>> childData;
public ExpandableListAdapter(Context context, List<Integer> listDataHeader,
HashMap<Integer, ArrayList<ChildDataModel>> childData) {
this._context = context;
this.headerData = listDataHeader;
this.childData = childData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this.childData.get(this.headerData.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) {
ChildDataModel child = (ChildDataModel) 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 explications = (TextView) convertView.findViewById(R.id.explications);
explications.setText(child.getTxt());
Button play = (Button) convertView.findViewById(R.id.play);
Button doc = (Button) convertView.findViewById(R.id.doc);
Button param = (Button) convertView.findViewById(R.id.param);
final String aventure = child.getTxt_aventure();
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
doc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
param.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("TAG", "poulet: ");
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.childData.get(this.headerData.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.headerData.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.headerData.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
int imageId = this.headerData.get(groupPosition);
logo.setImageResource(imageId);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_group.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="wrap_content"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="20dp">
</Space>
<ImageView
android:id="#+id/logo"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="100dp"/>
</LinearLayout>
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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:id="#+id/divers">
<TextView
android:id="#+id/explications"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:textStyle="bold"
android:layout_weight="1.5">
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jouer"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="#+id/doc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Documents"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
<Button
android:id="#+id/param"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paramètres"
android:layout_gravity="center"
android:background="#drawable/bouton_main"
android:layout_weight="1">
</Button>
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</Space>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Can somoene help me ?
I want to start a new intent on click on Button.
Edit : i add child Click Listener on MainActivity but its works only if i set clickable,focusable = false on the 3 Buttons in layout list_item.xml.
And than the click is on the complete child. I can't say if the click is on Button 1, 2 or 3.
Can we fix it ?
Problem solved.
I do this in ExpandableListAdapter in getChildView.
param.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent next = new Intent(parent.getContext(), parametres.class);
parent.getContext().startActivity(next);
}
});
I implemented a navigation drawer, with navigation view. and i am adding value in navigation view through a menu.xml file.
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#android:color/white"
android:background="?attr/colorAccent"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/nav_header"
>
</android.support.design.widget.NavigationView>
every thing is working fine, but i am facing a problem, I want to display submenu's , after click on menu.
I tried many thing's like:-
I add submenu in menu.xml, inside item.
somthing like this..
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ChangeOutlet_fragment"
android:icon="#drawable/home_icon"
android:title="#string/changeOutlet"
android:checked="true">
<menu>
<group>
<item
android:title="one"></item>
<item
android:title="two"></item>
<item
android:title="three"></item>
</group>
</menu>
then it return me output like this.
Output: click on this link to see output
https://drive.google.com/file/d/0B0B9-WZYydK7RG1yY0tRdkhOSW8/view?usp=sharing
Now problem is that, in this way i am not able to click on menu , only sub menu are clickable here.
I want here to show sub menu , only after click on menu, otherwise sub menu does shown to other or say it is in invisible state
The best solution is to have an expandable list view in navigation view.See the code below
activity_navigation_view.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/navigation_view_fragment_container"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/navigation_view_header">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:layout_marginTop="192dp"
android:id="#+id/navigationmenu">
</ExpandableListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
The layout navigation header is as below
navigation_view_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="#ff5722"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
In your navigation view activity, set the adapter for the expandable list view.
NavigationViewActivity.java
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
ExpandableListAdapter mMenuAdapter;
ExpandableListView expandableList;
List<ExpandedMenuModel> listDataHeader;
HashMap<ExpandedMenuModel, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
final ActionBar ab = getSupportActionBar();
/* to set the menu icon image*/
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expandableList= (ExpandableListView) findViewById(R.id.navigationmenu);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
prepareListData();
mMenuAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild, expandableList);
// setting list adapter
expandableList.setAdapter(mMenuAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding data header
listDataHeader.add("heading1");
listDataHeader.add("heading2");
listDataHeader.add("heading3");
// Adding child data
List<String> heading1= new ArrayList<String>();
heading1.add("Submenu of item 1");
List<String> heading2= new ArrayList<String>();
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
heading2.add("Submenu of item 2");
listDataChild.put(listDataHeader.get(0), heading1);// Header, Child data
listDataChild.put(listDataHeader.get(1), heading2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
return true;
}
});
}
#Override
public void onFragmentInteraction(Boolean isDataSaved) {
}
}
The adapter for expandable list view is as follows
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mListDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<String> listDataHeader,HashMap<String, List<String>> listChildData,ExpandableListView mView)
{
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList=mView;
}
#Override
public int getGroupCount() {
int i= mListDataHeader.size();
Log.d("GROUPCOUNT",String.valueOf(i));
return this.mListDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
int childCount=0;
if(groupPosition!=2)
{
childCount=this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.size();
}
return childCount;
}
#Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
Log.d("CHILD",mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition).toString());
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.listheader, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.submenu);
ImageView headerIcon= (ImageView)convertView.findViewById(R.id.iconimage);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getIconName());
headerIcon.setImageDrawable(headerTitle.getIconImg());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_submenu, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.submenu);
txtListChild.setText(childText);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
list_submenu.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:id="#+id/submenu"/>
</LinearLayout>
listheader.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:id="#+id/iconimage"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:id="#+id/submenu"/>
</LinearLayout>
</LinearLayout>
I have posted whole code for clarity. Hope this helps.......
You can create it using a ExpandableListview.
look at this
I prefer you should go with custom layout in side navigation drawer.
Here is sample link - how to implement custom layout in navigation drawer.
You also need to implement expandable listview in side navigation drawer for your scenario, here is sample of expandable list view.
You need to combine both things to achive navigation drawer with expandable list.
how can i add an image header to my navigation drawer layout like this one
and this is my code :
MainActivity.java
package com.webileapps.navdrawer;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.webileapps.navdrawer.House_Fragment.Garage_Fragment;
import com.webileapps.navdrawer.House_Fragment.Hall_Fragment;
import com.webileapps.navdrawer.House_Fragment.Kitchen_Fragment;
import com.webileapps.navdrawer.menu.Settings.AccountPreference;
import com.webileapps.navdrawer.menu.Settings.DialogChangeAccount;
import com.webileapps.navdrawer.menu.Settings.DialogChangeCity;
import com.webileapps.navdrawer.menu.Settings.DialogSettingsApp;
import com.webileapps.navdrawer.menu.Settings.DialogSupport;
import com.webileapps.navdrawer.menu.Settings.CityPreference;
import com.webileapps.navdrawer.menu.Settings.DialogueConnexion;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends SherlockFragmentActivity
implements DialogChangeCity.dialogDoneListenerCity,
DialogChangeAccount.dialogDoneListenerAccount,
DialogSettingsApp.dialogDoneListenerSett,
DialogSupport.dialogDoneListenerSupp,
DialogueConnexion.dialogDoneListenerConn {
DrawerLayout mDrawerLayout;
DrawerLayout mDrawerLayoutR;
ExpandableListView mDrawerList;
ExpandableListView mDrawerListR;
ActionBarDrawerToggle mDrawerToggle;
ActionBarDrawerToggle mDrawerToggleR;
private CharSequence mDrawerTitle;
private CharSequence mDrawerTitleR;
private CharSequence mTitle;
private CharSequence mTitleR;
List<Model> listDataHeader;
List<Model> listDataHeaderR;
HashMap<Model, List<Model>> listDataChild;
ExpandableListAdapter listAdapter;
ExpandableListAdapter listAdapterR;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mTitleR = mDrawerTitleR = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
mDrawerListR = (ExpandableListView) findViewById(R.id.right_drawer);
prepareListData();
prepareListDataRight();
// set a custom shadow that overlays
the main content when the drawer opens
listAdapter = new ExpandableListAdapter(this,
listDataHeader, listDataChild);
listAdapterR = new ExpandableListAdapter(this, listDataHeaderR,null);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// mDrawerLayout.setDrawerShadow(R.drawable.up_24,GravityCompat.END);
// set up the drawer's list view with items and click listener
/* mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mRoomTitles));*/
mDrawerList.setAdapter(listAdapter);
mDrawerListR.setAdapter(listAdapterR);
//mDrawerList.setOnItemClickListener(
new DrawerItemClickListener());
mDrawerListR.setOnGroupClickListener(new ExpandableListView.
OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandable
ListView,
View view, int i, long l) {
switch(i){
case 0:
DialogSettingsApp dialogSettingsApp =
new DialogSettingsApp();
dialogSettingsApp.
show(getFragmentManager(),"diagSett");
break;
case 1:
DialogChangeAccount dialogChangeAccount =
new DialogChangeAccount();
dialogChangeAccount.
show(getFragmentManager(),"diagA");
break;
case 2:
DialogChangeCity dialogChangeCity =
new DialogChangeCity();
dialogChangeCity.show(getFragmentManager(),"diagC");
break;
case 3:
DialogSupport dialogSupport = new DialogSupport();
dialogSupport.show(getFragmentManager(),"diagSupp");
break;
case 4:
DialogueConnexion dialogueConnexion =
new DialogueConnexion();
dialogueConnexion.
show(getFragmentManager(),"diagConn");
break;
}
return false;
}
});
mDrawerList.setOnGroupClickListener(new ExpandableListView.
OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Fragment frg = null;
switch (groupPosition) {
case 0:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content, PageSlidingHomeFragment.newInstance()
, PageSlidingHomeFragment.TAG).commit();
mDrawerList.setItemChecked(groupPosition, true);
mDrawerList.setSelection(groupPosition);
mDrawerLayout.closeDrawer(mDrawerList);
break;
case 1:
break;
case 2:
frg = new Kitchen_Fragment();
break;
case 3:
frg = new Garage_Fragment();
break;
case 4:
frg = new Hall_Fragment();
break;
}
if (frg != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, frg).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(groupPosition, true);
mDrawerList.setSelection(groupPosition);
mDrawerLayout.closeDrawer(mDrawerList);
}
return false;
}
});
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggleR=new ActionBarDrawerToggle(this,
mDrawerLayout,
R.drawable.down_24,
R.string.drawer_open,
R.string.drawer_close
){
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitleR);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /*
nav drawer image to replace 'Up' caret */
R.string.drawer_open, /*
"open drawer" description for accessibility */
R.string.drawer_close /*
"close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerListener(mDrawerToggleR);
if (savedInstanceState == null) {
selectItem(0);
}
}
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu){
com.actionbarsherlock.view.MenuInflater
inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(
com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
mDrawerLayout.closeDrawer(mDrawerListR);
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
}
case R.id.action_contact:{
mDrawerLayout.closeDrawer(mDrawerList);
if (mDrawerLayout.isDrawerOpen(mDrawerListR)) {
mDrawerLayout.closeDrawer(mDrawerListR);
} else {
mDrawerLayout.openDrawer(mDrawerListR);
}
break;
}
}
return super.onOptionsItemSelected(item);
}
public void showInputDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}
public void changeCity(String city) {
new CityPreference(this).setCity(city);
}
// The click listener for ListView in the navigation drawer
/*private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view
, int position,long id) {
selectItem(position);
}
}
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void selectItem(int position) {
switch (position) {
case 0:
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content,
PageSlidingHomeFragment.newInstance(),
PageSlidingHomeFragment.TAG).commit();
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
private void prepareListDataRight(){
listDataHeaderR = new ArrayList<Model>();
listDataHeaderR.add(new Model(R.drawable.settings_24,
"App Settings"));
listDataHeaderR.add(new Model(R.drawable.edit_user_24,
"Update Account"));
listDataHeaderR.add(new Model(R.drawable.edit_24, "Change City"));
listDataHeaderR.add(new Model(R.drawable.quest_24, "Support"));
listDataHeaderR.add(new Model(R.drawable.
ic_action_user, "Disconnection"));
}
private void prepareListData() {
listDataHeader = new ArrayList<Model>();
listDataChild = new HashMap<Model, List<Model>>();
// Adding child data
listDataHeader.add(new Model(R.drawable.home24, "Home"));
listDataHeader.add(new Model("Rooms"));
listDataHeader.add(new Model(R.drawable.kitchen_24, "Kitchen"));
listDataHeader.add(new Model(R.drawable.garage_24, "Garage"));
listDataHeader.add(new Model(R.drawable.room_24, "Hall"));
// Adding child data
List<Model> home = new ArrayList<Model>();
home.add(new Model(R.drawable.settings_24, "Settings"));
home.add(new Model(R.drawable.devices_24, "Devices"));
home.add(new Model(R.drawable.weather_24, "Weather"));
List<Model> rooms = new ArrayList<Model>();
rooms.add(new Model(R.drawable.bed_c_24, "1sr Child room"));
rooms.add(new Model(R.drawable.bed_c_24, "2sd Child room"));
rooms.add(new Model(R.drawable.bedroom_24, "Parents room"));
rooms.add(new Model(R.drawable.hotel_24, "Guests room"));
rooms.add(new Model(R.drawable.dining_room_24, "Dining room"));
rooms.add(new Model(R.drawable.livingroom_24, "Living room"));
rooms.add(new Model(R.drawable.office_24, "Office"));
rooms.add(new Model(R.drawable.bathroom_24, "Bathroom 1st floor"));
rooms.add(new Model(R.drawable.bathroom_24, "Bathroom 2nd floor"));
// Header, Child data
listDataChild.put(listDataHeader.get(1), rooms);
}
#Override
public void onDone(String text, boolean state) {
if (state) {
changeCity(text);
//this.recreate();
restartSelf();
}
}
private void restartSelf() {
AlarmManager am = (AlarmManager) gettSystemService(Context.
ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,Calendar.getInstance()
.getTimeInMillis()
+ 1000, // one second
PendingIntent.getActivity(this, 0, getIntent(),
PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT));
finish();
}
#Override
public void onDoneA(String login, String opwd, String npwd) {
if(login!="" && opwd!="" && npwd!=""){
changeAccount(login,opwd,npwd);
}
}
private void changeAccount(String login, String opwd, String npwd) {
new AccountPreference(this).setAccountL(login);
new AccountPreference(this).setAccountOP(opwd);
new AccountPreference(this).setAccountNP(npwd);
}
#Override
public void onDone() {
int x = 0;
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas
.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
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:layout_width="match_parent" android:id="#+id/content"
android:layout_height="match_parent" >
</RelativeLayout>
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#000"
android:choiceMode="singleChoice"
android:divider="#color/abs__bright_foreground_disabled_holo_dark"
android:dividerHeight="2dp" />
<ExpandableListView
android:id="#+id/right_drawer"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_gravity="end"
android:background="#000"
android:choiceMode="singleChoice"
android:divider="#color/abs__bright_foreground_disabled_holo_dark"
android:dividerHeight="2dp" />
</android.support.v4.widget.DrawerLayout>
ExpandableListAdapter.java
package com.webileapps.navdrawer;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<Model> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<Model, List<Model>> _listDataChild;
public ExpandableListAdapter(Context context, List<Model> listDataHeader,
HashMap<Model, List<Model>> 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 Model childText = (Model) 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);
ImageView IconListChild = (ImageView) convertView.findViewById
(R.id.item_icon_child);
IconListChild.setImageResource(childText.getIcon());
txtListChild.setText(childText.getTitle());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
int i=0;
switch (groupPosition) {
case 1:
i= this._listDataChild.get
(this._listDataHeader.get(groupPosition)).size();
break;
default:
i=0;
break;
}
return i;
}
#Override
public Model 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) {
Model headerTitle = (Model) 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);
ImageView IconListHeader = (ImageView) convertView.findViewById
(R.id.item_icon_group);
IconListHeader.setImageResource(headerTitle.getIcon());
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getTitle());
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
In the Navigation Drawer layout xml file. Above drawerView and inside your parent layout add an image view as shown below:
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="140dp"
android:src="respective_source_of_image" />
This should be sufficient. Hope this helps.
Use Recycler View to create a navigation drawer. Recycler view is used most now. In your navigation drawer fragment have your layout file as shown below. This make you achieve your objective:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ms.t.tms.NavigationDrawerFragment">
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="140dp"
android:src="respective_image_src" />
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/image"
android:background="#color/colorPrimary"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
You can simply do it following this sample :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- insert you main view childs here -->
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:layout_width="240dp"
android:layout_gravity="end"
android:background="#color/colorPrimary"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="240dp"
android:layout_height="140dp"
android:src="#drawable/logo" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:background="#color/colorPrimary"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
You will have your image added before the listview. You can manage your view like any other view.
Hope it will help you ;)
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" />
In my application, I have a list and a button, in my list i want when i select parent item all of the child-item be selected indeed I want a Check-box tree. I use an Expandable List View but i have a problem in my code:
My code is:
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">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="30dp"
android:layout_marginRight="2dp"
android:focusable="false"/>
<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="#ffffff" />
</LinearLayout>
list_item2.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"
android:background="#ffffff">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="30dp"
android:layout_marginRight="2dp"
android:focusable="false"/>
<TextView
android:id="#+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="-40dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/>
</LinearLayout>
backup_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ExpandableListView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/expandableListView"/>
<Button
android:id="#+id/backup"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="BackUp"
android:clickable="true" />
</LinearLayout>
How can i fix it?
ExpandableListAdapter:
package com.mCloud.android.ui.activity;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.mCloud.android.R;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
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 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_item2, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
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;
}
}
ParentActivity:
package com.mCloud.android.ui.activity;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ExpandableListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.mCloud.android.Log_OC;
import com.mCloud.android.R;
import com.mCloud.android.filters.MediaFilenameFilter;
public class ParentActivity extends SherlockFragmentActivity {
private static final String TAG = "mCloudPreferences";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.backup_list);
ActionBar actionBar = getSherlock().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
super.onMenuItemSelected(featureId, item);
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
intent = new Intent(getBaseContext(), DisplayInfoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
default:
Log_OC.w(TAG, "Unknown menu item triggered");
return false;
}
return true;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataHeader.add("Files");
listDataHeader.add("Apps");
listDataHeader.add("SMS");
listDataHeader.add("Contacts");
listDataHeader.add("Call Logs");
listDataHeader.add("Calender");
listDataHeader.add("Setting");
List<String> files = new ArrayList<String>();
File file = Environment.getExternalStorageDirectory();
String[] list = file.list(new MediaFilenameFilter());
if (list != null) {
for (String name : list)
files.add(name);
}
files.add("Advance Selection");
List<String> apps = new ArrayList<String>();
apps.add("All Apps");
List<String> sms = new ArrayList<String>();
sms.add("Inbox");
sms.add("Draft");
sms.add("Send");
List<String> contacts = new ArrayList<String>();
contacts.add("All Contacts");
List<String> calllogs = new ArrayList<String>();
calllogs.add("Resived");
calllogs.add("Missed");
calllogs.add("Dailed");
List<String> calender = new ArrayList<String>();
calender.add("All calender");
List<String> setting = new ArrayList<String>();
setting.add("All setting");
listDataChild = new HashMap<String, List<String>>();
listDataChild.put(listDataHeader.get(0), files);
listDataChild.put(listDataHeader.get(1), apps);
listDataChild.put(listDataHeader.get(2), sms);
listDataChild.put(listDataHeader.get(3), contacts);
listDataChild.put(listDataHeader.get(4), calllogs);
listDataChild.put(listDataHeader.get(5), calender);
listDataChild.put(listDataHeader.get(6), setting);
}
}
but you do understand that ExpandableListView work like what I asked you..?
when you press on parent the parent expands (unless you state otherwise) and when you clikc on it again the parent collapses..
if you want to be able to expand the parent and also be able to check and uncheck the parent checkbox you need to give two views in the same row of the parent, the first is the checkbox and the second is the parent title (which can be empty like "")
they should be side by side.
you can do somthing like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Rl11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/desserts_main_expand"
android:orientation="horizontal" >
<ImageView
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:cropToPadding="true"
android:padding="4dp"
android:scaleType="fitXY"
android:src="#android:drawable/ic_menu_add" />
<TextView
android:id="#+id/ParentTextI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/addIng"
android:layout_toRightOf="#+id/Side_image"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:text="dummy text"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold|italic" />
</RelativeLayout>
and then in the adapter:
declare a global variable
CheckBox checkbox;
**this is only for the parent checkbox part
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(checkbox.isChecked()=true)
{checkbox.setChecked(false);}
else checkbox.setChecked(true);
ParentActivity.checkall();
}
});
and then in ParentActivity:
add a function that checks every child of the same parent:
public static void checkall()
{
for(int j=0 ; j<files.size() ; j++)
{if (listAdapter.checkbox.isChecked()==true)
{files.get(j).setChecked(true)};
}
I have made the code specially for you so there might be some error cause i havnt checked it..
but it supposed to be somthing like this.