So I've searched allot about navigation drawers here, and when I was pointed to a tutorial from an answer in another persons question. I did so.
I successfully managed to create and style the nav drawer to my liking.
But now I've been searching tirelessly on how I can launcher activities from the navigation drawer. I've managed to get some code into the the MainActivity but upon clicking the item it does not launch anything? All activities are define in the Manifest. I decided to use Toasts as a trail and error, still no luck.
here's my code for nav drawer, and launch activity.
// Drawer Activity
// Get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// Get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// Run Activity from drawer
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
And this is my DrawerItemClickListener method
private class DrawerItemClickListener implements ListView.OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
Intent a = new Intent(this, AppInfo.class);
startActivity(a);
break;
case 1:
Intent b = new Intent(getBaseContext(), WelcomeActivity.class);
startActivity(b);
}
}
}
Repalce this with MainActivity.this like that:
Intent a = new Intent(MainActivity.this, AppInfo.class);
startActivity(a);
Also Change that
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
replace
drawerListView.setOnItemClickListener(this);
Check there for Custom Adapter
Intent abc = new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(abc);
This is how I've been doing it, directly referencing each activities name.
Related
So, I have 3 activities that I want to link with a Navigation drawer but I'm exactly sure how to do that. I saw somewhere that I should make a new class for the Navigation Drawer methods or something like that but I didn't really understand. So, what would be a good way to do this?
By the way, I'm very new to android development...
It is simple, if you check the navigation drawer of Google examples, they load a fragment when you click in an item.
Just change it, and use for each item:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
Change the activities names for each one of your three activities.
Here an example of how to do it:
Navigation Drawer
In this part you have to change the code that I mentioned before:
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case 1:
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(MainActivity.this,ForthActivity.class);
startActivity(intent);
break;
default:
break;
}
As far as I know, you should make a new activity in which the navigation-drawer is used, and then convert the 3 activities to fragments. In this way, you can navigation between this 3 fragments, This is the recommended pattern to navigate between some Top level "View".
I am trying out the navigation drawer (slide menu) given in this tutorial.
The difference with above link and mine is that instead of calling fragments I am trying to call the activity. When the app opens I am not able to see the Navigation drawer menu I can see only the action bar with HOME activity opened.
Here is the code that I changed: (Is it necessary to have a fragment or can I use activity for my first screen in Navigation Drawer?)
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.drawer,
R.string.drawer_open,
R.string.drawer_close
)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
displayView(position);
}
}
private void displayView(int position)
{
switch (position)
{
case 0:
//fragment = new HomeFragment();
Intent intent = new Intent(this, Home.class);
startActivity(intent);
return;
case 1:
//fragment = new FindPeopleFragment();
Intent intent1 = new Intent(this, Profile.class);
startActivity(intent1);
break;
case 2:
//fragment = new PhotosFragment();
Intent intent2 = new Intent(this, Details.class);
startActivity(intent2);
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
How do I fix this to show Navigation drawer on my Home Activity?
Update:
I even tried the following option given in this link:
How can I call one of my activity using navigation drawer ? but I am still not getting the navigation slide menu.
You can't do that...
Navigation drawer is a layout from an activity and you cant show an activity inside another activity, you need to use a fragments for this!
An activity is a screen, you cant show a screen inside another screen, but a fragment may be a component of a screen, and you can inflate a fragment inside a container of an activity and then show to the user.
if you wan't do this, you can create a abstract activity and inherit, but you will not have a a activity in a fragment, you will have a multiple activities with each one with your own navigation drawer.
If you look at the sample documentation for Android Navigation drawer, it is explicitely defined to use fragments instead of activities to load different 'pages' from the navigation bar. As the app lifecycle for Android goes, fragments are easier to load and produce less impact on the Android system. they can also be easily set into the background and be replaced by other different fragments.
Having said that, your best approach would be to convert your activities into fragments and use them to load the different pages you need in your app. It is not such a difficult task, and I will show you how:
Change all activities to fragments
public class nameOfFragment extends Fragment { }
Use the onCreateView method instead of onCreate
public View onCreateView() {
View view = inflater.inflate(R.layout.activity_main, container, false);
//any other elements in that view need to be included here in this manner.
Button rate = (Button) rootView.findViewById(R.id.rate);
return view;
}
These changes should be enough to change your activities into fragments.
You need to do the same for all fragments which are accessed through your navigation bar.
Please post your code for the activities you are using if you need any further help.
Hope this helps :)
You've got the code to build and display a navigation bar right? Put your code above into a base activity class. Every activity that you want to access the navigation bar from should then inherit from that base class.
Google's IO 2014 app does the same thing, take a look at their source here.
When you do this, you will need to find a way to maintain the drawer's state as you transition to the next activity. I'm not sure how to do that, but you'll probably find the answer in the source code for the IO app.
If you have to have single instance of drawer, then you have to go by fragments.
If you have no choice to go other than with activities, define a base class for all your activites(say BaseActivity). And in BaseActivity - you can do the necessary coding to integrate drawer. Each activity extending BaseActivity will now show up drawer menu (but the instances will be different)
Now you need to use the toolbar. Android has made appcompat library to introduce material design in older versions of android as well. You need to use compile dependency of appcompat.
Change the style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
Then we will choose to have no old action bar. After then we make a contract between navigation drawer and toolbar. So we need not to use old actionbar now.
You can refer the code below:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6d7fe2"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
></android.support.v7.widget.Toolbar>
What i have is left navigation menu that i have made using navigation drawer and i want it's button to open activities instead of fragments .. but the weird thing is that when the main activity opens which has the menu it open the second activity direct without displaying the main activity .. here is my code :
private void displayView(int position) {
Intent intent = null;
switch (position) {
case 0:
intent = new Intent(MainActivity.this, Gallery.class);
break;
....
if(intent != null) {
mDrawerLayout.closeDrawer(mDrawerList);
setTitle(navMenuTitles[position]);
startActivity(intent);
}
else {
// error in creating activity
Log.e("MainActivity", "Error in creating Activity");
}
}
And i call it from here :
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
So why this is happening? can anyone help me?
In generally, the navigation drawer is initialized with some code when the activity starts(to show one of the items/do something). If, when you start the activity holding this navigation drawer, you immediately go the one of the activities pointed by the navigation drawer items then you need to check your code to see if you're not by any chance calling the displayView() method when the activity starts(which will start the other activity).
Have you implemented a ListView inside of your Navigation Drawer? I had the same problem and it turns out that when I was setting my adapter for the ListView I was using
divListView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item__activated_1,
android.R.id.text1,
divisionAda));
The ListView was "activated" making it gain focus and displaying it on top of the activity. I remedied the problem by removing the "activated".
So now my setting my adapter looks like this:
divListView.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
divisionAda));
I know this has been asked here but the answers were quite confusing. I have 3 items in my ListView. They are "Aluminium", "Gold" and "Zinc". Through each one of them, I want to start different activities and for that I have created the 3 activities which i named "Aluminium.java","Gold.java" and "Zinc.java"
I have used this ListView in a drawer layout for the navigation drawer. I implemented navigation drawers through the code given below which i got from a site.This code changes fragments and its not working properly. Instead of fragments, I want to switch activities.
I want to achieve 3 things:
Switch between activities through the listview in the navigation drawer.
To achieve point 1, I want to get the clicked list item and then use intents.
I want all the 3 activities to have this navigation drawer.
Sorry if its too dumb but I am a beginner. Please help me out with the code.
Java code
public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};
final String[] fragments ={
"com.Chinmay.navigationdrawer.Gold",
"com.Chinmay.navigationdrawer.Aluminium",
"com.Chinmay.navigationdrawer.Zinc"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
android.support.v4.app.FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
});
}
}
Make a base activity class and put all your drawer code there, and extend this base class for your 3 activity, in that way, you'll have drawer for your all activities.
class Gold extends BaseActivity{
}
For the clicking part, you already set an item click listener, just make a switch case such as
switch (pos){
case 0:
Intent i = new Intent(this,Gold.java);
startActivity(i);
break;
}
// fill the rest
}
I am using the sample code from the developer.android.com for navigation drawer. I am unable to know what exactly needs to be changed to start activities instead of images which now appear. So what parts do I need to delete so that I can make listviewitemclick to open activities?
I am working here
private void selectItem(int position) {
switch(position){
case 0:
Intent a = new Intent(MainActivity.this, sampleopen.class);
startActivity(a);
}
}
The problem is that it opens the second activity first then when we press back it goes to first activity and there the drawer is implemented
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
Here you define a listener - you need to provide your own instead of new DrawerItemClickListener() and there you'll be able to launch activities according to position received.
// set the on item click listener for the listview object
mNavigationListView.setOnItemClickListener(mOnNavigationItemClickListener);
// handle clicks here
private AdapterView.OnItemClickListener mOnNavigationItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) then launch ACtivity #1
//....
}
};