Toolbar Menu not clickable on Android API 19 - android

I am trying have my android app work on multiple versions and have ran into a jam with API 19. I have a toolbar that is not clickable. The toolbar shows but clicking is not firing. The app works fine for API 21 and 23. I also notice that the view pager is also not clickable but the swipe works. Not sure what I am doing wrong...
Toolbar:
<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/toolbarGeneral"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
Style:
<resources>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">false</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
</resources>
MainActivity:
public class MasterActivity : AppCompatActivity, View.IOnClickListener//, Android.Support.V4.App.FragmentActivity
{
//private XmppClient _xmppClient;
private ViewPager _mViewPager;
//private bool _shouldImportContact;
//private bool _shouldInitialiseSystem;
private AppCompatActivity _context;
private MasterPageChangeListener _pageChangeListener;
private IMenuItem _editMenu;
private SQLiteConnection _dbConnection;
private int _fragmentPos;
private Android.Support.V4.App.Fragment[] _masterFragments;
private CRUDRepository<FacebookInfo> _facebookCRUD;
private FacebookInfo _facebookInfo;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_facebookInfo = new FacebookInfo();
_facebookCRUD = new CRUDRepository<FacebookInfo>();
_fragmentPos = Intent.GetIntExtra("FragmentPos", 0);
_dbConnection = SQLiteHelper.GetConnection();
try
{
_context = this;
SetContentView(Resource.Layout.Master); //Set view the Master view
Toolbar.Toolbar toolbar = FindViewById<Toolbar.Toolbar>(Resource.Id.toolbar);
this.SetSupportActionBar(toolbar);
_mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
_mViewPager.Adapter = new MasterAdapter(base.SupportFragmentManager, _masterFragments, titleLabels);
_mViewPager.SetCurrentItem(_fragmentPos, true);
TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(_mViewPager);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
fab.SetOnClickListener(this);
//Create the pageChangeListener class so that the page selected is known
_pageChangeListener = new MasterPageChangeListener(this, _mViewPager);
_mViewPager.AddOnPageChangeListener(_pageChangeListener);
}
catch (Exception Ex)
{
var data = Ex.Message;
}
}
OnPrepareOptionsMenu and OnCreateOptionsMenu:
public override bool OnPrepareOptionsMenu(IMenu menu)
{
if (_pageChangeListener.tabPosition == 0)
_editMenu.SetVisible(true);
else if (_pageChangeListener.tabPosition == 1 || _pageChangeListener.tabPosition == 2)
_editMenu.SetVisible(false);
return base.OnPrepareOptionsMenu(menu);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
base.OnCreateOptionsMenu(menu);
var inflater = MenuInflater;
inflater.Inflate(Resource.Menu.menu, menu);
_editMenu = menu.FindItem(Resource.Id.action_add);
return true;
}

The issue directly relates to this:
1) You need to define app: correctly to xmlns:app="http://schemas.android.com/apk/res-auto" instead of xmlns:app="http://schemas.android.com/apk/lib-auto"
2) You need to change the word app:layout_behaviour to app:layout_behavior
Feel free to edit your original question with the respective code so this answer makes more sense! (As more source code was revealed in a PM)

Related

AppBarLayout/Toolbar not showing (height = 0)

I am developing an app to display details about movie/tv shows. When opening the movie from search results it opens correctly. Then I add the movie to my collection, turns to MainActivity where all my movies are listed. Selecting the same movie, but now the entire AppBarLayout is hidden! I don't get any exceptions of any kind, in debug I see that it is visible, but has height of 0. Why is this happening?
I have a method that collapses the CollapsingToolbarLayout if no banner image is found, but this is not the case here. It should anyhow have a toolbar with title when collapsed, and expand on scroll.
Layout of DetailActivity:
<android.support.design.widget.CoordinatorLayout 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:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.NoActionBar"
tools:context=".activites.DetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="240dp"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_behavior="#string/appbar_spring_behavior">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="60dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NestedFixFlingScrollView
[...]
Some relevant setup methods in DetailActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
movieChanges = InterfaceHolder.movieChanges;
findNodes();
setupToolbar();
setupCurrentMovie();
setupImageGallery();
setupFAB();
displayDetails();
fetchTmdbDetails();
}
private void setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
getWindow().setStatusBarColor(Color.TRANSPARENT);
setTitleWhenCollapsed();
}
private void setupCurrentMovie() {
movie = new Gson().fromJson(getIntent().getStringExtra(EXTRA_MOVIE), Movie.class);
if (movie == null) {
Toast.makeText(this, R.string.movie_not_found, Toast.LENGTH_SHORT).show();
finish();
}
}
[...]
Starting DetailActivity from search result:
holder.listItemContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (resultModel.searchResult.isTvShow() || resultModel.searchResult.isMovie()) {
final Movie movie = new Movie();
movie.setTitle(resultModel.displayText);
movie.setYear(resultModel.year);
movie.setFormat(MovieFormat.BLU_RAY);
movie.setType(MediaType.from(resultModel.mediaType));
movie.setPosterPath(resultModel.posterUrl);
movie.setTmdbId(resultModel.searchResult.getId());
final Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_MOVIE, new Gson().toJson(movie));
context.startActivity(intent);
}
}
});
Starting from MainActivity:
final Movie movie = movieList().get(position);
final Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_MOVIE, new Gson().toJson(movie));
startActivity(intent);
styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
styles.xml-21:
<resources>
<style name="AppTheme21" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
Both the SearchActivitiy and DetailActivity uses AppTheme.NoActionBar. MainActivity uses AppTheme. I now noticed that when opening a movie from SearchActivity it is showing correctly, but if I press back (to search) and then opens the same movie again, it does not show correctly.
I had some suspicion that it whould be this line in the SearchActivity that caused the trouble, but it was not:
getSupportActionBar().hide();
It seems that it is only after using the SearchActivity that the DetailActivity does not show correctly. All of my other example data in MainActivity opens correctly in DetailActivity, until i have used the SearchActivity. Even some of the movies that I have not searched for.
Sorry if this question grew to be to long/detailed, but I often see that people are asking for more details, so I chose to include as much relevant info as I thought was necessary.
Please let me know if you need any more info?
I figured it out!
I am using Ion for image loading, and had a callback method "collapseAppBarOnError":
private FutureCallback<ImageView> collapseAppBarOnError() {
return new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView result) {
stopLoading();
if (e != null && !(e instanceof CancellationException)) {//TODO: Denne vil ikke funke om errorDrawable ligger i cache?
collapse();
} else if (result != null && result.getDrawable() != null && !result.getDrawable().equals(getDrawable(R.drawable.ic_movie_white_48dp))) {
expand();
}
}
};
}
It was the "else if" in the callback method that caused the trouble. Sorry, for this was impossible for you guys to see when I had not postet all my code.
It turns out that it actually had nothing to do with the SearchActivity, the error was also happening if I opened a movie I already had in my collection two times or more, even if not using the search! It worked correctly the first time, but not the following times.

How to add spinner to ActionBar?

In the log, I have
NullPointerException : Attempt to invoke virtual method 'void
android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null
object reference.
Here is my following code :
public class MatchesActivity extends Activity implements ActionBar.OnNavigationListener {
private ActionBar actionBar;
private ArrayList<SpinnerNavItem> navSpinner;
private TitleNavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matches);
actionBar=getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Botola Pro",R.drawable.ic_menu_camera));
navSpinner.add(new SpinnerNavItem("Coupe du trone",R.drawable.ic_menu_camera));
adapter = new TitleNavigationAdapter(getApplicationContext(),navSpinner);
actionBar.setListNavigationCallbacks(adapter,this);
}
#Override
public boolean onNavigationItemSelected(int itemPosition, long itemId){
return false;
}
}
here is my styles.xml and my stylesv21.xml :
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
</resources>
To get you started, here's some pointers. Toolbar is the new ActionBar. You can define any custom Toolbar layout you want from XML.
For a Spinner, for example, save this as toolbar_spinner.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark"
>
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
Now, you can simply include that Toolbar layout into your Activity. (Make sure that Activity uses a Theme without a Toolbar like Theme.AppCompat.NoActionBar)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_spinner" />
<!-- Activity content here -->
</LinearLayout>
Then, you can start off your Activity class like so
public class SpinToolbarActivity extends AppCompatActivity {
private Toolbar mToolbar;
private Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spintoolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mSpinner = (Spinner) findViewById(R.id.spinner_nav);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
// TODO: add items to mSpinner using an Adapter
}
}

ActionBar doesn't show in Android KitKat 4.4.4

I'm developing an app in Android and I'm having problem with implementing the ActionBar in a Fragment.
Here's the code of my Fragment:
public class TopicListFragment extends ListFragment {
private LinkedList<Topic> mTopics;
private static final String TAG = "TopicListFragment";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mTopics = TopicLab.get(getActivity()).getTopics();
ArrayAdapter<Topic> adapter = new TopicAdapter(mTopics);
setListAdapter(adapter);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "Menu created");
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_topic_list, menu);
}
And here's the XML file for the meny layout:
<?xml version="1.0" encoding="utf-8"?>
<menu 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" >
<item android:id="#+id/menu_item_new_topic"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/new_topic"
app:showAsAction="ifRoom"></item>
</menu>
When I run the app the ActionBar doesn't show and if I press the menĂ¹ button from my smartphone, it appears in an ugly botton line with only the title.
As you can see in the Fragment class, I have inserted a line of debug, but it print the message only when I press the menĂ¹ button but not when the Activity is created.
What can I do to fix it? Every answer will be really appreciated! Thank you!
EDIT----
As asked this is the styles.xml file
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Put this code in xml file
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
And below code in activity which holds fragment
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
And in your style add
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Long time after, here is a safe option if you getting a gap in the actionbar testing with the old versions:
final RelativeLayout mainLayout = view.findViewById(R.id.main_layout);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Rect rectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
mainLayout.setPadding(0, rectangle.top, 0, 0);
}

How to prevent the actionMode and toolbar to "jump" when toggling between them?

Background
I have an activity in my app that has a toolbar as the actionBar, and it also has an actionMode, for multi-selection of items.
The problem
Every time I close the actionMode, there is a "jump" between the two modes, so I can see both the toolbar and the actionMode.
Maybe I'm just doing it wrong, but I remember it worked fine in the past.
Here's how it looks like using the snippet code I've made:
What I've tried
This is a snippet of the code I've used. To test it, run the app, wait a moment for the actionMode to appear, and then either press the back button, or press the button on the actionMode. Do note that all classes that I use are of the support library (when available).
MainActivity.java
public class MainActivity extends AppCompatActivity {
protected ActionMode.Callback _actionModeCallback;
protected ActionMode _actionMode;
Toolbar _toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_toolbar = (Toolbar) findViewById(R.id.activity_app_list__toolbar);
setSupportActionBar(_toolbar);
_actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
return false;
}
#Override
public void onDestroyActionMode(final ActionMode mode) {
_toolbar.setVisibility(View.VISIBLE);
_actionMode = null;
}
#Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
_toolbar.setVisibility(View.GONE);
return true;
}
#Override
public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
mode.finish();
return true;
}
};
//
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
_actionMode = startSupportActionMode(_actionModeCallback);
}
}, 2000);
}
}
activity_main.xml
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/activity_app_list__toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:colorControlNormal="?attr/colorControlNormal"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:ignore="UnusedAttribute"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"/>
</FrameLayout>
</LinearLayout>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->.
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
<item name="colorPrimary">#FF0288D1</item>
</style>
</resources>
The question
Why is this happenning? How can I fix it?
Is this a known bug, perhaps?
What you're looking for is the ACTION_MODE_OVERLAY flag. In your Activity.onCreate() method, add the following before the call to super.onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
super.onCreate(savedInstanceState);
// other stuff...
}
The same as described by #Kevin Coppock can also be achieved by adding <item name="windowActionModeOverlay">true</item> in your AppTheme.
The theme could look like this:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->.
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
<item name="colorPrimary">#FF0288D1</item>
<item name="windowActionModeOverlay">true</item>
</style>

How to add ToolBar in PreferenceActivity?

I want to add ToolBar in PreferenceActivity in my android application. I wrote the following code.
public class SettingsActivity extends PreferenceActivity {
SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
root.addView(bar, 0);
bar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
This worked perfectly in my android Kitkat phone API 19 but force closed in API level 10 i.e. gingerbread. Please suggest me.
You need a layout that contains a Toolbar, and a ListView with android:id="#android:id/list"
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/content_frame"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
addPreferencesFromResource(R.xml.preferences);
...
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
#Override
public void setContentView(#LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
#Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
#Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
#Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
private void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
...
}
Check out my fully working example:
activity_settings.xml
SettingsActivity.java
Reference from the Android team: AppCompatPreferenceActivity
Try:
public class SettingsActivity extends AppCompatPreferenceActivity {
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
/* getFragmentManager().beginTransaction()
.replace(android.R.id.content, new GeneralPreferenceFragment())
.commit();
*/
//addPreferencesFromResource(R.xml.pref_general);
}
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
app_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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>
I'm late but here's a small fix to the problem instead of writing tons of code or adding bulky libraries.
Go to AndroidManifest.xml and to your Preference Activity add the custom theme
<activity
android:name=".Dashboard.PreferencepActivity"
android:label="#string/title_activity_preference"
android:theme="#style/Pref"></activity>
Here's the custom theme added to the above #style/Pref
<style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Hope it helps!
You can easily add toolbar from #android:style/Theme.Material.Light.DarkActionBar
In AndroidManifest.xml :
<activity
android:name=".activity.SettingsActivity"
android:theme="#style/SettingsTheme"
android:label="Settings"/>
In v21/styles.xml
<style name="SettingsTheme" parent="#android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">#color/colorPrimary</item>
<item name="android:colorPrimaryDark">#color/colorPrimaryDark</item>
In v14/styles.xml for Back API support
<style name="SettingsTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar.V14.Movie.NoTitle</item>
Very easy and working well in my case by inflating custom toolbar.
In your java code do as below,
public class SettingsPrefActivity extends AppCompatPreferenceActivity {
// private static final String TAG = SettingsPrefActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting up toolbar
getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings");
setSupportActionBars(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// load settings fragment
getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
}
}
and at your xml side code,add one preference category at the top do as below,
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
<PreferenceCategory android:layout="#layout/toolbar_setting"></PreferenceCategory>
and in your layout resource folder toolbar_setting,
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="#dimen/appbar_elevation"
android:minHeight="?attr/actionBarSize"
android:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
This method will not solve adding toolbar, but you can get the default ActionBar back for your settings page. Create a theme for settings activity, by inheriting from parent theme.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Settings" parent="AppTheme">
<item name="windowNoTitle">false</item>
<item name="windowActionBar">true</item>
</style>
In AndroidManifest.xml set the android:theme
<activity
android:name=".Settings"
android:theme="#style/AppTheme.Settings" />
That is all.
Instead of:
public class PreferencesActivity extends Activity
Do this:
public class PreferencesActivity extends AppCompatActivity

Categories

Resources