extending NavigationDrawer Activity - java

I created NavigationDrawer class and now I`m creating new Activities which extends Navigation Drawer class. Unfortunately I am constantly getting such errors:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
here is the code for the NavigationDrawer Class:
public class Requests extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.requests);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent in = getIntent();
if (in.hasExtra("warehouse"))
{
id = R.id.nav_warehouse;
}
else if (in.hasExtra("cash"))
{
id = R.id.nav_cash;
}
if (id == R.id.nav_warehouse) {
Intent i = new Intent(getApplicationContext(), WarehouseRequests.class);
startActivity(i);
} else if (id == R.id.nav_cash) {
Intent i = new Intent(getApplicationContext(), CashRequests.class);
startActivity(i);
} else if (id == R.id.nav_log_out) {
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
i.putExtra("logout", 1);
startActivity(i);
}
return true;
}
}
Here is the XML files for NavigationDrawer:
requests.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:background="#dff3f7"
>
<include
layout="#layout/app_bar_requests"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_requests"
app:menu="#menu/warehouse_requests_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_request.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.sanzharaubakir.exgroup.Requests">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_requests" />
</android.support.design.widget.CoordinatorLayout>
and content_requests.xml:
<?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"
android:id="#+id/container"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.sanzharaubakir.exgroup.Requests"
tools:showIn="#layout/app_bar_requests">
</RelativeLayout>
Here is the code for my Activity:
public class WarehouseRequests extends Requests {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.warehouse, null, false);
drawer.addView(contentView, 0);
}
}
and here is what I have in styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>

add this to your styles.xml file
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style to your Activity in the manifest.xml file.
android:theme="#style/AppTheme.NoActionBar"

android:theme="#style/AppTheme.NoActionBar"
add this to your warehouserequests activity in manifest file

Related

bottomnavigationview with navigation Drawer

I want to implement a bottomnavigationview with a navigation drawer as an interior element
I managed to create a bottomnavigationview and a navigation drawer but I don't see how to put the navigation drawer in the bottomnavigationview
My MainActivity java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "debinf MainActivity";
//public static final String FRAGMENT_KEY = "fragment";
private BottomNavigationView bottomNavigationView;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: ");
bottomNavigationView = (BottomNavigationView) findViewById(R.id.main_bottom_nav);
navigationView = (NavigationView) findViewById(R.id.main_sidebar);
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer);
setupNavigation();
}
private void setupNavigation() {
Log.i(TAG, "setupNavigation: ");
navController = Navigation.findNavController(this, R.id.main_fragment);
appBarConfiguration =
new AppBarConfiguration.Builder(R.id.navigation_friends,R.id.navigation_order,R.id.navigation_home,R.id.navigation_groups,R.id.navigation_settings,R.id.navigation_profile,R.id.navigation_all_activity) //Pass the ids of fragments from nav_graph which you dont want to show back button in toolbar
.setDrawerLayout(drawerLayout)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); //Setup toolbar with back button and drawer icon according to appBarConfiguration
NavigationUI.setupWithNavController(navigationView, navController);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
/*
** Listener for bottomNavigation must be called after been setupWithNavController
** This command will override NavigationUI.setupWithNavController(bottomNavigationView, navController)
** and the automatic transaction between fragments is lost
* */
//bottomNavigationView.setOnNavigationItemSelectedListener(this);
//navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
Log.i(TAG, "onBackPressed: ");
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
Log.i(TAG, "onBackPressed: DRAWER IS OPEN - CLOSING IT");
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onSupportNavigateUp() {
Log.i(TAG, "onSupportNavigateUp: ");
// replace navigation up button with nav drawer button when on start destination
return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp();
}
My activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/main_drawer"
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"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/main_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/main_bottom_nav"
app:defaultNavHost="true"
app:navGraph="#navigation/mainnav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/main_bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/main_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>
My bottom_nav_menu.xml (fragment in bottomnavigationview):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_friends"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_friends" />
<item
android:id="#+id/navigation_order"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_order" />
</menu>
My drawer_menu.xml (fragments in drawer):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_friends"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_friends" />
<item
android:id="#+id/navigation_order"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_order" />
<item
android:id="#+id/navigation_groups"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_groups" />
<item
android:id="#+id/navigation_profile"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_profile" />
<item
android:id="#+id/navigation_settings"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_settings" />
</menu>
My mainnav_graph.xml (all fragments):
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/mobile_navigation"
app:startDestination="#+id/navigation_home">
<fragment
android:id="#+id/navigation_home"
android:name="fr.spindyo.bottombarwithdrawer.ui.home.HomeFragment"
android:label="#string/title_home"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/navigation_friends"
android:name="fr.spindyo.bottombarwithdrawer.ui.friends.FriendsFragment"
android:label="#string/title_friends"
tools:layout="#layout/fragment_friends" />
<fragment
android:id="#+id/navigation_order"
android:name="fr.spindyo.bottombarwithdrawer.ui.order.OrderFragment"
android:label="#string/title_order"
tools:layout="#layout/fragment_order" />
<fragment
android:id="#+id/navigation_groups"
android:name="fr.spindyo.bottombarwithdrawer.ui.groups.GroupsFragment"
android:label="#string/title_groups"
tools:layout="#layout/fragment_groups" />
<fragment
android:id="#+id/navigation_profile"
android:name="fr.spindyo.bottombarwithdrawer.ui.profile.ProfileFragment"
android:label="#string/title_profile"
tools:layout="#layout/fragment_profile" />
<fragment
android:id="#+id/navigation_settings"
android:name="fr.spindyo.bottombarwithdrawer.ui.settings.SettingsFragment"
android:label="#string/title_settings"
tools:layout="#layout/fragment_settings" />
<fragment
android:id="#+id/navigation_all_activity"
android:name="fr.spindyo.bottombarwithdrawer.ui.allFragment.AllFragment"
android:label="#string/title_all_activity"
tools:layout="#layout/fragment_all_fragment" />
</navigation>

NavigationDrawer doesn't work, it's closing before doing something

I made an empty activity and then I changed it to a DrawerLayout and every time I open the Menu and click an item the drawer is closing and nothing happens like i'm just opening and closing it, can't doing anything else.
This is the layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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"
tools:context=".MainActivity"
android:id="#+id/navigation_home_drawer">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/mainmenu"
>
</com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
And this is the Java Code
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private MapView mMapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = findViewById(R.id.navigation_home_drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView =findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
mMapView = findViewById(R.id.map);
initGoogleMap(savedInstanceState);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_home:{
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();
break;
}
case R.id.nav_profile: {
Intent i = new Intent(this, ProfileActivity.class);
startActivity(i);
finish();
break;
}
case R.id.nav_settings: {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
finish();
break;
}
}
return true;
}
}
Here's the menu layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_home"
android:title="Home"
android:icon="#drawable/ic_home_black_24dp">
</item>
<item
android:id="#+id/nav_profile"
android:title="My Profile"
android:icon="#drawable/ic_person_black_24dp">
</item>
<item
android:id="#+id/nav_settings"
android:title="Settings"
android:icon="#drawable/ic_settigs">
</item>
</menu>
I also have the code for Map in this file, but it's not important, when I select an item from that menu I just want to change the activity to that, but nothing happens, only close the navigation, can you please tell me if I forgot to add something or what should I change to make the navigation work? Thank you in advance!
You can use this code to make navigation drawer :
NavigationView navigationView;
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
navigationView = findviewbyid(R.id.nav_drawer_home);
drawerLayout = findviewbyid(R.id.drawer_home);
drawerLayout.requestLayout();
setUpNavigationView();
}
private void setUpNavigationView() {
navigationView.setNavigationItemSelectedListener(menuItem -> {
switch (menuItem.getItemId()) {
case R.id.nav_recharge:
break;
case R.id.nav_offers:
break;
case R.id.nav_rides:
break;
case R.id.nav_refer_friend:
break;
case R.id.nav_wallet:
break;
case R.id.nav_about_us:
break;
case R.id.nav_feedback:
break;
default:
break;
}
if (menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_recharge"
android:icon="#drawable/ic_recharge_wallet"
android:title="Rechange Wallet" />
<item
android:id="#+id/nav_offers"
android:icon="#drawable/ic_offers"
android:title="Offer's" />
<item
android:id="#+id/nav_rides"
android:icon="#drawable/ic_ride"
android:title="Rides" />
<item
android:id="#+id/nav_refer_friend"
android:icon="#drawable/ic_refer_friend"
android:title="Refer a friend" />
<item
android:id="#+id/nav_wallet"
android:icon="#drawable/ic_wallet_transfer"
android:title="Wallet Transfer" />
<item
android:id="#+id/nav_about_us"
android:icon="#drawable/ic_about_us"
android:title="About Us" />
<item
android:id="#+id/nav_feedback"
android:icon="#drawable/ic_feedback"
android:title="Feedback" />
</group>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/home_content"/>
<com.google.android.material.navigation.NavigationView
android:clickable="true"
android:id="#+id/nav_drawer_home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/nav_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

Open navigation drawer menu in another activity

I have created the navigation drawer menu and now i want it to appear in my main activity.
I have the following code in my main activity:
public class MainActivity extends sideMenu {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_main, contentFrameLayout);
}}
When i run the app it crashes when entering main activity and it tells this:
**Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.**
It specifies the error is in two places:
Menu activity -
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Main activity -
super.onCreate(savedInstanceState);
If someone can shed some light here i'd be grateful!
EDIT:
Android manifest:
<?xml version="1.0" encoding="utf-8"?>
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity android:name=".Registo" />
<activity android:name=".ListaEmpresas" />
<activity android:name=".ListaFavoritos" />
<activity android:name=".Candidaturas" />
<activity
android:name=".sideMenu"
android:label="#string/title_activity_side_menu"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
menu activity code:
public class sideMenu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.side_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} 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;
}
}
Menu XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
<include
layout="#layout/app_bar_side_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_side_menu"
app:menu="#menu/activity_side_menu_drawer" />
app_bar_side_menu xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context="amsi.dei.estg.ipleiria.pt.ima.sideMenu">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_side_menu" />
</android.support.design.widget.CoordinatorLayout>
Content_side_menu xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="amsi.dei.estg.ipleiria.pt.ima.sideMenu"
tools:showIn="#layout/app_bar_side_menu">
</android.support.constraint.ConstraintLayout>
nav_header_side_menu:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:backgroundTint="#android:color/holo_red_dark"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="IMA"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NOME USER" />
</LinearLayout>
Your theme already has an ActionBar so you will have to convert it to NoActionBar.
For Whole App:
Open the styles.xml and make sure the last line of the parent has a word NoActionBar for example :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- changed .DarkActionBar to .NoActionBar -->
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
For An Activity:
Or you can just change it for one particular activity in the Manifest like by adding this atrribute in the Manifest :
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
use this theme.
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

How to reset DrawerLayout navigation item icon and text colour when activity finished in Android?

I have MainActivity.java which contains DrawerLayout / Navigation. I used the following code in the MainActivity to launch About.java activity:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent = new Intent(Intent.ACTION_VIEW);
if (id == R.id.nav_fav) {
} else if (id == R.id.nav_themes) {
} else if (id == R.id.nav_about) {
intent = new Intent(getApplicationContext(), About.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
The About activity launches fine and the icon/text colour of the item in the DrawerLayout/ Navigation highlights/ active but it remains highlighted/active even the About activity is finished by pressing back button.
About.java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
default:
}
return true;
}
MainActivity.xml:
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_main_drawer.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_menu_favorite"
android:title="#string/favorite" />
<item
android:id="#+id/nav_themes"
android:icon="#drawable/ic_menu_theme"
android:title="#string/themes" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="#string/about_us" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/share" />
<item
android:id="#+id/nav_rate"
android:icon="#drawable/ic_menu_rate"
android:title="#string/rate_us" />
<item
android:id="#+id/nav_more"
android:icon="#drawable/ic_menu_more"
android:title="#string/more_apps" />
</menu>
</item>
</menu>
Graphical representation of the problem:
Thank you #MikeM for providing solution to the problem.
I had <group android:checkableBehavior="single"> in the layout of menu items which caused the problem. I removed the code and this fixed the problem.
<group>
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_menu_favorite"
android:title="#string/favorite" />
<item
android:id="#+id/nav_themes"
android:icon="#drawable/ic_menu_theme"
android:title="#string/themes" />
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="#string/about_us" />
</group>
According to android the code does the following:
When a checkable item is selected, the system calls your respective
item-selected callback method (such as onOptionsItemSelected()). It is
here that you must set the state of the checkbox, because a checkbox
or radio button does not change its state automatically. You can query
the current state of the item (as it was before the user selected it)
with isChecked() and then set the checked state with setChecked().
Android Menu Documentation

Toolbar icons not appearing in AppCompat Activity

I am trying to add toolbar to my app but somehow it is not working. Neither title not action icons are appearing. Even " : " menu is also not appearing. Here are my codes:
Style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Menu_main.xml:
<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="com.example.vaibhav.thirdeye.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item android:id="#+id/next"
android:title="#string/next"
android:orderInCategory="200"
android:icon="#drawable/next_arrow"
app:showAsAction="always"/>
</menu>
content_main.xml:
<?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"
android:background="#drawable/back_splash"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.vaibhav.thirdeye.MainActivity"
tools:showIn="#layout/activity_main"
app:theme="#style/AppTheme.NoActionBar">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
MainActivity.java:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.vaibhav.thirdeye.MESSAGE";
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adding Custom toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.next_arrow);
toolbar.setTitle("ThirdEye");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Toast settings = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
settings.show();
case R.id.next:
Toast next = Toast.makeText(this, "Voila!", Toast.LENGTH_SHORT);
next.show();
}
return true;
}
}
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/icon_size"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
Output Screenshot:
Any helps????
Remove extra toolbars in your MainActivity's activity_main.xml layout, it will solve your problems.

Categories