TL;DR
The Item Menu (three points) on Action Bar
of your App, is usually used as Options menu, Settings or About.
Implementing Standard Design Navigation Drawer:
Implementing the method - onCreateOptionsMenu
First you need to implement the method onCreateOptionsMenu
in your file MainActivity.java
, in that way:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Creating the - main.xml file
Now edit or create a file (if it does not exist) in the "menu" folder, called main.xml
, with the following code:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_about"
android:title="@string/about"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Adding a String Name - about
Now add a string name called "about", with the name "About", there in the folder "values" in strings.xml
, in that way:
<resources>
<string name="app_name">Novo App</string>
<string name="about">Sobre</string>
</resources>
Now it’s just typing CTRL+S and then CTRL+F9 to see the result.
You will only have one menu item from 3 Pontos
"About" appearing in the Action Bar
ready to be "used", but you will not see anything happening, realize that if you click on it, nothing will happen, until you have not created another screen and nor implemented another very important method, see below.
Creating a new Fragment - sobreFragment
Create a new one now fragment
called sobreFragment
, in that way:
Click on your sample package: br.com.app
, with the right mouse button and proceed to New
> Fragment
> Fragment(Blank)
With the window New Android Component
open, make the following changes:
- Fragment Name: sobreFragment
- Fragment Layout Name: Fragment_about
Clear the options:
- Include Fragment Factory methods?
- Include callbacks interface?
now click the [Finish] button and wait while Android Studio creates its new fragment
...
Implementing the method - onOptionsItemSelect
After you’ve created your new fragment
, to open a new screen (here in case I used a Fragment
), for your "About" option to work, you will have to implement another method called onOptionsItemSelect
in your file MainActivity.java
, in that way:
@Override public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_about) {
sobreFragment fragment = new sobreFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
}
return super.onOptionsItemSelected(item);
}
See that above I used a id
called fragment_container
, to call the fragment
"sobreFragment" inside another fragment
empty, so that you can switch between the screens.
Example - Transition between Screens
If I click on a menu item Navigation Drawer
or in a menu item 3 pontos
of my Action Bar
, I will be sent to a certain screen and if then I click on another item, I will go to another screen, so the use of a fragment
empty with a id
call for @+id/fragment_container
to "store" the screens I need to see at that time.
So that no implementation error occurs in your method onOptionsItemSelected
, you need a new Activity
, or a new fragment
which is empty, so that you can toggle and "store" certain fragment
("screen"), inside it.
"Storing Screens - fragment_container"
In my case, I used the file content_main.xml
, that’s in the folder layout
and that was with nothing, to "store" my "screens" (Fragments), as made below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:background="@android:color/white"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
To call a new Activity():
Create a new Activity
by the name of MainActivity2
and insert this code below into your method onNavigationItemSelected
of his MainActivity
:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
To call a new Fragment(Fragment):
Create a new Fragment
by the name of sobreFragment
and insert this code below into your method onNavigationItemSelected
of his MainActivity
:
sobreFragment fragment = new sobreFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
In Your Mainactivity - Solution in Practice:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// abrindo um novo fragment
sobreFragment fragment = new sobreFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
} else if (id == R.id.nav_gallery) {
// abrindo um nova activity
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
** Important warning: **
If you created a new project in Android Studio, with a Blank Activity
and then created a menu Navigation Drawer
, should remember that you must follow the order of precedence of Activities, realize that your app will start with the blank screen first and nothing will happen, so remember to change your File AndroidManifest.xml
to start with the menu Navigation Drawer
, or whenever possible start a project directly with the menu Navigation Drawer
and later create the other Activities(activities) and Fragments(fragments).
Androidmanifest.xml file - Mode "Wrong":
See below, application starting with the BlankActivity
("Wrong way"), assuming that the Activity
blank is named after .BlankActivity
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.erroapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".BlankActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
Obs: note that the above code is not wrong, ele só será considerado errado se você não adicionar nada em sua tela, fazendo com que seu aplicativo inicie sem nada na tela
, see that for this above code to be correct you will have to transform this BlankActivity
on a login screen, where she registers/logs in a user and plays to the other screen of her app (logs in and plays the result to MainActivity
), or turn it into a splashscreen screen, as in this post: Activity does not change.
See how to change your file below AndroidManifest.xml
if this error occurs...
Androidmanifest.xml File - Correct Mode:
See below, application starting with the menu Navigation Drawer
(Correct mode), assuming that the Activity
menu Navigation Drawer
is named after .MainActivity
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.appcorreto">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BlankActivity"></activity>
</application>
</manifest>
Obs: Note that the above code is correct, assuming that your BlankActivity
nothing. Your application starts with the main activity(Mainactivity - which contains the Navigation Drawer menu setContentView(R.layout.activity_main);
) and its BlankActivity
does not interfere with anything, it is stopped, waiting to be called if it is the case, you can call this activity through the menu, as in the above excerpt > In Your Mainactivity - Solution in Practice: < changing only the call with the Intent of: MainActivity2.class
for: BlankActivity.class
.
I have an answer post here at Stackoverflow, which talks more about this precedence problem of Activities
(activities), access: Activity does not change
I hope I’ve helped.
Are you using android studio? It helps you a lot in this part when creating a project with Navigation Drawer.
– Furflez