Hide/show arrow/menu on toolbar - android

So, i have Activity with FrameLayout, code of activity layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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_main"/>
and when i'm going to differents fragment i need to change toolbar.
I need to set different text, show back arrow, remove back arrow, and show/remove three dots in right corner for menu.
I have next code in Activity
#Override
public void onCreate(Bundle savedInstanceState) {
...
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
and next methods in my class
public void setTextTabBar(String title) {
getSupportActionBar().show();
getSupportActionBar().setTitle(title);
}
public void setTabBarFragment() {
getSupportActionBar().show();
getSupportActionBar().setTitle(getString(R.string.about_app_title));
toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setFragment(fragmentMain);
}
});
}
Is this right way? I'm coding to android 8+.
Also i need to add menu, and remove back arrow, how to implement this?

I don't think so, assuming your fragment transaction is replacing the <include layout="#layout/content_main"/> part of your code. According to this post: ActionBar (Support) with Fragment (support)
You need to extend each fragment so they can have their own action bars.
This makes sense as fragments are supposed to be modular in nature and only call back to the host activity when you implement special functions, callbacks, or listeners to do so.
As far as your back arrows go, you will need the code lines: getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); to enable and set the back button. And just change the booleans to false if removing the back arrow.

Related

How to make hide/show toolbar when our list scrolling to the top

How to make hide/show toolbar when our list scrolling to the top, knowing that the toolbar view is described inside activity_main.xml but recyclerView is described in another fragmet nomed Fragment_main.xml
sorry for my english :)
Since your activity which has the toolbar within its content view is starting the fragment, you can always get a hold of it from your fragment.
MainActivity mainActivity = (MainActivity)getActivity();
I would recommend doing a method for it in your MainActivity:
public void showToolbar(boolean show) {
// If you have your toolbar as a private member of MainActivity
toolbar.setVisiblity(show ? View.VISIBLE : View.GONE);
// But you can also do this
if (show) {
getSupportActionBar().show();
}
else {
getSupportActionBar().hide();
}
}
And then when you actually want to hide it from your fragment, call it:
((MainActivity)getActivity()).showToolbar(false);
To make the UI change more smooth, I recommend translating it instead of just instantly hiding it. Take a look at the top answer here for inspiration:
android lollipop toolbar: how to hide/show the toolbar while scrolling?
If you don't know how to take care of when to actually show or hide it via scroll logic, take a look at this library which handles a lot for you and also gives examples:
https://github.com/ksoichiro/Android-ObservableScrollView
This is kind of simple.
Just put this code in your toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>
This will do the magic : app:layout_scrollFlags="scroll|enterAlways"
remember to add in your xml the app call xmlns:app="http://schemas.android.com/apk/res-auto"
Check this tutorial: https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout
Use below code:
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
...>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.design.widget.CoordinatorLayout>
If RecyclerView is in another fragment, then add below line to the View which contain RecyclerView in this CoordinatorLayout.
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Must Use CoordinatorLayout.

Navigation view with fragment. Toolbar

So, I have an activity with navigation view. By click on its item I change fragment in activity. All fragment have the same toolbar. But one have this toolbar and TabLayout to it. I would like to know what is better to declare toolbar once on activity like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar" />
</RelativeLayout>
or declare it in each fragment.
The disadvantage of the first method is default toolbar shadow. When I add tabs in fragment, shadow looks like
When I tried 2 solution. All my toolbar was with back icon instead drawer animated logo.
Thanks.
I had the exact same problem. This is how I solved it:
Move the toolbars to the fragments like you suggested (so you won't have a shadow separating the two). This allows for a way more flexible way to implement (different) toolbars in your layouts too.
Replace the Toolbar's navigation icon by a custom one like this:
toolbar.setNavigationIcon(R.drawable.ic_action_menu);
(I used the Android Asset Studio to easily create an icon with the preferred color)
Now open the NavigationView with the new menu(home) icon. You can do this through the MainActivity (the one with the NavigationView). Create a public method in that Activity that opens the drawer:
public void openDrawer(){
mDrawerLayout.openDrawer(Gravity.LEFT);
}
Now call this method in the OnOptionsItemSelected in your fragments like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case android.R.id.home: //Menu icon
((MainActivity)getActivity()).openDrawer();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That's it. Of course the downside is that you must implement the Toolbar in each Fragment. However, this is the only way (that I know of) that enables you to have the Toolbar (+TabLayout) in a Fragment and still be able to control your NavigationView.
You can use AppBarLayout from design support library like:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
...
/>
</android.support.design.widget.AppBarLayout>
and then you can change visibility of tablayout.
For more information about desing layout library : link

Why ActionBar is Missing(Android) and how to add back button?

Hello to Stack Overflow community.
I have included the AppCompactActivity in my class but, the action bar still not showing in my app. And I also want to add back button at my action bar.
public class infoAplikasi extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
...
}
How, to solve it and how to add the back button to main? Thanks.
Using an AppCompatActivity means that you need to add a toolbar view in your R.layout.info to enable you to use an actionbar within your activity.
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?attr/colorPrimary">
and setting the toolbar to the activity is just calling the setSupportActionBar(toolbar) where you set the toolbar view in the parameter
Go here for more tutorial about adding a toolbar
and for the backbutton don't attach a navigation drawer to the toolbar.

Android extend up button

I want to extend the up button on the actionbar to include the activity name. For example, instead of only being able to click the arrow to go back, I could also click the words next to it. I know some apps allow this but I haven't seen any instructions on how to do it.
Here is an example of what it currently allows:
Here is an example of what I want it to do:
What you can do is create a custom layout in your toolbar. Then when the user clicks on the view containing the title text and the up button, you handle the logic yourself.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
app:contentInsetStart="72dp"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<!--Contains your up button and title text-->
<SomeCustomLayout
android:id="#+id/customLayout"
android:layout_width="match_parent"
android:layout_width="wrap_content" />
</android.support.v7.widget.Toolbar>
Then get the view in your code with
Toolbar toolbar = findViewById(R.id.toolbar);
SomeCustomLayout customLayout = toolbar.findViewById(R.id.customLayout);
customLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// You'll probably want to fire off
// an intent here to go back to the parent activity
}
});

Toolbar - add the up button

I am trying to use the Toolbar instead of the ActionBar, but I can't figure out how to add the up button to return to the previous activity.
I couldn't find any method that could relate to it.
How do I add the up button?
I guess what you are looking for is something like this:
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_detail);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Or in case of using in Fragment:
Toolbar toolbar = (Toolbar) view.findViewById(R.id.app_bar_detail);
((ActionBarActivity) getActivity()).setSupportActionBar(toolbar);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This will show up your Action Bar inside of your toolbar, but don't worry everything will fit together well. The last you have to do if you dont want any shadow under your action bar or any background of it is change your theme in vaules/styles.xml.
<style name="AppThmeme.Base" parent="Theme.AppCompat.NoActionBar">
If you want to do this in XML, you can use...
<android.support.v7.widget.Toolbar
app:navigationIcon="?homeAsUpIndicator"
...
If you're wondering why clicking on up button doesn't work with fragments, you need to set up a navigation listener as well, not sure why Google hasn't enabled it by default:
protected fun setupToolbar(toolbar: Toolbar) {
(activity as AppCompatActivity).run {
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener { onBackPressed() }
}
}
In case when previous activity is always same for a given activity then up/back button can be achieved with the help of parentActivityName attribute. It can be mentioned in AndroidManifest.xml file as shown below:
<activity android:name=".DetailActivity" android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Let's say DetailActivity was opened from MainActivity. So when you are on DetailActivity then tool bar will automatically show a left pointing arrow (Refer screenshot):
When we click on left pointing arrow then MainActivity gets displayed.
Calling getSupportActionBar().setHomeButtonEnabled(true); should still work I think, as long as you have already called setSupportActionBar(toolbar);
You can add your own 'up' button in toolbar, after all it is just a ViewGroup.
You can customize toolbar as much as you want, in your toolbar.xml, or wherever you have defined android.support.v7.widget.Toolbar in your layout add your 'up' button like given below :
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/color_toolbar"
android:layout_width="match_parent">
<ImageButton
android:id="#+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="uphandler"
android:src="#drawable/backbutton"
android:layout_gravity="end"/>
</android.support.v7.widget.Toolbar>
Now, define uphandler function in your activity to listen to this up button :
public void uphandler(View v){
this.finish(); // This will kill current activity, and if previous activity is still opened in background, it will come in front.
}
You have to add these line back button show automatically
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
on back, button click automatically back to activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
if you are using navigation component, then adding up button in fragment would be like this:
in your fragment.xml
<androidx.constraintlayout.widget.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=".presentation.recipeitem.RecipeDetailsFragment">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
android:elevation="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
.
.
then in your fragment you would do this
inside onViewCreated(..)
val navController = findNavController()
val appBarConfiguration = AppBarConfiguration(navController.graph)
viewBinding.toolbar.setupWithNavController(navController, appBarConfiguration)
if you need to add title then use this in onResume
viewBinding.toolbar.title = "my title"
if you are using newer version of android studio:
first declare toolbar of androidx not android
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent"
app:popupTheme="#style/Theme.Sunshine.PopupOverlay" />
second, get a reference to the toolbar in Activity.java and use following code;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar ab= getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);

Categories

Resources