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.
Related
I have an app with Single Activity multiple Fragments architecture in my app. The problem is, there is one listing fragment with normal Toolbar, from here user can click and go to Details screen. Now in Details screen, I want the Activity to become Fullscreen with CoordinatorLayout and Collapsing Toolbar with StatusBar area covered in layout as transparent. And when user goes back to Listing screen. The Activity should disable fullscreen and again get the StatusBar color.
The problem here is I am setting the activity as fullscreen using following code:
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
And when the user goes back, I am disabling the Fullscreen by the following code:
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
After disabling the fullscreen, in my previous fragment, I am getting UI bounds clipped off.
Thanks in advance.
Try this in your DetailsActivity -
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
I would recommend using
android:theme="#style/AppTheme.NoActionBar"
for your activity.
and in your XML activity build in custom toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/action_bar_bkgnd"
app:theme="#style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toolbar Title"
android:layout_gravity="center"
android:id="#+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
and in your activity class
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
Now for your fragments, all you have to do is to show or hide this toolbar depends on the fragment you want to the same or another toolbar , And you can do that with OnResume and OnPause of the fragments.
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.
How can I add this labels with arrow below a toolbar?
By that I want to make a transition - with the help of an animation - from one activity to the other.
Thanks in advance!
You can create a custom toolbar. First create your toolbar_layout.xml and place whatever you want (including your label and arrows) into that layout. Then add the following to your activity_layout.xml:
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
And finally add the following to your activity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setSupportProgressBarIndeterminateVisibility(true);
}
I cannot grasp what's wrong with my code and why I am getting java.lang.NullPointerException raised by the setSupportProgressBarIndeterminateVisibility().
My gradle dependencies contains:
compile 'com.android.support:appcompat-v7:21.0.0'
Does anybody know how to use the indeterminate progress bar in the support.v7 action bar with the API 21?
You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.
Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.toolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
//Make progress bar appear when you need it
progressBar.setVisibility(View.VISIBLE);
//Make progress bar disappear
progressBar.setVisibility(View.INVISIBLE);
}
Hope this helps :)
EDIT: replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.
Per this comment on Chris Banes' (the author of AppCompat) AppCompat v21 announcement post:
Either way, progress bar's are not supported on Toolbar anymore.
On why:
Because Toolbar is a highly focused widget. If you want a ProgressBar, add it to the Toolbar yourself (it's just a ViewGroup).
I'd expect more information as the official documentation gets updated here shortly.