Custom Toolbar for all activities not working as expected? - android

I want to set a custom toolbar with a back arrow (to go to the previous activity, which in this case is always back to main activity) for all activities except main activity.
As read from a couple of posts, I have created a custom toolbar activity and here is the code:
activity_toolbar_back_arrow.xml
<LinearLayout
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"
android:orientation="vertical"
tools:context=".ToolbarBackArrow">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:title="My First App"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:id="#+id/toolbar_back_arrow"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"/>
</LinearLayout>
ToolbarBackArrow.class
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
public class ToolbarBackArrow extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar_back_arrow);
Toolbar toolbar = findViewById(R.id.toolbar_back_arrow);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Now an another activity is created that extends the above custom activity:
VisionMission.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
public class VisionMission extends ToolbarBackArrow {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vision_mission);
}
}
In the activity_vision_mission.xml, I have some code specific to just that activity.
As per the solutions, I read this should create a toolbar in the VisionMission activity, but it is not. What am I missing?
In manifest, I have made theme as No ActionBar for all the activities.

No need to go for custom toolbar if your only requirement is back button.
In your manifest.xml file for the specified activity,
<activity
android:name="com.example.app_name.A" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name=".B"
android:label="B"
android:parentActivityName="the activity to go on back click" >
<!--android:parentActivityName="com.examle.myparentactivity" -->
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
</activity>
It will automatically show the back button with desired behaviour.

It's because in your ToolbarBackArrow you are setting your layout first and then in VisionMission you're resetting the layout and old layout with your toolbar is being replaced by another call to setContentView. To achieve desired result use includes:
include_toolbar.xml:
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:title="My First App"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:id="#+id/toolbar_back_arrow"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"/>
activity_vision_mission.xml:
<LinearLayout
...>
<include layout="#layout/include_toolbar" />
... Your other views ...
</LinearLayout>
and in super class ToolbarBackArrow
public class ToolbarBackArrow extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Not needed anymore
// setContentView(R.layout.activity_toolbar_back_arrow);
Toolbar toolbar = findViewById(R.id.toolbar_back_arrow);
// always check because you can forget to add an 'include' and
// toolbar will be null here, so you'll get NullPointerException
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}

Related

How replace setSupportActionBar by styles?

In my android app, I can't use AppCompatActivity.
I have no source code of this part of the project.
So I use
androidx.fragment.app.FragmentActivity
app theme:
<style name="MainAppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
my layout with toolbar:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="#dimen/tool_bar_height"/>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Here is my code snippet:
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
public class MainNavigationDrawerFragmentActivity extends androidx.fragment.app.FragmentActivity
mToolbar = (Toolbar) findViewById(R.id.toolBar);
toolbaTitleTextView = (TextView) findViewById(R.id.toolbaTitleTextView);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar,
R.string.application_name,
R.string.application_name) {
public void onDrawerClosed(View view) {
toolbaTitleTextView.setText(mTitle);
invalidateOptionsMenu();
}
as result toolbar is show but menu not show.
To show the menu I need to use
setSupportActionBar(toolbar);
but I can't use this method, because I can't use AppCompatActivity.
Is it possible to replace setSupportActionBar(toolbar); by another method or maybe settings/styles to show menu it toolbar?

setSupportActionBar cannot be applied to android.support.v7.widget.Toolbar

On this new project, when setting a toolbar into the main activity java file, it gets the error on
"setSupportActionBar(mToolbar)" saying "getSupportActionBar() in
AppCompatActivity cannot be applyed to
(android.support.v7.widget.Toolbar)"
On similar questions, the answers was just to change from "import android.widget.Toolbar" to "import android.support.v7.widget.Toolbar" which is already set.
The main activity xml is this:
<?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"
android:background="#color/colorAccent"
tools:context=".MainActivity">
<include
android:id="#+id/main_page_toolbar"
layout="#layout/app_bar_layout"
>
</include>
</RelativeLayout>
The toolbar I'm trying to insert is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_app_bar"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
And the main java file:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.main_page_toolbar);
getSupportActionBar(mToolbar);
getSupportActionBar().setTitle("WhatsApp");
}
}
Am I missing something? A compatibility set?
Looks like it's a typo, and you're calling getSupportActionBar() (with a g) instead of setSupportActionBar().
You need to use setSupportActionBar() to configure toolbar in the activity:
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("WhatsApp");
}
}

Getting 'java.lang.UnsatisfiedLinkError: Native method not found: ' on setSupportActionBar() for Toolbar

After getting past the earlier problem (Getting 'The method setSupportActionBar(Toolbar) in the type AppCompatActivity is not applicable for the arguments (Toolbar)' in my AppCompatActivity), now setSupportActrionBar() is crashing. Again, here is the relevant code:
import android.support.v7.widget.Toolbar;
public class FivetoGo extends AppCompatActivity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
.
.
.
and the layout:
<?xml version='1.0' encoding='UTF-8' ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6d7fe2"
android:minHeight="?android:attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp" >
</android.support.v7.widget.Toolbar>
.
.
.
</RelativeLayout>
It turns out that all I needed was to change the parent theme from Theme.AppCompat.Light to Theme.AppCompat.Light.NoActionBar.

Toolbar using drawable at background doesn't display home button within Motorola device

When i use a drawable at toolbar background, the home button isn't displayed. Curiously, i just notice that in Motorola device's.
here's the xml layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/topo"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_below="#id/toolbar"
android:id="#+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- the layout which will be the content of the activity (which will be hosted inside the drawer (NOT the list of the drawer)) -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textSize="16sp" />
</FrameLayout>
</FrameLayout>
</RelativeLayout>
here's then activity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.mikepenz.iconics.typeface.FontAwesome;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.SectionDrawerItem;
public class CustomContainerActivity extends AppCompatActivity {
//save our header or result
private Drawer result = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_custom_container_dark_toolbar);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Create the drawer
result = new DrawerBuilder(this)
//this layout have to contain child layouts
.withRootView(R.id.drawer_container)
.withToolbar(toolbar)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(
new PrimaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_gamepad),
new PrimaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_eye),
new SectionDrawerItem().withName("teste 1"),
new SecondaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_cog),
new SecondaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_question).setEnabled(false),
new SecondaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName("teste 1").withIcon(FontAwesome.Icon.faw_bullhorn)
)
.withSavedInstance(savedInstanceState)
.build();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (result != null && result.isDrawerOpen()) {
result.closeDrawer();
} else {
super.onBackPressed();
}
}
}
http://i.stack.imgur.com/eiDbK.png
Screen at GenyMotion emulator: Motorola Moto X at left, Samsung S5 at right
The problem occurs in real devices too.
Previewsly, thanks for any help

How can I align Android Toolbar menu/icons to the left like in Google Maps app?

Here is a screenshot of Google Maps Toolbar.
As you can see icons are aligned to the left instead of right (default behavior). I've tried adding android:layout_gravity="left" and android:gravity="left" to the toolbar but it didn't work. I also tried adding an internal LinearLayout (with same gravity values) to the Toolbar but that didn't work either. Any ideas?
I want to be able to use a regular Android menu with the Toolbar widget instead of recreating everything from scratch.
After some struggling and digging in Android Toolbar code I managed to make it work. Basically, the idea is to add a new android.support.v7.widget.ActionMenuView as child of the Toolbar, set its gravity to top|start, and then add the menu to that action menu view in your Activity. Here is the code:
my_toolbar.xml
<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/tToolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:gravity="center_vertical|start"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<android.support.v7.widget.ActionMenuView
android:id="#+id/amvMenu"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>
my_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Toolbar-->
<include
android:id="#+id/tToolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/my_toolbar" />
</RelativeLayout>
MyActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public final class MyActivity extends ActionBarActivity {
private ActionMenuView amvMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this layout includes the custom toolbar my_toolbar.xml
setContentView(R.layout.my_activity);
Toolbar t = (Toolbar) findViewById(R.id.tToolbar);
amvMenu = (ActionMenuView) t.findViewById(R.id.amvMenu);
amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
setSupportActionBar(t);
getSupportActionBar().setTitle(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// use amvMenu here
inflater.inflate(R.menu.my_activity_menu, amvMenu.getMenu());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Do your actions here
return true;
}
}
If there is no special reason to add toolbar items as menu actions, UI controls can be directly added to the toolbar, and aligned similar to aligning any other item inside a layout.
For an example, following toolbar has a left aligned Spinner and a right aligned EditText.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/tab_background"
android:gravity="center_vertical">
<Spinner
android:id="#+id/categorySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:hint="Name" />
</android.support.v7.widget.Toolbar>

Categories

Resources