In my app I have problem like this. Note that I'm working with fragments and I have drawer too.
That's the method in my MainActivity for drawer open/close.
public void drawerInit() {
toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer);
view = findViewById(R.id.mainView);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
float moveFactor = (drawerView.getWidth() * slideOffset);
view.setTranslationX(moveFactor);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
}
Example I have 3 fragments (F1, F2, F3). F1 is my main fragment where I can open and close the drawer. When I'm opening F2 or F3 fragments, I need to change the drawer icon to back arrow. I'm doing this part successfully, but the problem is when I'm clicking on this back arrow, that opens the navigation drawer instead of going back. So how can I fix this part?
Here the part, where I'm changing the icon to back arrow in fragment.
((AppCompatActivity) getActivity()).getSupportActionBar().show();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
Add in your Activity
public void crateMenuButton(){
toggle.setDrawerIndicatorEnabled(true);
if(toolbarDrawable == null) {
toolbarDrawable = toolbar.getNavigationIcon();
}
toolbar.setNavigationIcon(toolbarDrawable);
invalidateOptionsMenu();
toggle.syncState();
}
public void createBackButton() {
toggle.setDrawerIndicatorEnabled(false);
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if the drawerToggle is disabled, fall off to the home button action
if (!toggle.isDrawerIndicatorEnabled()) {
// pop fragment here
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStack();
}
} else {
if (drawerLayout.isDrawerOpen(navigationView)) {
drawerLayout.closeDrawer(navigationView);
} else {
drawerLayout.openDrawer(navigationView);
}
}
}
});
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white));
}
Download Back Arrow
Then call from your fragment as your need
((YourActivity) getActivity()).createBackButton();
OR
((YourActivity) getActivity()).crateMenuButton();
Related
This is the code. Please help me. If I click any item on NavigationView then the drawer was closing.
The app is running well but this one function is not working. When I debug this app then I saw navView.setNavigationItemSelectedListener is not called. If I click any item menu then close the drawer. How can I fix this issue? Please help me
private void initView() {
mToolbar = findViewById(R.id.dash_toolbar);
drawer = findViewById(R.id.drawer);
navView = findViewById(R.id.navigation_view);
bottomNavigationView = findViewById(R.id.bottom_navigation);
}
private void actionBar() {
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.app_name);
}
}
private void drawerMenu() {
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
AppBarConfiguration mAppBarConfiguration = new AppBarConfiguration.Builder
(R.id.nav_home, R.id.nav_share)
.setDrawerLayout(drawer)
.build();
navView.setNavigationItemSelectedListener
(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
Toast.makeText(DashboardActivity.this, "Home ", Toast.LENGTH_SHORT).show();
}
if (id == R.id.nav_share) {
Toast.makeText(DashboardActivity.this, "Share message", Toast.LENGTH_SHORT).show();
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
how to get back hamburg icon when set back icon?
hen I am implementing change toggle icon when fragment on activity ,its set to back icon but need when its back the fragment then set it to again hamburg icon for open drawer of the activity,
this is my code
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
toolbar.setNavigationIcon(R.drawable.ic_action_navigation_arrow_back);
//drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);// show back button
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
} else {
//show hamburger
///drawerFragment.mDrawerToggle.setDrawerIndicatorEnabled(true);
toolbar.setNavigationIcon(R.drawable.icon1);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
//drawerFragment.mDrawerToggle.syncState();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
}
});
}
}
});
If you are using fragment to show content and have the single toolbar inside host activity, you could manage your toolbar.
#Override
public void onResume() {
super.onResume();
ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();
actionBar.setTitle("First Fragment");
actionBar.setIcon(R.drawable.back_icon);
}
Do not forget to call
setSupportActionBar(toolbar)
I managed to create a drawer and have the hamburger sign but the hamburger is not working when tapped. Also how can I change the code so that my app has a transparent notification bar so that the color is same(or preferably a little darker) and one can see the app drawer opened in status bar. Something like this: Transparent status bar
FirstActivity.java:
public class FirstActivity extends AppCompatActivity {
DrawerLayout mDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false); //removes the package name from toolbar
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Window w = getWindow(); // in Activity's onCreate() for instance //Integration of app into status bar
// w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// }
// These lines are needed to display the top-left hamburger button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Make the hamburger button work
mDrawer = (DrawerLayout) findViewById(R.id.DL);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.app_name,R.string.app_name){
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerOpened(View drawerView) {
}
};
mDrawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
// toasts the message when ListView item is clicked
ListView mDrawerListView = (ListView) findViewById(R.id.left_drawer);
mDrawerListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String drawerstring = ("Menu Item at position " + position + " clicked.");
mDrawer.closeDrawer(GravityCompat.START);
Toast.makeText(getApplicationContext(),drawerstring,Toast.LENGTH_SHORT).show();
}
});
}
You have to override onOptionsItemSelected and handle the home item to open the drawer. Its not done for you, because Android doesn't know what you're using that button for (home? back? hamburger? Something else?). The ActionDrawerToggle knows how to handle it if you want to just delegate it.
public void setContentView(int layoutResID)
{
DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
supportInvalidateOptionsMenu();
//drawerOpened = false;
}
public void onDrawerOpened(View drawerView)
{
supportInvalidateOptionsMenu();
//drawerOpened = true;
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
}
I want to add navigation drawer icon to this navigation drawer activity. now i have edited code but still now error app stops at startup
maybe you can try to add it to layout file modifying the xml code,or follow this tutorial for more information Android Sliding Menu
I have two fragments MainFragment and First_Fragment. First fragment contains a video view but whenever I try to rotate, nav drawer always goes back to its main fragment. How could I save or restore my fragment state ? so it would stop going back to its main fragment.
Here's my code for the MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
VideoView videoView;
NavigationView navigationView = null;
Toolbar toolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainFragment first = new MainFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, first);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.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.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
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
MainFragment first = new MainFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, first);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
First_Fragment first = new First_Fragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, first);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
}
When you're not handling the activity change like orientation, keyboard, etc. it will automatically recreates the activity. Therefore you're fragment does not save your previous instance. To solve that, I only encounter two solution.
The first one is already mentioned by Zeeshan that handle your orientation change by following this step:
Handle the orientation change in your AndroidManifest
<activity
android:name="com.example.Activity"
android:label="Activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateHidden|adjustResize"
android:theme="#style/Theme.AppCompat.Light" />
Handle the event in the onConfiguration Change method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
The second one is to save the fragment state. Refer here https://stackoverflow.com/a/17135346/5870896
You can do setRetainInstance(true) in your fragments, but a BottomNavigationBar will work incorrectly. In this case you can use some boolean flag in activity and save/restore it with Bundle like this:
In onCreate:
if (savedInstanceState != null) {
isMapFragmentVisible = savedInstanceState.getBoolean("isMainVisible");
} else {
navigator.navigateToFragment(fragmentLocation);
}
In onSavedInstanceState:
outState.putBoolean("isMainVisible", isMapFragmentVisible);
In onNavigationSelectedListener:
case R.id.bottom_navigation_map:
if (isMapFragmentVisible) {
break;
} else {
navigator.navigateToFragment(fragmentMap);
isMapFragmentVisible = true;
}
return true;
case R.id.bottom_navigation_location:
if (!isMapFragmentVisible) {
break;
} else {
navigator.navigateToFragment(fragmentLocation);
isMapFragmentVisible = false;
}
return true;
For me it works perfect. Are there any more elegant ways?
Activity recreates when you rotate the device. you need to mention in your manifest that you will handle the orientation change in your code so that system don't do it like this:
<activity name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
and then handle the event
#Override
public void onConfigurationChanged(Configuration newConfig) {
}
in your activity
Write this in manifests.xml
android:configChanges="orientation"
like this:-
<activity
android:name=".fragmentLayouts.MainActivity"
android:configChanges="orientation" />
Add this to your java code:-
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
drawerListener.onConfigurationChanged(newConfig);
}