How to effectively use navigation pane - android

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
}

Related

Android Navigate Back to Activity; Don't Reload Parent

I have a scenario where I am clicking on a ListFragment and spinning up a new Activity like below:
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), VenueBeerActivity.class);
Parcelable wrapped = Parcels.wrap(mAdapter.getItem(position));
intent.putExtra("venue", wrapped);
startActivity(intent);
}
This works fine and displays the new activity.
I've modified this activities manifest so it points back to its parent activity (in this case main activity)
However the problem I have is when the back button is pressed, it reloads the entire parent. The parent is a list and I don't want it to reload the users position. How can I prevent this?
As a note. The parent houses a Page Tab Strip.
I'm sure this is a relatively simple fix...
What do you mean by "back button"? Is it the up button in the toolbar? If that's the case, edit the onOptionsItemSelected in your VenueBeerActivity to:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
So when user press the Up button it will get the behavior of the back button in the navigation bar.
In the parent activity, set the android:launchMode attribute to singleTop. This would prevent the system from creating a new instance of parent when up button is pressed.

Using a Navigation Drawer to Switch Activites?

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".

How to switch between layouts in android?

I am making a contact list application, and i have a main screen that helps you save a contact. I have a search button there. Then i created another UI that i call it search screen. When i click on the search button on main screen, i want to be redirected to the other UI, i.e. search screen. I am planning to add setOnClickListener method for search button but how can i switch from main screen to search screen when i click the button?
btn_goSearch.setOnClickListener(new OnClickListener() {
?????????
}
});
Thanks
#Override
public void onClick(View v) {
Intent intent = new Intent(Currentactivity, YourSearcgActivity.class);
intent.putExtra("EXIT", false);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
you should create another Activity, maybe call it SearchActivity, and when the user clicks the button, just start SearchActivity with a simple intent, that will load its own layout and everything:
startActivity(new Intent(context,SearchActivty.class));

Start another Activity inside the FrameLayout of TabActivity

I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here

Android pages Activity or Layout?

My app Consists an intro page with "Start", "About", etc..
I managed to create a functional version of the app by having the "Start" button call a new layout in which a new onclick listener is defined.
This doesn't seem clean to me that I#m defining a new onClick listener for each Layout I use and wonder how the correct way would be to create individual pages (including my "About" and any other screens I implement).
Any advice is appreciated, thanks.
You should create an Activity for each "screen". All the buttons will launch the same event (onClick) and you will start the right activity looking at the ID of the event's source view.
Something like this:
public void onButtonClick(View target) {
System.out.println("Button clicked '" + target + "'");
switch (target.getId()) {
case R.id.a_button:
Intent intent = new Intent(this, AboutActivity.class);
this.startActivity(intent);
break;
}
}

Categories

Resources