How to move to another activity using bottom navigation bar using kotlin in android studio? [duplicate] - android

//Here is my java code
public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
BottomNavigationView bottomNavigationView;
NavigationView navigationView;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull final MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
return true;
case R.id.navigation_stylist:
StylistFragment stylistsFragment=new StylistFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
return true;
case R.id.navigation_apps:
MyapptsFragment myaaptsFragment=new MyapptsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
return true;
case R.id.navigation_tips:
HairtipsFragment hairtipsFragment=new HairtipsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
return true;
case R.id.navigation_account:
AccountFragment accountFragment=new AccountFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//start onboarding when app is opening first time
isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(HomeActivity.this, PREF_USER_FIRST_TIME, "true"));
Intent introIntent = new Intent(HomeActivity.this, OnboardingActivity.class);
introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);
if (isUserFirstTime)
startActivity(introIntent);
setContentView(R.layout.activity_home);
bottomNavigationView = findViewById(R.id.bottom_navigation);
navigationView=findViewById(R.id.nav_drawer);
//bottom navigationview listener
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
//navigation drawer listener
navigationView.setNavigationItemSelectedListener(this);
//open home fragment on first launch
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
HomeFragment homeFragment=new HomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout,homeFragment).commit();
return true;
case R.id.nav_products:
StylistFragment stylistsFragment=new StylistFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1=getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frameLayout,stylistsFragment).commit();
return true;
case R.id.nav_promotions:
MyapptsFragment myaaptsFragment=new MyapptsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction2=getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frameLayout,myaaptsFragment).commit();
return true;
case R.id.nav_purchases:
HairtipsFragment hairtipsFragment=new HairtipsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction3=getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frameLayout,hairtipsFragment).commit();
return true;
case R.id.nav_settings:
AccountFragment accountFragment=new AccountFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction4=getSupportFragmentManager().beginTransaction();
fragmentTransaction4.replace(R.id.frameLayout,accountFragment).commit();
return true;
}
return false;
}
}
// Here is my 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.support.design.widget.NavigationView
android:id="#+id/nav_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="#style/menu_text_style"
app:menu="#menu/navigation_drawer" />
<!--app:headerLayout="#layout/nav_header_main"-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/shadow"
android:animateLayoutChanges="true">
</FrameLayout>
<View
android:id="#+id/shadow"
android:layout_width="match_parent"
android:layout_height="#dimen/_1sdp"
android:layout_above="#id/bottom_navigation"
android:background="#color/shadow"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="#color/navigationitem"
app:itemTextColor="#color/navigationitem"
app:menu="#menu/navigation_item"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I want to create both navigation drawer and bottom navigation in the same activity. I created XML design now I have to write java code for both. Bottom navigation is working perfectly but navigation item clicklistener is not working. I created fragments for navigation drawer and also for bottom navigation. When I click the items in navigation drawer it's not redirecting to respective fragment. The given clicklistener for navigation drawer is not at all working.

Solution:
Try the following steps to get it working:
Step1: Implement onNavigationItemSelected as shown below:
.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Step2: Use the method generated by the interface as:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.your_drawer_item_id:
....
....
case R.id.your_drawer_item_id:
....
....
}
}
Step3: In your class declare a Global variable:
public NavigationView navigationView;
Step4: In your onCreate() initialize the variable as:
navigationView = (NavigationView) findViewById(R.id.your_nav_view);
Finally: Setup the navigationView:
navigationView.setNavigationItemSelectedListener(this);
That's it.
Hope it works.
DrawerLayout 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">
<include
layout="#layout/app_bar_vendor_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/White"
android:fitsSystemWindows="true"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/bottom_color"
app:itemTextColor="#color/bottom_color"
app:headerLayout="#layout/nav_header_vendor_drawer_layout"
app:menu="#menu/vendor_drawer_menu" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_vendor_drawer_layout 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"
tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyMaterialTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/Project_Standard"
app:popupTheme="#style/MyMaterialTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_above="#+id/bottom_navigation">
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/toolbar_dropshadow_above"
android:layout_above="#id/bottom_navigation"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/White"
app:itemIconTint="#color/bottom_color"
app:itemTextColor="#color/bottom_color"
app:menu="#menu/vendor_bottom_navigation"/>
</RelativeLayout>
Try it.

You can use on the same activity using a method that recive a MenuItem parameter like this:
private void myClickItem(MenuItem item){
switch (item.getItemId()) {
case R.id.bottom_home:
// DO SOMETHING
break;
case R.id.drawer_main:
// DO SOMETHING
break;
}
}
Then, call like this:
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(item -> { // using lamda
myClickItem(item); //call here
return true;
});
NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(item -> {
myClickItem(item); // call the same method here
drawerLayout.closeDrawers(); // bonus: hide navigation after click
return false;
});
You don't need to modify your xml res, it'll work fine.

Related

Navigation drawer: How do I set the navigation item selected listener with custom tool bar?

I don't see any error when I open the navigation drawer, but it keeps getting closed whenever I select an item. Edit: My drawer gets closed automatically on item selection without showing the Toast message as mentioned in NavigationItemSelectedListener. The toast message is not displayed.
I have used a custom toolbar in which I have added an image view as a side menu button.
<?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=".CustomToolbar"
android:id="#+id/drawer_layout">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header_file"
app:menu="#menu/main_manu"
android:layout_gravity="start"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bggradient">
<include
android:id="#+id/toolbar"
layout="#layout/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
public class CustomToolbar extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
private ImageView side_menu;
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_toolbar);
toolbar = (Toolbar) findViewById(R.id.navigation_bar);
side_menu = findViewById(R.id.side_menu);
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.navigation_view);
setSupportActionBar(toolbar);
toggle = new ActionBarDrawerToggle(this, drawer, R.string.open, R.string.close);
drawer.addDrawerListener(toggle);
toggle.syncState();
side_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(Gravity.LEFT);
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home_menu: {
Toast.makeText(CustomToolbar.this, "Home Selected", Toast.LENGTH_SHORT).show();
}
case R.id.community_menu: {
Toast.makeText(CustomToolbar.this, "Community Selected", Toast.LENGTH_SHORT).show();
}
case R.id.posts_menu: {
Toast.makeText(CustomToolbar.this, "Posts Selected", Toast.LENGTH_SHORT).show();
}
case R.id.website_menu: {
Toast.makeText(CustomToolbar.this, "Website Selected", Toast.LENGTH_SHORT).show();
}
case R.id.share_menu: {
Toast.makeText(CustomToolbar.this, "Share Selected", Toast.LENGTH_SHORT).show();
}
case R.id.feedback_menu: {
Toast.makeText(CustomToolbar.this, "Feedback Selected", Toast.LENGTH_SHORT).show();
}
case R.id.about_menu: {
Toast.makeText(CustomToolbar.this, "About us Selected", Toast.LENGTH_SHORT).show();
}
case R.id.logout_menu: {
Toast.makeText(CustomToolbar.this, "Logout Selected", Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
I have tried other method with default toolbar, and it was working properly but I am not able to figur out the problem with custom toolbar. I wanted to display a toast on item selection inside the navigation drawer. Please help me find the solution...
Thank you.
In onNavigationItemSelected, Change this
return false;
to
return true;
After so long finally I found the solution to my problem. But I don't know why it was happening like that but I'm happy that it is finally working. And I am also very happy to share it with those who may experience the same issue in the future which I hope they won't.
I referred to the link from the Git hub for this solution. Link: https://github.com/priyalbhatewara123/Navigation-Drawer--Android
Finally here is the solution I found,
I just added the following lines of codes below </ScrollView> which was above <ScrollView> in the problematic code used in my activity_custom_toolbar.xml.
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header_file"
app:menu="#menu/main_manu"
android:layout_gravity="start"/>
I'm also adding the code after changes,
<?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=".CustomToolbar"
android:id="#+id/drawer_layout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bggradient">
<include
android:id="#+id/toolbar"
layout="#layout/layout_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header_file"
app:menu="#menu/main_manu"
android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

How to open search interface over BottomNavigationView's fragment?

I want to implement an interface having BottomNavigationView where clicking on the top search bar opens the next interface shown in figure 2. I have implemented BottomNavigationView. But unable to do that above said.
Here is the code:
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomNavigationView = findViewById(R.id.home_bottom_nav_view);
bottomNavigationView.setSelectedItemId(R.id.home_recent_menu_id);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = new RecentFragment();
switch (item.getItemId()) {
case R.id.home_contact_menu_id:
selectedFragment = new ContactFragment();
break;
case R.id.home_recent_menu_id:
selectedFragment = new RecentFragment();
break;
case R.id.home_status_menu_id:
selectedFragment = new StatusFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.home_fragment_container_lyt_id, selectedFragment).commit();
return true;
}
});
}
}
Home Activity XML code
<?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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="#+id/home_search_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#+id/home_fragment_container_lyt_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/home_search_view_id"
android:layout_above="#+id/home_bottom_nav_view"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/home_bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="#menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
create a BottomNavigationFragment which is going to contain ViewPager2 with BottomNavigationView and that view pager will contain [favourite, recent and contacts ] fragments
create a SearchFragment which will be used for searching purpose
create a navigation graph which will contain [BottomNavigationFragment and SearchFragment] when user clicks on search view navigate him to SearchFragment
you can also set animation using Navigation Component lib
//adapter for ViewPager2 vp2 in BottomNavigationFragment
lets say your SearchView is in favourite fragment onClick or onFocus of that
findNavController().navigate(R.id.toSearchFragment)
you will land in Search fragment

After including Relative layout in the Drawer layout options in the menu don't respond to click

I created navigation drawer across multiple activities. In xml I have a Drawer layout. Before including relative (or others) layout in it, options in the menu were clickable (going to other activities). However, after including layout, it stopped going to other activities (options are not clickable).
<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"
android:clickable="true"
android:focusable="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main2"
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"
android:clickable="true"
android:focusable="true"
app:headerLayout="#layout/nav_header_main2"
app:menu="#menu/activity_main2_drawer" />
<include
layout="#layout/main2_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
And code for Main2Activity:
#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.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id) {
case R.id.nav_camera:
Intent h = new Intent(Main2Activity.this, Main2Activity.class);
startActivity(h);
break;
case R.id.nav_gallery:
Intent g = new Intent(Main2Activity.this, Settings.class);
startActivity(g);
break;
case R.id.nav_slideshow:
Intent s = new Intent(Main2Activity.this, Allergy2.class);
startActivity(s);
break;
case R.id.nav_manage:
Intent t = new Intent(Main2Activity.this, Instruction.class);
startActivity(t);
break;
}
Why not using Fragments instead of Activities!!
To clear out your question you said that you want to switch between multiple activities and these all activities have the same NavigationDrawer Layout!
If yes its a very bad practice...you can use Fragments instead of each activity you've created in the NavDrawerMenu... TODO that follow me:
First you will create your MainActivity with the Navigation drawer as normal then in the method onNavigationItemSelected here you will begin the transactions between the fragments instead of Intent as follows:
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
//here is the main place where we need to work on.
int id=item.getItemId();
switch (id) {
case R.id.nav_camera:
getFragmentManager().beginTransaction()
.replace(R.id.main_frameLayout, new
BlankFragment1()).commit();
break;
case R.id.nav_gallery:
getFragmentManager().beginTransaction()
.replace(R.id.main_frameLayout, new
BlankFragment2()).commit();
break;
case R.id.nav_slideshow:
getFragmentManager().beginTransaction()
.replace(R.id.main_frameLayout, new
BlankFragment3()).commit();
break;
}
NOTE: you surly know that these fragments must be the child of an activity so main_frameLayout is the id of FrameLayout that the fragments are placed in in the activity you placed the NavigationDrawer in which is the MainActivity as follows:
app_bar_nav_drawer.xml
<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"
tools:context="com.tkmsoft.taahel.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="0dp"
android:layout_height="?android:actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:id="#+id/toolbar_title"
style="#style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/toolbar_filter_button"
android:gravity="center|start"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/main_frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
What's probably happening here is that your drawer is shadowed by the additional children you've created in the DrawerLayout.
To fix this, in the onCreate method of your Main2Activity, get a reference to your NavigationView and bring it to front:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.bringToFront();
If you added CoordinatorLayout and then inside -> AppBarLayout as it's child in app_bar_main2 with Toolbar, then you better to add your contents inside the CoordinatorLayout or maybe inside a NestedScrollView which is a child of the CoordinatorLayout.
The problem why they're not clickable is that the DrawerLayout cannot recognize this layout as it's child:
<include
layout="#layout/main2_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So, put the contents of this layout inside CoordinatorLayout and it's child NestedScrollView.

Toolbar arrow don't work in PreferenceFragment in NavigationDrawer

I'm using PreferenceFragment that I launch from the NavigationDrawer. In the preferenceFragment I show the toolbar. All looks good, but when I press the arrow in the toolbar to come back it doesn't work. I can see in the log this:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
But don't do nothing. The only way to come back is press button back in the device
Some help will be appreciated.
This is my code:
PreferenceFragment:
public class AnPreferenceActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pref_with_actionbar);
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new SettingsPreference())
.commit();
}
public static class SettingsPreference extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
}
}
}
Layout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationContentDescription="#string/abc_action_bar_up_description"
app:navigationIcon="?attr/homeAsUpIndicator"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/action_settings" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar" />
</RelativeLayout>
In onCreate() of your fragment first you should declare it has option Menu:
setHasOptionsMenu(true);
Then you should handle your own menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// close fragment here
getActivity().getFragmentManager().popBackStack();
return true;
default:
break;
}
return false;
}
If you need more Menu create new menu item:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fragment_menu, menu);
return true;
}
Accessing to toolbar from fragment is not good approach you should handle it in better way. There are lots of thread about this issue out there. (in fact Activity should handle Toolbar behaviour)

Replacing fragments in one activity

I am able to add a fragment to my fragmentlayout, but when I'm trying to replace the fragment with a new one the screen is blank. I can add a new fragment but then it's not scrollable. I'm also not able not remove a fragment.
Does someone know what I'm doing wrong? I red a lot about it and I feel like I have tried everything..
The framelayout is defined static:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>'
And this is the method for switching fragments:
void TabOnTabSelected (object sender, ActionBar.TabEventArgs tabEventArgs)
{
ActionBar.Tab tab = (ActionBar.Tab)sender;
var fragmentTransaction = FragmentManager.BeginTransaction ();
switch (tab.Text) {
case "vandaag":
if (currentFragment != null) {
fragmentTransaction.Remove (FragmentManager.FindFragmentByTag ("gister"));
}
if (fragmentToday == null) {
fragmentToday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (0).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentToday, "vandaag");
currentFragment = fragmentToday;
break;
case "gister":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentYesterday == null) {
fragmentYesterday = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (-1).ToString ("d-M-yyyy"));
}
currentFragment = fragmentYesterday;
fragmentTransaction.Add (Resource.Id.fragment_container, currentFragment, "gister");
break;
case "morgen":
if (currentFragment != null) {
fragmentTransaction.Remove (currentFragment);
}
if (fragmentTomorrow == null) {
fragmentTomorrow = WZWVDataOverview.NewInstance (DateTime.Now.AddDays (+1).ToString ("d-M-yyyy"));
}
fragmentTransaction.Add (Resource.Id.fragment_container, fragmentTomorrow);
currentFragment = fragmentTomorrow;
break;
}
fragmentTransaction.Commit ();
}
XML of the framelayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list" />
</LinearLayout>
Huge thanks in advance!
You can try using the .replace method. If you do not wish to be able to move back to the previous fragment than you can remove addToBackStack().
This may need to be altered slightly to fit your application.
FragmentManager fragmentManager = getFragmentManager();
Fragment mFragment = new DetailFragment();
mFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.fragment_container, mFragment).addToBackStack("DETAILFRAGMENT").commit();
POSSIBLE SECOND SOLUTION:
Check to see how your Fragment layout XML is. Possibly try using a FrameLayout as the parent.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.modup.fragment.DetailFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>
I have open fragments from navigation view as items are selected from it.(I have use DataBinding Concept)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.k15.projectkhyatisalesapp.activities.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:titleTextColor="#ffffff"
android:background="#ef6c00"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:theme="#style/ThemeOverlay.AppCompat.Dark"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp"
android:orientation="horizontal">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="wrap_content"
android:id="#+id/activity_main_frame_layout"
android:layout_height="wrap_content"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/activity_main_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/listnavigation" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</RelativeLayout>
<layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
//Declaration of Variables
private ActivityMainBinding mainBinding; //for binding values in xml
private ActionBarDrawerToggle mDrawableToogle; //This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.
private Fragment fragment;
private Class fragmentClass = null; //pointing to class of fragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(mainBinding.toolbar); //o designate a Toolbar as the action bar for an Activity
setUpDrawer(mainBinding.activityMainNavigation); //for select items in navigationView
mDrawableToogle = setupToogle();
mainBinding.drawerLayout.addDrawerListener(mDrawableToogle);
}
//private method to set navigationView
private void setUpDrawer(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
try {
selectItem(item); //call when item is selected from navigationView
} catch (IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
return true;
}
});
}
//items are selected from navigationView
public void selectItem(MenuItem menuItem) throws IllegalAccessException, InstantiationException {
switch (menuItem.getItemId()) {
case R.id.Home:
fragmentClass = Home.class;
break;
case R.id.Product:
fragmentClass = Product.class;
break;
case R.id.Media:
fragmentClass = Media.class;
break;
}
assert fragmentClass != null;
fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(mainBinding.activityMainFrameLayout.getId(), fragment).commit(); //change the fragment
setTitle(menuItem.getTitle());
menuItem.setCheckable(true); //for set selected item
mainBinding.drawerLayout.closeDrawers();
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawableToogle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawableToogle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupToogle() {
return new ActionBarDrawerToggle(this, mainBinding.drawerLayout, mainBinding.toolbar, R.string.open, R.string.close);
}
menu/listnavigation.xml is attached to navigation view I contains items of navigation view.
In my code as items are selected from navigation view accordingly fragment is replaced. Here Home, Product, Media are my fragment, you can create any fragment.
Try using fragmentTransaction.replace() instead of fragmentTransaction.add() and comment out code for removing fragments. When you replace the fragment - it gets removed. Also - remember to call: fragmentTransaction.commit() when you are done with replacing fragments.

Categories

Resources