I have an old project in eclipse which uses the Sherlock actionbar, however I would like to replace this actionbar with AppTheme or AppCompat actionbar. I want the actionbar to have the same features such as, dropdown list view. Please help.
public class MainActivity extends SherlockActivity implements ActionBar.OnNavigationListener,
OnItemClickListener {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getSupportActionBar().getThemedContext();
R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
Use Toolbar from support design library and AppCompatActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar);
}
}
And add
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
in R.layout.activity_main
Related
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);
}
}
}
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");
}
}
I'm working with the new android toolbar.
/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
if (isStandardToolbar)
customToolbar();
else
originalToolbar();
isStandardToolbar = !isStandardToolbar;
}
/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
/* Called after an click event */
private void customToolbar() {
LayoutInflater inflater = this.getLayoutInflater();
Toolbar toolbar = (ToolBar) inflater.inflate(R.layout.newtoolbar, null);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
It works so far, but now I want to change the toolbar view, after clicking on a list element.
That's my problem, because the last code snippet doesn't produce an exception or other issues, so it should be all ok. But I see only the "old" toolbar. I tried to set visibility to GONE or INVISIBLE to the old one, but it doesn't have any effect.
In my activity_main.xml, I include R.id.toolbar, but I think, the second code must overwrite the old one!?
EDIT:
AFAIK, the new toolbar should replace the old actionBar. Toolbar is used to place navigation or other, specific content. I my case, I want to create a small action menu, where the user can edit or delete a list item.
I found the solution to your question (maybe its too late...) but below is the code I used to make it :
MainActivity.java :
package fr.zwedge.sot;
import android.app.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
public class MainActivity extends AppCompatActivity {
boolean isStandardToolbar = true;
android.support.v7.widget.Toolbar toolbar = null, newToolbar = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initToolbars();
((Button)toolbar.findViewById(R.id.tbt)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toogleToolbar();
}
});
((Button)newToolbar.findViewById(R.id.tbt2)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toogleToolbar();
}
});
}
/* Jump into, after the user clicks on a listview item */
private void toogleToolbar() {
if (isStandardToolbar)
customToolbar();
else
originalToolbar();
isStandardToolbar = !isStandardToolbar;
}
private void initToolbars() {
if (toolbar == null)
toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);
if (newToolbar == null)
newToolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.newtoolbar);
}
/* Called inside onCreate and if nothing was clicked */
private void originalToolbar() {
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
/* Called after an click event */
private void customToolbar() {
toolbar.setVisibility(View.GONE);
setSupportActionBar(newToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
}
And here are the two toolbars in main.xml :
<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.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/holo_green_dark"
android:background="#FF008F12">
<Button
android:text="Bla bla bla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbt" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.Toolbar
android:id="#+id/newtoolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/holo_green_dark"
android:background="#FF8F0800">
<Button
android:text="Change once again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbt2" />
</android.support.v7.widget.Toolbar>
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view" />
Hope it helps you, Darkball60
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar); //not working
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
the error occurred is setSupportActionBar(android.support.v7.widget.Toolbar) in AppCompactActivity cannot be applied to android.widget.toolbar,my toolbar.xml is given below
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
you must import
import android.support.v7.widget.Toolbar;
not
android.widget.toolbar
Error:
setSupportActionBar(android.support.v7.widget.Toolbar) in
AppCompactActivity cannot be applied to android.widget.toolbar,my
toolbar.xml
You are getting this error becuase you have used android.support.v7.widget.Toolbar in XML while you are importing android.widget.toolbar in your Activity.
Try
setActionBar(mToolbar);
instead of
setSupportActionBar(mToolbar);
or
import Toolbar from android.support.v7.widget package.
import android.support.v7.widget.Toolbar
I'm having problem with ActionBar and app-compat-v21.
ActionBar is shown everywhere except PreferenceActivity
I tried to follow recommendations and made it with Fragments:
public class PrefsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
and
public class PrefsActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PrefsFragment()).commit();
}
}
However, there's no ActionBar.
BTW, in Styles.xml is set <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
What's wrong?
In order to use the new appcompat v21 you have to:
extend the AppCompatActivity
use a theme which inherits from Theme.AppCompat.(for example Light or NoActionBar)
The PreferenceActivity doesn't extend the AppCompatActivity.
It is the reason why there is no actionbar in your code.
As solution you can create a PreferenceFragment as you are doing and use it in a standard AppCompatActivity.
UPDATED 12/06/2015
With the new 22.1+ appcompat you can use also the AppCompatDelegate to extend AppCompat's support to any Activity.
You can check this official link to AppCompatPreferenceActivity, where you can find an example of this technique.
With the app compat the toolbar needs to be defined in the layout. You can create a layout for the Settings Activity with the toolbar and then add the preference fragment.
For example:
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<fragment
android:id="#+id/settings_fragment"
android:name="com.bla.bla.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar" />
</RelativeLayout>
SettingsActivity:
public class SettingsActivity extends ActionBarActivity{
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar actionbar = (Toolbar) findViewById(R.id.toolbar);
actionbar.setTitle("Settings");
actionbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SettingsActivity.this.finish();
}
});
}
MyPreferenceFragment:
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);;
}
}
edit: Updated the answer to use ActionBarActivity instead of the deprecated PreferenceActivity