How to change bottom app bar navigation icon programmatically - android

I am making an Android application which has one activity and many fragments. The activity contains a bottom app bar and that bottom bar has a navigation icon in it. Like this:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorbottomappbar"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/ic_menu_green_24dp">
</com.google.android.material.bottomappbar.BottomAppBar>
This navigation menu icon will be shown in every fragment. However, in some fragments I want to change that navigation icon in the bottom app bar to back button/icon. How can I achieve this? Also, currently I handle the navigation icon click in the main activity. How can I handle the click in the case of the back icon? How will it know what the current fragment is and how can I determine which fragment the back icon leads to?

If you look at the documentation, you'll see that BottomAppBar extends from Toolbar and it has an inherited method called setNavigationIcon(int res).
You can implement an interface that your main Activity implements like so:
interface FramentChangedListener {
void onFragmentChanged(int type);
}
Your activity would do something like this:
public class MainActivity extends Activity implements FragmentChangedListener {
// This will keep track of what is currently shown
private int current = 0;
#Override
public void onFragmentChanged(int type) {
if (type == FirstFragment.SOME_TYPE) {
// Update the current fragment value, we're associating each fragment
// with an int value.
current = type;
bottomAppBar.setNavigationIcon(R.drawable.your_back_icon);
}
}
...
}
In your fragment you would do something like this:
public class FirstFragment extends Fragment {
private FragmentChangedListener listener;
public static final int SOME_TYPE = 1;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceOf FragmentChangedListener) {
// context in this case is your activity, which implements FragmentChangedListener
listener = (FragmentChangedListener) context;
// You can call the listener now
listener.onFragmentChanged(SOME_TYPE);
}
}
}
In your Activity, add a listener to the BottomAppBar via setNavigationOnClickListener and whenever you receive the navigation icon event, you can check against the current value that we defined.

Related

Opening bottom sheet when clicked on the overflow menu on a fragment

I am trying to implement behavior of opening bottom sheet when clicked on overflow menu. ex: expected behavior
I may do this on an activity using onMenuOpened as suggested here,
But I want to do this on fragment.
How to achieve this behavior on a fragment?
I am using single activity pattern and navigation architecture component.
Create a interface which will be implemented by your Fragment's
ex:
public interface OnMenuOpenListener(){
boolean onMenuOpened();
}
public class MyFragment extends Fragment implements OnMenuOpenListener{
#Override
public boolean onMenuOpened(){
//open bottom sheet here
}
}
public class MyActivity extends Activity{
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == AppCompatDelegate.FEATURE_SUPPORT_ACTION_BAR && menu != null){
//overflow menu clicked, put code here...
// As you are using navigation component
Fragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
//MyFragment
Fragment fragment=navHostFragment.getChildFragmentManager().getFragments().get(0);
if(fragment instanceof OnMenuOpenListener){
((OnMenuOpenListener)fragment).onMenuOpened()
return false;
}
}
return super.onMenuOpened(featureId, menu);
}
}
As Support Action Bar is attached to Activity All the event's are captured by Activity all you need to do is get the Fragment which need's the event and trigger the call using a call back.If you return false onMenuOpened will not open the overflow menu and will trigger bottom sheet menu from your fragment.
P.S- I have not written the code in Editor so might have some error's but you must have got the idea.
Reference:
https://stackoverflow.com/a/51732378/7972699
As discussed here opening bottom sheet when clicked on the overflow menu is bad UX.
Why?
Quoting from the post
Because user have to reach the top of the screen to click the oveflow
menu, then go back to the bottom to click desired action which is on
the bottom sheet.
-
According to Fitt's Law - The time to acquire a target is a function
of the distance to and size of the target. I agree that I think
distance between the menu and the bottom sheet is substantial. This
solution allows placing a lot options in one place.
-
it also doesn't match the user expectation since people are used to
the overflow menu opening in a different manner.
If you have a top action bar, use usual context menu. If you have a bottom action bar you may use bottom sheet.
**You can try the following steps to open bottom sheet dialog:**
1. Just make a function inside Activity where the fragment is replace
public Fragment getCurrentFragment() {
return getSupportFragmentManager().findFragmentById(R.id.frameContainer);
}
Fragment fragment = getCurrentFragment();
if (fragment != null) {
if (fragment instanceof RequiredFragment) {
RequiredFragment.openBottumSheetDialog();
}
}
2. In Side RequiredFragment get your function from activity:
private BottomSheetDialog mBottomSheetDialogFragment;
private void showBottomSheetFilter() {
if (mBottomSheetDialogFragment == null) {
mBottomSheetDialogFragment = mBottomSheetDialogFragment.newInstance(feedSection);
mBottomSheetDialogFragment.setCallBackListener(new OnFeedsTypeSelectedListener() {
#Override
public void onFeedsTypeSelected(int contentType) {
filterByContentTypeId(contentType);
}
}
mBottomSheetDialogFragment.show(getChildFragmentManager(),
mBottomSheetDialogFragment.getTag());
}
3. Create a BottomSheetDialog Dialog fragment.
public class BottomSheetDialog extends BottomSheetDialogFragment {
private String[] feedsFilter;
private ListView listView;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
feedsFilter = getResources().getStringArray(R.array.ideas_filter);
}
#Override
public void setupDialog(final Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.dialog_idea_filter_bottom_sheet, null);
dialog.setContentView(contentView);
listView = (ListView) contentView.findViewById(R.id.listView);
ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(),
android.R.layout.simple_list_item_1, feedsFilter);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView < ? > parent, View view,
int position, long id) {
if (onFeedsTypeSelected != null) {
onIdeaTypeSelectedListenonFeedsTypeSelecteder.onFeedsTypeSelected(feedsFilter[position]);
}
dismiss();
}
});
}
public void setCallBackListener(onFeedsTypeSelected SelectedListener onFeedsTypeSelected) {
this.onIdeaTypeSelectedLionFeedsTypeSelectedstener = onFeedsTypeSelected;
}
}

Detecting when floating action button is hidden

I have a scrolling activity, and I want to be notified when the floating action button is being hidden.
In order to do so, I created MyFab which extends FloatingActionButton and an interface (to use as a callback) to call when the floating action button is being hidden, but I can't quite figure out what method is being called when I scroll and the floating action button disappears (I want to override the method, so I can call super and the callback that is implemented in the activity)
The interface:
public interface OnAppearanceChangedListener {
void onAppearanceChanged(int alpha);
}
My Floating Action Button:
public class MyFloatingActionButton extends FloatingActionButton {
private OnAppearanceChangedListener mListener;
public void setListener(OnAppearanceChangedListener mListener){
this.mListener = mListener;
}
/*insert code here to call mListener.onAppearanceChanged(),
probably by overriding some method */
}
The activity:
public class MainActivity extends Activity implements OnAppearanceChangedListener {
#Override
public void onAppearanceChanged(boolean visible) {
// I want this function to be called when Floating Action Button hide/show state is changed
}
}
--edit--
figured it out. animate() was the function I was looking for. It is being called to animate the floating action button shrinking action (what I was referring to as hiding)
Your question is quite ambiguous but if what you are looking for is to animate (hide) the fab on scroll you might want to refer to this tutorial. Otherwise explain your post to gain more help in community.
Cheers

How to disable or hide a Drawer Layout from Fragment Android

I have 10 different fragments in my application. I need to hide Navigation drawer (Drawer Layout) in few fragments, how can I access Drawer Layout from a fragment and hide it? I know we need to use in activity mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); but how to do it in fragments?
You could do something like this in your Fragment:
private MainActivity main;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
main = (MainActivity) activity;
}
You definitely should avoid this!
A mutch better solution would be to use an Interface to communicate between your Main and the Fragment. You will end up with something like this:
public interface MyInterface {
public void lockDrawer();
public void unlockDrawer();
}
Main:
public class DetailViewActivity extends AppCompatActivity implements MyInterface {
#Override
public void lockDrawer() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
#Override
public void unlockDrawer() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
Fragment:
public class MyFragment extends Fragment {
private MyInterface myInterface;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
myInterface = (MyInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement MyInterface");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
myInterface.lockDrawer();
return inflater.inflate(R.layout.example_fragment, container, false);
}
#Override
public void onDestroyView() {
super.onDestroyView();
myInterface.unlockDrawer();
}
}
Why this is the best solution: If you do something like ((HomeActivity) mActivity) you will not be able to reuse your Fragment.
There will be a ClassCastException. In order to reuse your Fragment you should use an Interface instead of casting you MainActivity. So every Activity which will use
your Frament can simply implement this Interface. Even if there's no DrawerLayout you can use it. So the big effort is reusability.
KOTLIN SOLUTION WITH NAVIGATION COMPONENT:
If you use a navigation component (one main activity with multiple fragment destinations) then you need to use addOnDestinationChangedListener to handle which fragment will you show and on which will hide your navigation view inside drawer layout.
Here you can see how to start with the navigation component and here is about the setDrawerLockMode method.
So your code will look something like this:
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navController = findNavController(R.id.nav_host_fragment)
navController.addOnDestinationChangedListener{_, destination, _ ->
if (destination.id == R.id.nav_fragment1) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
//DRAWER LOCKED IN fragment1
} else if (destination.id == R.id.nav_fragment2) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
//DRAWER UNLOCKED IN fragment2
} else {.....
}
}
This part of the code you can put in the onCreate() method in your MainActivity.
The easiest way to make a Navigation Drawer Activity is automatically in your android studio. Just follow File -> New -> Activity -> Navigation Drawer Activity.
You can do this by following way -
Write one public method inside your activity as follows -
public void enableDisableDrawer(int mode) {
if (mDrawerLayout != null) {
mDrawerLayout.setDrawerLockMode(mode);
}
}
and then inside fragment's onResume you can call this and change Drawer lock mode as required -
((HomeActivity) mActivity).enableDisableDrawer(DrawerLayout.LOCK_MODE_UNLOCKED);
OR
((HomeActivity) mActivity).enableDisableDrawer(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
where mActivity is my activity reference.
This way is working for me.
You can use this method to lock or unlock the drawer: DrawerLayout.setDrawerLockMode(...). (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use DrawerLayout.LOCK_MODE_LOCKED_CLOSED; to unlock, use DrawerLayout.LOCK_MODE_UNLOCKED.
If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// check lock mode before passing to ActionBarDrawerToggle
// I assume your drawer is on the left; if not, use Gravity.RIGHT
int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
refer link for more infolink here
Create two methods in your activity. One for opening the drawer and other for closing it. See below code.
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
.......
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
.........
}
public void openNavigationDrawer() {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
public void closeNavigationDrawer() {
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
public void lockNavigationDrawer() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
public void unLockNavigationDrawer() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
Now call the above methods from your fragment like below
((MainActivity)getActivity()).closeNavigationDrawer(); // to close drawer
((MainActivity)getActivity()).openNavigationDrawer(); // to open drawer
((MainActivity)getActivity()).lockNavigationDrawer(); // to lock drawer
((MainActivity)getActivity()).unLockNavigationDrawer(); // to unlock drawer
You can use this method to lock or unlock the drawer: DrawerLayout.setDrawerLockMode(...). (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use DrawerLayout.LOCK_MODE_LOCKED_CLOSED; to unlock, use DrawerLayout.LOCK_MODE_UNLOCKED.
If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.
enter code here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// check lock mode before passing to ActionBarDrawerToggle
// I assume your drawer is on the left; if not, use Gravity.RIGHT
int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
I struggled with it for hours: setting DrawerLayout to lock mode simply didn't work for me. Even following your examples.
Finally I came up with this post
DrawerLayout.setDrawerLockMode won't work if you set layout_gravity e.g. to start|bottom in the NavigationView. Just set it to start or end
Hope this helps someone

How to add Navigation Drawer to all the activities in the application?

Whats the efficient way to add Navigation Drawer on all the activities? I don't want to repeat the code for Navigation Drawer in all the activities and their layouts. Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Yes this is definitly the cleanest way to go.
public BaseActivity extends Activity {
#Override
protected void onCreate()
super.onCreate(); // calls Activity.onCreate()
// setup your Navigation Drawer
}
public FirstActivity extends BaseActivity {
#Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the FirstActivity
}
public SecondActivity extends BaseActivity {
#Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the SecondActivity
}
The "hard work" will be the layouts. Have one baseLayout for the BaseActivity with a place holder for the Content View (the visible part of the Activities). For all other Activitys use this layout and include your Content View.

ActionBarSherlock: Change dropdown navigation title

currently i am using ActionBarSherlock for my project. I am creating my actionbar with this code.
setTheme(R.style.Theme_Sherlock);
Context context = getSupportActionBar().getThemedContext();
list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
My question is. After I choose an option from the dropdown navigation, How do I keep that state throughout my activities?
For Example, in the homescreen, I choose "Sports" under my dropdown navigation. The title of the dropdown navigation then becomes "Sports". When I change activites however, the dropdown navigation title defaults back to the first item on the list.
One method I used was to create a base activity that each navigation item / activity extended from. Within the base activity, I overloaded onResume with an int to track which activity was active, and set the selected navigation item in that method.
Example:
public class BaseActivity extends FragmentActivity {
//...
protected void onResume(final int actId) {
super.onResume();
//...setup your action bar via getSupportActionBar() calls...
getSupportActionBar().setSelectedNavigationItem(actId);
}
Then in your individual activities:
public class ExampleActivity extends BaseActivity {
private final int ACT_ID = 1;
//...
protected void onResume() {
super.onResume(ACT_ID);
//...
}
}
Hope that helps!

Categories

Resources