How to conserve menu when creating an Intent - java

I am very new to Java / Android programing. Android Studio offered me a layout that consists in a menu like such:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_empty"
android:icon="#drawable/ic_menu_send"
android:title="Empty" />
<item
android:id="#+id/nav_quotes"
android:icon="#drawable/ic_menu_send"
android:title="Quotes" />
<item
android:id="#+id/nav_info"
android:icon="#drawable/ic_menu_send"
android:title="Information" />
</group>
</menu>
For now I'm trying to make the second item of the menu work, here's where I am when I press it:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_empty) {
} else if (id == R.id.nav_quotes) {
Intent intent = new Intent(this, QuotesActivity.class);
startActivity(intent); // start the newly created Intent
} else if (id == R.id.nav_info) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
As you can see I create an Intent that leads me to QuotesActivity, but once I enter this page, the menu that was on the top left (classic clickable android menu) disappears.
Here's the activity_quotes.xml file for the QuotesActivity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.kade_c.plus.QuotesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/quote_welcome"
android:id="#+id/textView_quotes"
android:layout_marginTop="236dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quotes_button"
android:id="#+id/button_quotes"
android:onClick="onClick"
android:layout_marginBottom="184dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
What can I do to give the user the possibility to access the menu at any given time? Additionally, if you see flaws in how I proceed, please let me know !
Thank you.

Related

Can not go to onOptionsItemSelected in Android studio

I follow the tutorial https://www.youtube.com/watch?v=iEXh1-KVeVc&t=213s for making bottom menu.
Then i use onCreateOptionsMenu and onOptionsItemSelected to set action when click in, but it still not working.
Here is my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/leftNav"
android:icon="#drawable/ic_baseline_stars_24"
android:title="News"/>
<item
android:id="#+id/home"
android:title=""/>
<item
android:id="#+id/rightNav"
android:icon="#drawable/ic_baseline_menu_24"
android:title="Menu"/>
</menu>
MainActivity.java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu
getMenuInflater().inflate(R.menu.bottom_nav_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.leftNav:
// Left nav
Toast.makeText(this,"Click left",Toast.LENGTH_LONG);
return true;
case R.id.rightNav:
return true;
case R.id.home:
Toast.makeText(this,"Click home", Toast.LENGTH_LONG);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
activity_main xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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=".MainActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:id="#+id/bottomAppBar"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:fabCradleVerticalOffset="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottomNavigationView"
android:layout_marginRight="16dp"
app:menu="#menu/bottom_nav_menu"
android:background="#drawable/transparent_background"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home"
android:src="#drawable/ic_baseline_sports_basketball_24"
app:layout_anchor="#id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Any one can help me, i'm new with android studio. Thanks in advance

IDs to IDs linking on menus of drawer and toolbar to nav_graph not working

Please, I integrated the new Navigation Controller Component, using navHostFragment. My project runs on one activity-to-many fragments. It got a drawer & toolbar. I read that it is possible to set the ids in both menus (drawer, toolbar), to same ids in nav_graph for navigation when onClick event occurs.
activity_main.xml
<androidx.drawerlayout.widget.DrawerLayout 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="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/colorPrimary"
android:id="#+id/appContainer"
tools:context=".MainFragment"
tools:openDrawer="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_toolbar"
app:title="#string/app_name"
android:theme="#style/AppTheme.AppBarOverlay" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"
android:transitionName="#string/transition_type_container" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#color/drawer_selector"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/menu_left_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_graph.xml
<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/nav_graph"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.emexrevolarter.worksmart.MainFragment"
android:label="fragment_holder"
tools:layout="#layout/fragment_holder" />
<fragment
android:id="#+id/aboutFragment"
android:name="com.emexrevolarter.worksmart.AboutFragment"
android:label="fragment_about"
tools:layout="#layout/fragment_about" />
<fragment
android:id="#+id/cameraFragment"
android:name="com.emexrevolarter.worksmart.CameraFragment"
android:label="fragment_camera"
tools:layout="#layout/fragment_camera" />
<fragment
android:id="#+id/accountFragment"
android:name="com.emexrevolarter.worksmart.AccountFragment"
android:label="fragment_account"
tools:layout="#layout/fragment_account" />
<fragment
android:id="#+id/formCreateAccountFragment"
android:name="com.emexrevolarter.worksmart.forms.FormCreateAccountFragment"
android:label="fragment_form_create_acount"
tools:layout="#layout/fragment_form_create_acount" />
<fragment
android:id="#+id/formUserLoginFragment"
android:name="com.emexrevolarter.worksmart.forms.FormUserLoginFragment"
android:label="fragment_form_user_login"
tools:layout="#layout/fragment_form_user_login" />
<fragment
android:id="#+id/helpFragment"
android:name="com.emexrevolarter.worksmart.HelpFragment"
android:label="fragment_help"
tools:layout="#layout/fragment_help" />
<activity
android:id="#+id/cameraXActivity"
android:name="com.emexrevolarter.worksmart.media.CameraXActivity"
android:label="activity_camera"
tools:layout="#layout/activity_camera" />
<fragment
android:id="#+id/placeholderFragment"
android:name="com.emexrevolarter.worksmart.ui.main.PlaceholderFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main" />
<activity
android:id="#+id/mainActivity"
android:name="com.emexrevolarter.worksmart.MainActivity"
android:label="MainActivity" />
<activity
android:id="#+id/splashActivity"
android:name="com.emexrevolarter.worksmart.SplashActivity"
android:label="splash_animation"
tools:layout="#layout/splash_animation" />
<navigation android:id="#+id/nested_settings"
app:startDestination="#id/settingsFragment">
<fragment
android:id="#+id/settingsFragment"
android:name="com.emexrevolarter.worksmart.SettingsFragment"
android:label="fragment_settings"
tools:layout="#layout/fragment_settings" />
<fragment
android:id="#+id/formEditAboutFragment"
android:name="com.emexrevolarter.worksmart.forms.FormEditAboutFragment"
android:label="fragment_form_edit_about"
tools:layout="#layout/fragment_form_edit_about" />
<fragment
android:id="#+id/listUsersFragment"
android:name="com.emexrevolarter.worksmart.view.ListUsersFragment"
android:label="fragment_list_users"
tools:layout="#layout/fragment_list_users" />
</navigation>
<navigation android:id="#+id/nested_tips"
app:startDestination="#id/searchFragment">
<fragment
android:id="#+id/searchFragment"
android:name="com.emexrevolarter.worksmart.SearchFragment"
android:label="fragment_search"
tools:layout="#layout/fragment_search" />
<fragment
android:id="#+id/tipsFragment"
android:name="com.emexrevolarter.worksmart.TipsFragment"
android:label="fragment_tips"
tools:layout="#layout/fragment_tips" />
</navigation>
<navigation android:id="#+id/nested_tasks"
app:startDestination="#id/tasksFragment">
<fragment
android:id="#+id/tasksFragment"
android:name="com.emexrevolarter.worksmart.TasksFragment"
android:label="fragment_tasks"
tools:layout="#layout/fragment_tasks" />
</navigation>
<navigation android:id="#+id/nested_notifications"
app:startDestination="#id/notificationsFragment">
<fragment
android:id="#+id/notificationsFragment"
android:name="com.emexrevolarter.worksmart.NotificationsFragment"
android:label="fragment_notifications"
tools:layout="#layout/fragment_notifications" />
</navigation>
<navigation android:id="#+id/nested_report"
app:startDestination="#id/reportFragment">
<fragment
android:id="#+id/reportFragment"
android:name="com.emexrevolarter.worksmart.ReportFragment"
android:label="fragment_report"
tools:layout="#layout/fragment_report" />
</navigation>
menu_left_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:id="#+id/leftDrawer"
tools:viewBindingIgnore="true"
tools:context=".MainActivity">
<group
android:id="#+id/group1"
android:checkableBehavior="single">
<item android:id="#+id/aboutFragment"
android:title="#string/drawer_about"
android:icon="#drawable/ic_baseline_info_24"
android:iconTint="#color/colorPrimary" />
<item android:title="#string/drawer_label">
<menu>
<item android:id="#+id/helpFragment"
android:title="#string/drawer_help"
android:icon="#drawable/ic_baseline_help_24"
android:iconTint="#color/colorPrimary"/>
</menu>
</item>
</group>
main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/notificationsFragment"
android:icon="#drawable/ic_baseline_notifications_24"
app:showAsAction="ifRoom"
android:visible="false"
android:title="Notification">
</item>
<item
android:id="#+id/searchFragment"
android:icon="#drawable/ic_baseline_search_24"
app:showAsAction="ifRoom"
android:visible="false"
android:title="Search">
</item>
<item
android:id="#+id/accountFragment"
android:icon="#drawable/ic_baseline_lock_open_24"
app:showAsAction="ifRoom|withText"
android:title="Account">
</item>
Everything works as per navigation if I use the below:
subText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// action_global_aboutFragment
NavController navController = Navigation.findNavController(v);
navController.navigate(R.id.nested_settings);
}
});
More Info:
minSdkVersion 23
targetSdkVersion 30
Implementation:
// Java language implementation
def nav_version = "2.3.3"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
I need a solution to this, as regards IDs to IDs linking. I love the concept. Who got this working on Android Java?
Solved!
Listeners are needed for both Drawer & Toolbar
// setup click listeners for Drawer & Toolbar
drawerView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
if(id == R.id.accountFragment) {
// add your code here
} else {
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle Menu item selection
switch (item.getItemId()) {
case R.id.aboutFragment:
// add your code here
return true;
default:
// default function;
NavigationUI.onNavDestinationSelected(item, navController);
return true;
}
// return false;
}
});
Thank you.

Handle clicks on menu item in toolbar in android

How to handle clicks from a toolbar with Navigation Component in android?
I have a MaterialToolbar in an activity with a menu in xml, the menu shows up and accepts clicks but no action is executed.
This is how I initialize the navigation component:
binding = DataBindingUtil.setContentView(this, R.layout.activity_home);
navController = Navigation.findNavController(this, R.id.fragment_container);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph())
.setDrawerLayout(binding.activityHome)
.build();
NavigationUI.setupWithNavController(binding.navigationView, navController);
NavigationUI.setupWithNavController(binding.toolBar, navController, appBarConfiguration);
This is my Toolbar menu, it has only one item that will open a DialogFragment:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/dialog_account"
android:icon="#drawable/icon_account"
android:title="#string/dialog_account"
app:showAsAction="always" />
</menu>
Then this is the dialog in the navigation_graph
<dialog
android:id="#+id/dialog_account"
android:name="my.package.name.dialogs.DialogAccount"
android:label="#string/dialog_account"
tools:layout="#layout/dialog_account" />
This is the activity layout, nothing fancy there:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data></data>
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="#+id/fragment_container"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/tool_bar"
style="#style/toolbar_style"
app:menu="#menu/toolbar" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="#dimen/null_dimen"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/app_bar"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
style="#style/navigation_view" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
The Activity layout has a NavigationView and a MaterialToolbar, the navigation from the NavigationView works fine, but I can't find a way to open my dialog from the Toolbar even though it seems like is taking clicks but doing nothing. I have tried with onCreateOptionsMenu but it does not get triggered, guess the Navigation Component is working but my dialog is not opening on touching of the menu.
What am I missing here, any hand?
To handle on click on Toolbar simply use "setOnMenuItemClickListener" by providing id of the toolbar.
mToolbar= findViewById(R.id.main_tollbar);
mToolbar.inflateMenu(R.menu.game_menu);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.search_menu:
Toast.makeText(MainActivity.this,"Search Successful",Toast.LENGTH_LONG).show();
break;
case R.id.menus_logout:
Toast.makeText(MainActivity.this,"Logout Successful",Toast.LENGTH_LONG).show();
break;
case R.id.acc_settings:
Toast.makeText(MainActivity.this,"Setting Successful",Toast.LENGTH_LONG).show();
break;
case R.id.about_menu:
Toast.makeText(MainActivity.this,"About Successful",Toast.LENGTH_LONG).show();
break;
case R.id.feedback_menu:
Toast.makeText(MainActivity.this,"Feedback Successful",Toast.LENGTH_LONG).show();
break;
}
return true;
}
});
If you are using the Toolbar (without using setSupportActionBar(toolbar);) you have to use:
NavigationUI.setupWithNavController(toolbar,navController,mAppBarConfiguration);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
NavController navController = Navigation.findNavController(context, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController);
}
});
In your case it is enough since the id in the menu matches with the id in the navigation graph.
In general you can also use:
public boolean onMenuItemClick(MenuItem item) {
Navigation.findNavController(view).navigate(R.id.action...);
//....
}
Final note: the method onCreateOptionsMenu is only triggered if your are using an ActionBar.
For Navigation component you can use inflator
val view-inflater.inflate(R.Layout.fragment_main, container, false)
view.textview1.setonClickListener{
Navigation.findNavController(view).navigate(R.id.settings)
}

My ToolBar is too high and because of that it looks ugly and the screen below is cropped, how do I fix it?

Here's my toolbar screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Include your Toolbar -->
<include
android:id="#+id/mytoolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- Embed your fragment in a Layout View-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Activities.MenuActivity"
/>
</FrameLayout>
</LinearLayout>
How it looks on your phone:
You can see that the toolbar is too high.
Manifest:
<activity
android:name="com.example.Activities.MenuActivity"
android:label="#string/title_activity_test"
tools:ignore="InnerclassSeparator" />
<activity
Styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
MenuActivity.class
setContentView(R.layout.activity_menu);
Toolbar toolbar = findViewById(R.id.mytoolbar);
setSupportActionBar(toolbar);
Drawer(toolbar,MenuActivity.this,MenuActivity.this);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("Аппараты");
}
void Drawer:
public void Drawer(Toolbar toolbar, Context context, android.app.Activity activity){
DatabaseReference rootRef2 = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef2 = rootRef2.child("users").child(getUid());
ValueEventListener valueEventListener2 = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("account").getValue(String.class);
String Avatar = dataSnapshot.child("Avatar").getValue(String.class);
Long coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class);
String coinsView = "Баланс: " + coinsAmount;
if (Avatar.equals("1")) {
avatar = (R.drawable.a1);
}
if (Avatar.equals("2")) {
avatar = (R.drawable.a2);
}
IProfile profile = new ProfileDrawerItem()
.withName(email)
.withEmail(coinsView)
.withIcon(avatar);
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(activity)
.withHeaderBackground(R.drawable.account_header_background)
.addProfiles(
profile
)
.build();
result = new DrawerBuilder()
.withActivity(activity)
.withToolbar(toolbar)
.withAccountHeader(headerResult)
.addDrawerItems(
new PrimaryDrawerItem()
.withName("Устройства")
.withIcon(R.drawable.device)
.withIdentifier(1)
)
.addDrawerItems(
new PrimaryDrawerItem()
.withName("Сканер")
.withIcon(R.drawable.scaner)
.withIdentifier(2)
)
.addDrawerItems(
new PrimaryDrawerItem()
.withName("Магазины")
.withIcon(R.drawable.shop)
.withIdentifier(3)
)
.addDrawerItems(
new PrimaryDrawerItem()
.withName("Купоны")
.withIcon(R.drawable.coupons)
.withIdentifier(4)
)
.addDrawerItems(
new PrimaryDrawerItem()
.withName("Архив Купонов")
.withIcon(R.drawable.oldcoupons)
.withIdentifier(5)
)
.withOnDrawerItemClickListener((view, i, iDrawerItem) -> {
if(iDrawerItem.getIdentifier() == 1 && !Activity.equals("Menu")){
Intent intent = new Intent(context,MenuActivity.class);
startActivity(intent);
}
else if(iDrawerItem.getIdentifier() == 2 && !Activity.equals("Scaner")){
Intent intent = new Intent(context,DecoderActivity.class);
startActivity(intent);
}
else if(iDrawerItem.getIdentifier() == 3 && !Activity.equals("Shop")){
Intent intent = new Intent(context,ShopActivity.class);
startActivity(intent);
}
else if(iDrawerItem.getIdentifier() == 4 && !Activity.equals("oldCoupons")){
Intent intent = new Intent(context,oldCouponsActivity.class);
startActivity(intent);
}
return false;
})
.build();
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
Objects.requireNonNull(result.getActionBarDrawerToggle()).setDrawerIndicatorEnabled(true);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
uidRef2.addValueEventListener(valueEventListener2);
}
How to fix it and how do we all? I have long tried to fix it I used android:fitsSystemWindows="true" and I still did not help activity is cut only the first time if you restart the activity is all OK. I came to the conclusion that this may be due to firebase but how to solve this and eventually fix this problem I could not
Toolbar.xml
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="#dimen/materialize_toolbar"
android:background="#color/grey_500"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
Try setting android:fitsSystemWindows="true" to your base linear layout.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
Update - The answer that finally worked was to use CoordinatorLayout as the base layout.
P.S - To any person looking at this in the future, the other possible considerations were,
Setting it in the theme using <item name="android:fitsSystemWindows">true</item>
Setting it's value as false instead of true. It doesn't make logical sense, but I've seen people state that that works at times
Ensure that you have android:minHeight="?attr/actionBarSize" in your Toolbar
It might be worth completely replacing the container_toolbar LinearLayout with your Toolbar and set fitsSystemWindows as true.
Using a CoordinatorLayout or DrawerLayout your base layout. They're supposed to handle fitsSystemWindows differently from the other 'basic' layouts.
Consider the plethora of ways suggested here
And if all else fails, then you can override fitSystemWindows(Rect insets) to get your desired layout as the official docs suggests.
Try simplifying your layout. Put the toolbar and fragment inside the LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/mytoolbar"
layout="#layout/toolbar" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Activities.MenuActivity"
/>
</LinearLayout>

Android custom app bar underneath system default

My MainActivity.xml is as below:
<?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">
<include
layout="#layout/app_bar_appbar"
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_appbar"
app:menu="#menu/activity_appbar_drawer" />
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
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:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_3" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
</android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
My intention is to have an appbar with navigation drawer (icon on left), and options menu (icon on right). However, it appears to be stuck underneath the system default. Here is an image showing its preview:
Additionally, my styles.xml is:
<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.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Changing AppCompat.Light.DarkActionBar to NoActionBar has no effect. The appbar with "AppName" just remains above the other appbar (which should show "BEEP" as text, in addition to the two icons for navbar and options menu) - NoActionBar is also defined in the manifest for the activity:
<activity
android:name=".MainActivity"
android:label="BEEP"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is the relevant part of the MainActivity.java, if needed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
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.nav_view);
navigationView.setNavigationItemSelectedListener(this);
...
So, how can I get the toolbar (ID is "toolbar") to display instead of the default appbar that currently contains the text "AppName" in the above image? I also followed this, however there is no change in the result.
EDIT 1: The following screenshot shows my user-defined appbar appearing underneath the default, for some reason. I have also included my app_bar_appbar.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.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"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_appbar" />
</android.support.design.widget.CoordinatorLayout>
As you are having Theme.AppCompat.Light.DarkActionBar to your Application tag of AndroidManifest all activity will include default actionbar.
Create one more theme in your styles.xml with NoActionBar and apply this to your NavigationDrawerActivity.
i.e.
<style name="navigation_theme" parent="Theme.AppCompat.Light.NoActionBar"/>
Set it to your Navigation Activity
<activity
android:name=".NavigationDrawerActivity"
android:label="#string/title_activity_navigation_drawer"
android:theme="#style/navigationtheme" />
Example of default theme with ActionBar and NavigationDrawerActivity with NoActionBar theme
EDIT
As #Talisman said, it was the issue with the TabLayout and ViewPager
of DrawerLayout
<?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.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.and.NavigationDrawerActivity">
<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="#android:color/holo_blue_light"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--Your content in this FrameLayout-->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.and.NavigationDrawerActivity">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<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_navigation_drawer"
app:menu="#menu/activity_navigation_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
Now Set the TabLayout and ViewPager in other Fragment and Load that fragment in this FrameLayout on onCreate method of NavigationDrawerActivity
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tr = fm.beginTransaction();
tr.replace(R.id.frame_container, fragment);
tr.commitAllowingStateLoss();
where fragment is other fragment which contains your TabLayout and ViewPager.
Everything is looking perfect as it should be. If you just want to change the title of toolbar then you just have to add few lines of code in MainActivity.java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// add few lines here like...
toolbar.setTitle("Your title here");
// ....
setSupportActionBar(toolbar);
If right option menu not showing then override these two method in MainActivity.java
#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;
}
#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 false;
}
return super.onOptionsItemSelected(item);
}
First of all select "AppTheme" from layout preview
Select Project Theme >> AppTheme.NoActionBar
To create option menu for your Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_item:
//do need full stuff
break;
case R.id.menu_settings:
//do need full stuff
break;
}
}
create a menu.xml in res>menu folder
<?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">
<item
android:id="#+id/menu_item"
android:title="Item"/>
<item
android:id="#+id/menu_settings"
android:title="Settings" />
</menu>

Categories

Resources