Reputation:
I made a drawer navigation based on a tutorial on the internet and I followed everything but when I open my app everything is working correctly except my onclick event. I'm pretty new to android (2 weeks) and tried to figure it out by myself but it didn't work out. I tries onclicklistener but that one didn't give any possitive feedback for me.
How do I make a click event that will take me to another activity?
my code:
public class LayoutOneActivity extends ActionBarActivity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
Resources res = getResources();
String [] menu_items = res.getStringArray(R.array.menu_items); // String array where the menu items will be stored
menu = menu_items; // Variable for the menu items
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Looking for the id "drawer_layout" and apply as layout
dList = (ListView) findViewById(R.id.left_drawer); // Looking for the listview where the items will be stored
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);// Making a new adapter
dList.setAdapter(adapter);// Give the list-layout the variable "adapter" which is an adapter (obviously)
dList.setSelector(R.drawable.back);// Sets the colour of the list-layout
dList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
dLayout.closeDrawers();// The layout will be clossed when clicked outside the layout
Bundle args = new Bundle();// New bundle which will parse the data between various activities
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
}
layout_one
<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">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity1"
android:text="@string/clickActivity1" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity2"
android:text="@string/clickActivity2" />
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity3"
android:text="@string/clickActivity3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity4"
android:text="@string/clickActivity4"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity5"
android:text="@string/clickActivity5"/>
<Button
android:layout_width="80dp"
android:layout_height="85dp"
android:background="@drawable/button"
android:onClick="openNewActivity6"
android:text="@string/clickActivity6"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </FrameLayout>
<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="#fff"/> </android.support.v4.widget.DrawerLayout>
menu_detail_fragment
<?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:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#ffffff"
android:layout_gravity="center"
android:id="@+id/detail"/> </LinearLayout>
Upvotes: 1
Views: 2200
Reputation: 423
To set a navigation menu onClickListener you have to implement the NavigationView.OnNavigationItemSelectedListener interface on the Activity.
On the onCreate method you have to define the navigation onClickListener to the context Activity like this:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Then you have to Override the method
@Override
public boolean onNavigationItemSelected(MenuItem item)
I write an example of how to implement the method below:
@Override
public boolean onNavigationItemSelected(MenuItem item){
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Urban");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_gallery) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Cosmos");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_slideshow) {
EventFragment eventFragment = new EventFragment();
Bundle bundle = new Bundle();
bundle.putString("selectedEvent", "Sink");
eventFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame,eventFragment).commit();
} else if (id == R.id.nav_share) {
ShopListFragment intent = new ShopListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, intent).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(START);
return true;
}
Upvotes: 1