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".
Related
I created a Navigation Drawer and inserted the necessary items and their fragments.
Also, I added a item that I have to open another Activity, which code should I put that by pressing on the item do this?
For example, item id nav_menu6 ..... but it should not be linked to a fragment but open an activity ...
private void displaySelectedScreen(int itemId) {
Fragment fragment = null;
// Handle navigation view item clicks here.
switch (itemId) {
case R.id.nav_menu1:
fragment = new BlankFragment();
break;
case R.id.nav_menu2:
fragment = new BlankFragment2();
break;
case R.id.nav_menu3:
fragment = new BlankFragment3();
break;
case R.id.nav_menu4:
fragment = new BlankFragment4();
break;
case R.id.nav_menu5:
fragment = new BlankFragment5();
break;
case R.id.nav_menu6:
fragment = new BlankFragment6();
break;
}
https://developer.android.com/training/basics/firstapp/starting-activity.html
Read this about starting a new activity.
Basically, the key is the intent.
Intent intent = new Intent(this, YourActivityName.class);
startActivity(intent);
or in a shortened form:
startActivity(new Intent(this, YourActivityName.class));
I want to set a default frame layout on fragment,and this frame layout shows at top of the half layout,and remaining half contains list view.frame layout updates the layout item when click on the list item these all work fine.But when I want to set default frame layout when comes from previous activity then it creates problem.Here is how i am opening my fragments and this is my adapter class from this activity. I want to set the default frame layout on next activity containing list.
grid.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
frame=(FrameLayout)grid.findViewById(R.id.fragment_container);
Intent intent = new Intent(context, SingleItemView.class);
intent.putExtra("id", resultp.get(MainActivity.RANK));
intent.putExtra("count", resultp.get(MainActivity.COUNTRY));
intent.putExtra("population",resultp.get(MainActivity.POPULATION));
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
// Start SingleItemView Class
intent.putExtra("mylist", data);
context.startActivity(intent);
if(frame==null){
DefaultLogFrag fragment1 = new DefaultLogFrag();
FragmentManager fragmentManager1 = ((Activity) context).getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.add(R.id.fragment_container, fragment1);
fragmentTransaction1.commit();
}
}
});
Just Open your next activity using
Intent intent = new Intent(context, SingleItemView.class);
And add the Fragment transaction in onCreate() of SingleItemView.java activity
If you want to add different Fragments based on some condition then you can pass conditional variables with intent intent variable extra :
intent.putExtra("whichFragment", /* integer parameter */ 4);
Handle them in onCreate() of SingleItemView.java :
switch(getIntent().getExtraString("whichFragment")) {
case 1: /* add Fragment 1 */
break;
case 2: /* add Fragment 2 */
break;
case 3: /* add Fragment 3 */
break;
case 4: /* add Fragment 4 */
break;
}
Apologies for such a basic question, but how can I add a navigation pane to my app (the one that slides in from the left) and how can I make it launch other Activities with buttons inside the pane?
I recommend a Material Design drawer component, like this: http://mikepenz.github.io/MaterialDrawer/
You can start new Activities using Intents.
If you want to start a new Activity when user clicks a button, you have to create the Intent in the navigation pane's onItemClick event handler:
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem){
if (position==0){ //user clicked first button in pane
Intent intent = new Intent(this, FancyOtherActivity.class);
startActivity(intent);
}
if (position==1){ //user clicked second button
Intent intent = new Intent(this, GreatAnotherActivity.class);
startActivity(intent);
}
... //other buttons
}
I am trying to implement an article viewer, which, at first, will display a list of articles divided by category with the use of sliding tabs and a ViewPager.
I am pulling the articles from a server and am trying to load them to the UI via a ContentProvider and a CursorLoader.
In order to implement the aforementioned structure I followed this which uses tabs with a ViewPager.
Here is the point of interest:
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = getActivity().getLayoutInflater().inflate(R.layout.article_list,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);
switch (position) {
case 0:
Intent intent1 = new Intent(getActivity(), ArticleService.class);
intent1.putExtra(ArticleService.CATEGORY_EXTRA, 1);
getActivity().startService(intent1);
break;
case 1:
Intent intent2 = new Intent(getActivity(), ArticleService.class);
intent2.putExtra(ArticleService.CATEGORY_EXTRA, 2);
getActivity().startService(intent2);
break;
case 2:
Intent intent3 = new Intent(getActivity(), ArticleService.class);
intent3.putExtra(ArticleService.CATEGORY_EXTRA, 3);
getActivity().startService(intent3);
break;
case 3:
Intent intent4 = new Intent(getActivity(), ArticleService.class);
intent4.putExtra(ArticleService.CATEGORY_EXTRA, 4);
getActivity().startService(intent4);
break;
case 4:
Intent intent5 = new Intent(getActivity(), ArticleService.class);
intent5.putExtra(ArticleService.CATEGORY_EXTRA, 5);
getActivity().startService(intent5);
break;
}
// Return the View
return view;
}
As you can see I am creating a different request that corresponds to a different article category and I intend for the resulting content for each tab to be displayed in a ListView. The plan is to set the CursorAdapter to that ListView but I've never worked with sliding tabs before. How will I go about implementing that?
Thanks in advance.
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.