I have a navigation drawer prototype. we have heading and subheading in the same. am using menu group to include the titles.
1) just want to add sub title or help text for the navigation drawer.
2) want to customise the icon size to fit the whole heading and sub heading so it looks like the image below.
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:subtitle="View your biorhythm" <!-- This should be sub title. -->
android:title="Home"
/>
Inside in DrawerLayout create ListView using below code
<!-- List of Actions (pages) -->
<ListView
android:id="#+id/navList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="#+id/profileBox"
android:choiceMode="singleChoice"
android:background="#ffffffff" />
Now Create one Class that contain property for Title, SubTitle and Icon like below.
class NavItem {
String mTitle;
String mSubtitle;
int mIcon;
public NavItem(String title, String subtitle, int icon) {
mTitle = title;
mSubtitle = subtitle;
mIcon = icon;
}
}
Now Create Adapter for that
class DrawerListAdapter extends BaseAdapter {
Context mContext;
ArrayList<NavItem> mNavItems;
public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
mContext = context;
mNavItems = navItems;
}
#Override
public int getCount() {
return mNavItems.size();
}
#Override
public Object getItem(int position) {
return mNavItems.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.drawer_item, null);
}
else {
view = convertView;
}
TextView titleView = (TextView) view.findViewById(R.id.title);
TextView subtitleView = (TextView) view.findViewById(R.id.subTitle);
ImageView iconView = (ImageView) view.findViewById(R.id.icon);
titleView.setText( mNavItems.get(position).mTitle );
subtitleView.setText( mNavItems.get(position).mSubtitle );
iconView.setImageResource(mNavItems.get(position).mIcon);
return view;
}
}
Create Listview item xml to get your format
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<ImageView
android:id="#+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/ic_action_home"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center_vertical"
android:textColor="#000"
android:text="Line 1"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/icon"
android:layout_toEndOf="#+id/icon" />
<TextView android:id="#+id/subTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 2"
android:layout_toRightOf="#+id/icon"
android:layout_below="#+id/title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
and Finally Add into MainActivity.java file like below way.
private static String TAG = MainActivity.class.getSimpleName();
ListView mDrawerList;
RelativeLayout mDrawerPane;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavItems.add(new NavItem("Home", "Meetup destination", R.drawable.ic_action_home));
mNavItems.add(new NavItem("Preferences", "Change your preferences", R.drawable.ic_action_settings));
mNavItems.add(new NavItem("About", "Get to know about us", R.drawable.ic_action_about));
// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// Populate the Navigtion Drawer with options
mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
mDrawerList = (ListView) findViewById(R.id.navList);
DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);
// Drawer Item click listeners
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItemFromDrawer(position);
}
});
You will get Below Output :
Related
I want to do something described in this post, but I couldn't get the hide and show on scroll to work. Everything is showing, the drawer menu, the header view and the listview, but when I scroll the listview up and down, the header view stays there, doesn't hide and show as the list view is being scrolled. I've looked up few other posts such as these for a solution, but none of them helped.
Android design library CoordinatorLayout, AppBarLayout and DrawerLayout
DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show image
I have an Activity which contains a drawer menu and a fragment. In the Fragment, there is the CoordinatorLayout and AppBarLayout that I want to do show and hide the header view on list view scrolling.
The Activity layout with the DrawerLayout as the root view containing a FrameLayout for the main content and the RelativeLayout for the drawer menu content.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/content_frame"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"/>
<RelativeLayout
android:id="#+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="280dp"
android:layout_gravity="start"
android:background="#eee">
<RelativeLayout
android:id="#+id/sliding_menu_logo_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee">
<ImageView
android:id="#+id/sliding_menu_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/sliding_menu_logo_container"
android:clipToPadding="true"
android:divider="#null"
android:dividerHeight="0dp"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false"
android:scrollbarStyle="outsideOverlay" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
The Fragment layout where I want to hide the header view when list view is scrolling up and show the header view when the list view is scrolling down.
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:id="#+id/my_appbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#eee"
app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="First Name"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Last Name"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"/>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<ListView
android:id="#+id/rv_numbers"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
The app theme is
Theme.AppCompat.Light.DarkActionBar
The Activity class
public class ScrollingActivity4 extends AppCompatActivity {
protected DrawerLayout drawerLayout;
RelativeLayout leftDrawerView;
protected ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling4);
ScrollingActivity4Fragment scrollingActivity4Fragment = new ScrollingActivity4Fragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, scrollingActivity4Fragment, "tag_scrollingActivity4Fragment")
.addToBackStack(null)
.commit();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);
if (drawerToggle == null) {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
}
public void onDrawerOpened(View drawerView) {
}
public void onDrawerSlide (View drawerView, float slideOffset) {
}
public void onDrawerStateChanged(int newState) {
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
drawerToggle.syncState();
leftDrawerView = (RelativeLayout) findViewById(R.id.left_drawer);
ListView rvNumbers = (ListView) findViewById(R.id.list);
String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, numbers);
rvNumbers.setAdapter(itemArrayAdapter);
}
#Override
public boolean onOptionsItemSelected (MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
public class ItemArrayAdapter extends ArrayAdapter<String> {
String[] itemList;
private int listItemLayout;
public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int pos = position;
String item = getItem(pos);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout, parent, false);
viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.item.setText(item);
return convertView;
}
class ViewHolder {
TextView item;
}
}
}
The Fragment Class
public class ScrollingActivity4Fragment extends Fragment {
LinearLayout llHeader;
ListView rvNumbers;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_scrolling4_fragment, container, false);
llHeader = (LinearLayout) view.findViewById(R.id.ll_header);
rvNumbers = (ListView) view.findViewById(R.id.rv_numbers);
String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.list_item, numbers);
rvNumbers.setAdapter(itemArrayAdapter);
return view;
}
public class ItemArrayAdapter extends ArrayAdapter<String> {
String[] itemList;
private int listItemLayout;
public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int pos = position;
String item = getItem(pos);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout, parent, false);
viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
convertView.setTag(viewHolder); // view lookup cache stored in tag
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.item.setText(item);
return convertView;
}
class ViewHolder {
TextView item;
}
}
}
According to this post: ScrollingViewBehavior for ListView, CoordinatorLayout works only with RecyclerView and NestedScrollView, so I suggest you to change the ListView to RecyclerView
I have a list View in my drawer layout that I want to setlistener for each item, I read a couple of post and setFocusable, setclickiable, setfocusableInTouchMode to false for each element of listView but still doesn't work. Thank you in advanced. Here is My code :
targetItem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#ffffff"
android:focusable="true"
android:clickable="true"
>
<!-- icon -->
<ImageView
android:id="#+id/item_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="18dp"
android:layout_marginTop="25dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<!-- title -->
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/item_icon"
android:textSize="12dp"
android:layout_marginTop="28dp"
android:layout_marginRight="15dp"
android:textColor="#000000"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="#+id/item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/item_icon"
android:textSize="12dp"
android:layout_marginTop="28dp"
android:layout_marginRight="15dp"
android:textColor="#000000"
android:visibility="gone"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"/>
</RelativeLayout>
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context=".MainActivity" >
<!-- The navigation drawer -->
<ListView
android:id="#+id/listview"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#ffffff"
android:choiceMode="singleChoice"
android:clickable="true"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:focusable="true"
android:focusableInTouchMode="true" />
<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=".MainActivity" >
.
.
.
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sidebar();
}
private void sidebar() {
MyAdapter adapter = new MyAdapter(this, generateData());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.listview);
mDrawerList.setAdapter(adapter);
mDrawerList
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parnet,
android.view.View view, int position, long id) {
Toast.makeText(getBaseContext(), "Hello",
Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<Model> generateData() {
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("TimiT "));
models.add(new Model(R.drawable.home,home,1));
models.add(new Model(R.drawable.basket, basket,2));
models.add(new Model(R.drawable.list, list,3));
return models;
}
MyAdapter.java
public class MyAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.target_item, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = null;
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.target_item, parent, false);
rowView.setFocusable(true);
rowView.setClickable(true);
rowView.setFocusableInTouchMode(true);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
TextView idView = (TextView) rowView.findViewById(R.id.item_id);
imgView.setImageResource(modelsArrayList.get(position).getIcon());
imgView.setFocusable(false);
imgView.setClickable(false);
imgView.setFocusableInTouchMode(false);
titleView.setText(modelsArrayList.get(position).getTitle());
titleView.setFocusable(false);
titleView.setFocusableInTouchMode(false);
titleView.setClickable(false);
idView.setText(modelsArrayList.get(position).getID());
idView.setFocusable(false);
idView.setFocusableInTouchMode(false);
idView.setClickable(false);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Hello", Toast.LENGTH_LONG).show();
}
});
}
else{
rowView = inflater.inflate(R.layout.group_header_item, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}
}
Model.java
public class Model {
private int icon;
private String title;
private int id;
private boolean isGroupHeader = false;
public Model(String title) {
this(-1,title,0);
isGroupHeader = true;
}
public Model(int icon, String title,int id) {
super();
this.icon = icon;
this.title = title;
this.id = id;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getID() {
return icon;
}
public void setID(int id) {
this.id = id;
}
public boolean isGroupHeader() {
return isGroupHeader;
}
public void setGroupHeader(boolean isGroupHeader) {
this.isGroupHeader = isGroupHeader;
}
}
the problem is this line rowView.setOnClickListener. Registering the onClickListener is preventing the onItemClik to be fired. Get rid of it, and it will work. If you want to handla an additional click listener on your list item, you have to set the OnClickListener on a child of the convertView, not on the convertView itself and add android:descendantFocusability="blocksDescendants" to the root of your list item xml
Hi guys, I tried to learn how to use custom ListView by this page
It works fine but I have problems with another options.
Cannot set margin to listItems
Cannot set backgro
Cannot set OnItemClickListener
1# Margin
First of all I'm trying to set margin between list items but nothing works. I try to set margin on RelativeLayout in rowlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#drawable/rounded"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
>
2# Background
I want to set background on whole screen under listView. I think I put black color on every View in every xml I found but nothing affect the background. I can change background of single list items, that's all.
android:background="#000000"
3#OnItemClickListener
My last problem is that I'm not able to set onItemClickListener. I have no idea how to set it if I don't have simply ListView but RelativeLayout with TextView etc.
I guess I have to use
setOnClickListener(new OnClickListener()
and after that
public void onClick(View arg0) {
but I have no idea on which View I have to use this method.
Files
MyAdapter.java
public class MyAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.rowlayout, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = null;
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.rowlayout, parent, false);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
TextView counterView = (TextView) rowView.findViewById(R.id.item_counter);
// 4. Set the text for textView
imgView.setImageResource(modelsArrayList.get(position).getIcon());
titleView.setText(modelsArrayList.get(position).getTitle());
counterView.setText(modelsArrayList.get(position).getCounter());
}
else{
//rowView = inflater.inflate(R.layout.group_header, parent, false);
// TextView titleView = (TextView) rowView.findViewById(R.id.header);
// titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}
}
Model.java
package com.example.sunny.katan;
/**
* Created by Sunny on 15.09.14.
*/
public class Model{
private int icon;
private String title;
private String counter;
private boolean isGroupHeader = false;
public Model(String title) {
this(-1,title,null);
isGroupHeader = true;
}
public Model(int icon, String title, String counter) {
super();
this.icon = icon;
this.title = title;
this.counter = counter;
}
public String getCounter() {
return counter;
}
public String getTitle() {
return title;
}
public int getIcon() {
return icon;
}
public boolean isGroupHeader() {
return false;
}
public void setGroupHeader(boolean isGroupHeader) {
this.isGroupHeader = isGroupHeader;
}
//gettters & setters...
}
main.java
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// if extending Activity
//setContentView(R.layout.activity_main);
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
// if extending Activity 2. Get ListView from activity_main.xml
//ListView listView = (ListView) findViewById(R.id.listview);
// 3. setListAdapter
//listView.setAdapter(adapter); if extending Activity
setListAdapter(adapter);
}
private ArrayList<Model> generateData(){
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model(R.raw.barbell,"Cviky","37"));
models.add(new Model(R.raw.stretching,"Protahování","94"));
models.add(new Model(R.raw.work,"Generování workoutu","293"));
return models;
}
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#drawable/rounded"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
>
<!-- icon -->
<ImageView
android:id="#+id/item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:src="#raw/barbell"
/>
<!-- title -->
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/item_icon"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#+id/item_counter"
android:textSize="20dp"
/>
<!-- counter -->
<TextView
android:id="#+id/item_counter"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:background="#drawable/rectangle"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="false"
>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".katan_lobby"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/menu"
android:layout_gravity="center_horizontal"
android:background="#238CD4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/prvniItemSeznamu"
android:background="#238CD4"
style="#style/listView"
/>
</LinearLayout>
</ScrollView>
Looks like you are half way there in your activity.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
MyAdapter adapter = new MyAdapter(this, generateData());
ListView listView = (ListView) findViewById(R.id.menu);
listView.setAdapter(adapter); if extending Activity
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//You now have the position but you will need to get your model list and use .get(position) and cast it to Model to get access to it.
}
});
}
//First define your list view item xml… something like this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/white"
android:layout_marginTop="10dp"
android:padding="10dp" >
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/custom_blue"
android:text="TextView" />
<TextView
android:id="#+id/item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textColor="#color/custom_grey"
android:text="TextView" />
</LinearLayout>
//Then you define your list list adapter.
public class MyListAdapter extends ArrayAdapter<MyModel> {
private Context context;
private ArrayList<MyModel> myModel;
public MyListAdapter(Context context, ArrayList<MyModel> myModelList) {
super(context, R.id.icon, myModelList);
this.context = context;
this.myModelList = myModelList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = null;
View rowView = convertView;
TextView myItemName = (TextView)rowView.findViewById(R.id.item1);
TextView myItemTitle = (TextView)rowView.findViewById(R.id.item2);
//use your models getters/setters
myItemName.setText(myModelList.get(position).getItemTitle());
myItemTitle.setText(myModelList.get(position).getItemName());
return rowView
}
}
//Now use both the list adapter and list item in your ListFragment or ListActivity.
ArrayList<MyModel> myModelList = new ArrayList<MyModel>();
MyListAdapter myApt = new MyListAdapter(this, myModelList);
setListAdapter(myApt);
//For clicking on the items:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// ListView Clicked item value
String itemValue = l.getItemAtPosition(position).toString();
doSomethingWihtMyItemMethod(position);
}
/* You can customize your list item anyway you like, just be aware of screen real estate when doing so.Hope this helps. */
Many answers on SO would seem to suggest that it is not possible to add icons to the overflow menu list, however a number of apps, eg Contacts, (see image) have not only done just that but also have a rather nice border with rounded corners and make it appear by sliding up. How can I do the same? The standard menuinflater only adds icons to the action bar
You need to create a layout for represents each option and put that with a adapater.
I do something like this:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5sp"
android:paddingLeft="5sp"
android:paddingTop="5sp" >
<ImageView
android:id="#+id/dliIVImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:maxHeight="20sp"
android:maxWidth="20sp"
android:src="#drawable/icon" />
<TextView
android:id="#+id/dliLblOption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#fff" />
</LinearLayout>
Adapter:
public class MenuAdapter extends BaseAdapter {
private String[] options;
private LayoutInflater mInflater;
private ViewHolder holder;
public static final Integer[] images = { R.drawable.icon_search,
R.drawable.icon_profile, R.drawable.icon_password, R.drawable.icon_locate, R.drawable.icon_logout};
static class ViewHolder{
private TextView option;
private ImageView img;
}
public MenuAdapter(Context context, String[] options) {
mInflater = LayoutInflater.from(context);
this.options = options;
}
#Override
public int getCount() {
return options.length;
}
#Override
public Object getItem(int index) {
return options[index];
}
#Override
public long getItemId(int index) {
return index;
}
#Override
public View getView(int posicao, View convertView, ViewGroup arg2) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
holder = new ViewHolder();
holder.option = (TextView) convertView.findViewById(R.id.dliLblOption);
holder.img = (ImageView) convertView.findViewById(R.id.dliIVImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.option.setText(options[posicao]);
holder.img.setImageResource(images[posicao]);
return convertView;
}
}
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
opcoes = ["option1", "option2", "option3"];
MenuAdapter mAdapter = new MenuAdapter(getApplicationContext(), opcoes);
menuLateral = (DrawerLayout) findViewById(R.id.drawer_layout);
listaMenuLateral = (ListView) findViewById(R.id.left_drawer);
listaMenuLateral.setAdapter(mAdapter);
}
Main Layout
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
I have a Custom Base Adapter I am using with my ListView in a DrawerLayout. The ListView inflates ok, but I cannot call the onItemClickListener.
Here is the onCreate portion of the MainAcitivty, where I set the adapter and the onItemClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isPhone = getResources().getString(R.string.screen_type).toString().equals("phone");
mTitle = getResources().getString(R.string.app_name);
mCategoryTitles = getResources().getStringArray(R.array.categories);
mSubtext = getResources().getStringArray(R.array.subtext);
mTitle = mDrawerTitle = getTitle();
// Set the drawer toggle as the DrawerListener
mDrawerList = (ListView) findViewById(R.id.left_drawer);
BaseAdapter adapter = new NavigationListAdapter(this, mCategoryTitles, mSubtext);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
.....
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
//selectItem(position);
System.out.println("This is clicked");
}
}
Here is my Base Adapter Code
public class NavigationListAdapter extends BaseAdapter {
public static final int HDR_POS1 = 0;
public static final int HDR_POS2 = 9;
private static final Integer LIST_HEADER = 0;
private static final Integer LIST_ITEM = 1;
Context mContext;
String[] mCategories;
String[] mSubtext;
private SideBarPositionClickedCommunicator mCallback;
public interface SideBarPositionClickedCommunicator{
public void setListItemPosition(int itemInt);
}
public NavigationListAdapter(Context context, String[] categories, String[] subtext)
{
mContext = context;
mSubtext = subtext;
mCategories = categories;
}
#Override
public int getCount() {
return mCategories.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int positionClicked = position;
String headerText = getHeader(position);
if(headerText != null) {
View item = convertView;
if(convertView == null || convertView.getTag() == LIST_ITEM) {
item = LayoutInflater.from(mContext).inflate(
R.layout.lv_header_layout, parent, false);
item.setTag(LIST_HEADER);
}
TextView headerTextView = (TextView)item.findViewById(R.id.lv_list_hdr);
headerTextView.setText(headerText);
return item;
}
View item = convertView;
if(convertView == null || convertView.getTag() == LIST_HEADER) {
item = LayoutInflater.from(mContext).inflate(
R.layout.lv_layout, parent, false);
item.setTag(LIST_ITEM);
}
TextView header = (TextView)item.findViewById(R.id.lv_item_header);
header.setText(mCategories[position % mCategories.length]);
TextView subtext = (TextView)item.findViewById(R.id.lv_item_subtext);
subtext.setText(mSubtext[position % mCategories.length]);
//Set last divider in a sublist invisible
View divider = item.findViewById(R.id.item_separator);
if(position == HDR_POS2 -1) {
divider.setVisibility(View.INVISIBLE);
}
return item;
}
private String getHeader(int position) {
if(position == HDR_POS1 || position == HDR_POS2) {
return mCategories[position].toUpperCase();
}
return null;
}
#Override
public boolean isEnabled(int position) {
if(position == HDR_POS1 || position == HDR_POS2)
return false;
else
return true;
}
}
Here are my XML Layouts.
Header Layout:
<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="#dimen/lvHdrItemHeight"
>
<View
android:id="#+id/item_separator"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="#dimen/lvDividerHeight"
android:background="#color/dark_gray"
android:layout_marginTop="#dimen/lvSectionDividerMarginTop"
/>
<TextView
android:text="This is a text"
android:id="#+id/lv_list_hdr"
android:textColor="#color/dark_gray"
android:gravity="bottom|left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="#id/item_separator"
android:layout_alignParentLeft="true"
style="#style/listViewHeaderItem"
/>
</RelativeLayout>
Each line item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
tools:context=".MainActivity"
style="#style/listViewItem"
android:background="#android:drawable/list_selector_background"
>
<View
android:id="#+id/item_separator"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="#dimen/lvDividerHeight"
android:background="#color/lvDividerColor"/>
<TextView
android:id="#+id/lv_item_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
style="#style/listViewPrimaryDetail"
android:fontFamily="sans-serif-light"
android:ellipsize="marquee"
android:singleLine="true"
android:text="This is a test"
android:layout_alignParentLeft="true"
/>
<TextView
android:text="Of the Emergency Broadcast"
android:id="#+id/lv_item_subtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lv_item_header"
style="#style/listViewSecondaryDetail"
android:layout_above="#id/item_separator"
android:layout_alignParentLeft="true"
android:ellipsize="marquee"
android:singleLine="true"
/>
</RelativeLayout>
Finally, here is the 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="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" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#null"
android:dividerHeight="1dp"
android:background="#EEEEEE"/>
</android.support.v4.widget.DrawerLayout>
Here is the result of the layout inflation:
http://imgur.com/yWPp9ec
Please let me know if you have any advice.
I found the issue. It was in the style portion of my listViewItem.
<style name="listViewItem">
<item name="android:layout_height">#dimen/lvItemHeight</item>
<item name="android:clickable">true</item>
</style>
Changing android:clickable to false fixed the issue.