Running ActionBarActivity instead of Fragment on DrawerLayout - android

I want to making DrawerLayout for my new android app. But I need to use activity on same activity.
How can I handle it, I have code looks like this :
my_activity.java; (has a lot of code for list of menu, swipe vs, But I just add lines of my problems)
switch (position) {
case 0:
fragment = new HomeFragment();
break;
Here is my problem. I want to run activity file at this step but I cant. I wanna show some activity file case : 0, I tried everything I know But I cant do it.
ACTUALLY I m a bit of confused for this fragment & activity problems.
Thank you for helps.

First of all, your activity xml (the one you wanna use with the Navigation Drawer) must contain a <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android">
as its root element and a listview (last on the xml hierarchy)
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/background_light"
android:choiceMode="singleChoice" />
In your activity you should have:
DrawerLayout drawerLayout;
ListView drawerList;
ActionBarDrawerToggle drawerToggle;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.Open, R.string.Close);
drawerLayout.setDrawerListener(drawerToggle);
drawerList.setAdapter(new youradaptertype());
drawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
Intent l = new Intent(MainActivity.this,
FriendActivity.class);
startActivity(l);
break;
case 1:
Intent j = new Intent(MainActivity.this, RequestActivity.class);
startActivity(j);
break;
case 2:
Intent i = new Intent(MainActivity.this,
SettingsActivity.class);
startActivity(i);
break;
...
}
drawerLayout.closeDrawer(drawerList);
}
});
Inside each "case" statement, to start an activity you should use the Intent and start a new activity. If you simply wanna change fragments (instead of starting new activities) inside the "case" you should to something like this:
getSupportFragmentManager().beginTransaction()
.replace(YOUR_ROOT_VIEW_ID, new fragmenttobeinstantiated())
//optinal
.addToBackStack(null).commit();
See if it helps !

Related

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 attach an onclick listener to Android Studio's navigation drawer activity?

I am attempted to create an application in Android studio that has a navigation drawer.
I am using Android Studio (beta)0.8.14. In this version, there is a navigation drawer activity. I been able to set the labels for my navigation drawer menu using this piece of code and the corresponding values in my strings file
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.login);
break;
case 2:
mTitle = getString(R.string.sign_up);
break;
case 3:
mTitle = getString(R.string.view_map);
break;
case 4:
mTitle = getString(R.string.about);
break;
case 5:
mTitle = getString(R.string.version);
}
}
It looks really nice, but I can't figure out how to add onClickListeners for each of the items.
I've also added this in my NavigationDrawerFragment.java (which was automatically created by Android Studio):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
getString(R.string.login),
getString(R.string.sign_up),
getString(R.string.view_map),
getString(R.string.about),
getString(R.string.version),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
I would like to launch a different activity for each of the list items but I can't understand how and unfortunately I can't seem to find a tutorial that uses Android Studio's built-in navigation drawer activity.
Also, is it possible to have this navigation drawer available on all of my activities? Do I need to create a new navigation drawer fragment every time I create a new activity?
Thanks in advance!
The idea of having a navigation drawer is to use fragment. It wouldn't be very efficient to launch a new activity whenever you click an item in your navigation drawer.
In your selectItem(position) method you could execute some code for creating a new fragment for list item in your navigation drawer. Each navigation item should be a different fragment then just use a fragment transaction to add it to the container view of the main activity.
As a simple example of what the method may look like:
private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
switch(position) {
//fragment for position 0
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, new Fragment0())
.commit();
break;
//fragment for postion 1
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, new Fragment1())
.commit();
break;
//fragment for position 2
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, new Fragment2())
.commit();
break;
default:
break;
}
}
This may not be exactly what you want but it is an option for what you're trying to accomplish.
As a note in order for onSectionAttached() to work, all your framents will have to call that to pass their title to the main activity.
See this link for more info.
In order to put onclicklisteners on your navigation drawer, just put an intent below the line of code where you are changing the title bar i.e. in the switch case block
e.g.
case 1:
mTitle = getString(R.string.login);
Intent transfer = new Intent(HomeFragment.this,NextActivity.class);
startActivity(transfer);
This will do it for you.

How to launch activity from Navigation Drawer?

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.

get selected item - ListView Android

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
}

Navigation drawer doesn't close

The navigation drawer in my app is not closing. I am using activities instead of fragments. When i click on any item in the listview, it opens other activities as it should but when i go back, the drawer is still open. I have tried using DrawerLayout.closeDrawers(); but it did not work. How do I close the navigation drawer?
Here is my code:
Java
public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","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){
switch (pos){
case 0:
Intent i = new Intent(MainActivity.this,Aluminium.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(MainActivity.this,Gold.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(MainActivity.this,Zinc.class);
startActivity(i3);
break;
}
}
});
}
}
XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dp"
android:background="#000000"/>
</android.support.v4.widget.DrawerLayout>
have you tried :
mDrawerLayout.closeDrawer(drawerListView);
You can add this before calling startActivity()
In continuation to others answers and # Chinmay Dabke question of 'but the drawer closes half then pauses and then closes fully' in one of the comments, here is what you could do:
first as others suggested,
this line is missing. drawer.closeDrawer(navList);
And as far as the pausing of drawer is concerned while closing, you could do something like this.
use a Runnable and a Handler like this:
mRunnable = = new Runnable() {
#Override
public void run() {
//say
selectItem(pos); //Implement your switch case logic in this func
}
}
and then in the onDrawerClosed overrided method
#Override
public void onDrawerClosed(View view) {
if (mRunnable != null) {
mHandler.post(mRunnable);
mRunnable = null;
}
}
Hope this helps!
I would suggest you to use fragments for navigation drawer and to solve this issue of drawer not closing properly, I found this article very useful (using fragments). http://www.michenux.net/android-asynctask-in-fragment-best-pratices-725.html
Call
drawer.closeDrawer(navList);
in onItemClick() method
Try
drawer.closeDrawer(Gravity.START);
Your drawer gravity is start so Use that to close the corresponding drawer
I didn't see any code where you are closing the ListView from drawer... close the ListView Drawer on ListItem click...
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.closeDrawer(navList);
switch (pos){
case 0:
Intent i = new Intent(MainActivity.this,Aluminium.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(MainActivity.this,Gold.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(MainActivity.this,Zinc.class);
startActivity(i3);
break;
}
}
});
You need to close the drawer on list item click
drawer.closeDrawer(navList);
Also what is the use of FrameLayout in your xml. It is not used as a container to add or replace fragments
call the drawer.closeDrawer(navList); function before switch case
use
if(drawer.isDrawerOpen(navList))
{
drawer.closeDrawer(navList);
}
In onResume() and start of onItemClick() method.
or you can try another approach..run a Ui thread when you are selecting drawer item
private void selectDrawerItemItem(final int position){
//Toast.makeText(getApplicationContext(), "ItemClicked", Toast.LENGTH_SHORT).show();
darwer.closeDrawer(navList);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Fragment fragment = new Fragment(Activity.this);
Bundle args = new Bundle();
args.putInt(Fragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
// update selected item and title, then close the drawer
navList.setItemChecked(position, true);
setTitle(" " + navListTitles[position]);
}
}, 200);
// update the main content by replacing fragments
}
I was having the same problem.
I used
mDrawerLayout.closeDrawer(drawerListView);
before starting my new activity. It beautifully slides the drawer back in.
private DrawerLayout mDrawerLayout;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();
it works
Here is the code:
Lets say you have the drawer class
in your activity call the class as and make a vaiable and assign it(put the drawer layout within a fragment for smoother user experience)
Drawer drawer;
drawer = (Drawer)getActivity().getSupportFragmentManager().findFragmentById(R.id.theid);
drawer.mDrawerLayout.closeDrawer(Gravity.END);
//End for right and Start for left
Hope it helps

Categories

Resources