Make clickable item of listview in drawer navigation
-
I have used the below code for my navigation drawer which is used and included in several activities by including the layout and create object and calling the function. I want to add functionality to it, when one item clicked, it starts new activity. <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawerLayout"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawerFrame"> </FrameLayout> <ListView android:layout_width="240dp" android:background="#eaeaea" android:divider="@null" android:layout_height="match_parent" android:entries="@array/drawerList" android:id="@+id/drawerlist" android:layout_gravity="right" /> </android.support.v4.widget.DrawerLayout> public class DrawerNav { private Activity drawerAct; private ListView listView; private String[] drawerArray; private ActionBarDrawerToggle drawerListener; private DrawerLayout drawerLayout; public DrawerNav(Activity activity){ drawerAct = activity; } public void drawerAction() { final ImageView setting = (ImageView) drawerAct.findViewById(R.id.setting); final ImageView sepTitle = (ImageView) drawerAct.findViewById(R.id.seperatorTitle); //drawer navigation drawerLayout = (DrawerLayout) drawerAct.findViewById(R.id.drawerLayout); drawerArray = drawerAct.getResources().getStringArray(R.array.drawerList); listView = (ListView) drawerAct.findViewById(R.id.drawerlist); listView.setAdapter(new ArrayAdapter<>(this.drawerAct, R.layout.list_item_layout, drawerArray)); drawerListener = new ActionBarDrawerToggle(this.drawerAct, drawerLayout, null, R.string.openD, R.string.closeD) { @Override public void onDrawerSlide(View drawerView, float slideOffset) { setting.setImageResource(R.drawable.settingfocused); sepTitle.setVisibility(View.GONE); } @Override public void onDrawerOpened(View drawerView) { setting.setImageResource(R.drawable.settingfocused); } @Override public void onDrawerClosed(View drawerView) { setting.setImageResource(R.drawable.setting); sepTitle.setVisibility(View.VISIBLE); } }; drawerLayout.setDrawerListener(drawerListener); //drawer navigation } }
-
Answer:
Implement OnItemClick Listener like this for your listView object. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position){ case 0: //your code to call intent break; case 1: //your code to call intent break; //and so on default: break; } } });
Lamiya at Stack Overflow Visit the source
Related Q & A:
- How to make Semicircular horizontal listview in android?Best solution by Stack Overflow
- Why is my navigation drawer empty?Best solution by Stack Overflow
- how to change the position of item inside the listview dynamicly in android?Best solution by Stack Overflow
- How do you make a link into a clickable picture?Best solution by Yahoo! Answers
- How do you make a navigation bar on Myspace?Best solution by myspacegens.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.