No actionBar with app-compat-v21 library in preferenceactivity? - android

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

Related

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");
}
}

java.lang.IllegalStateException Butterknife

I am facing java.lang.IllegalStateException Required view 'splash_text' but I have included it in the xml.
I am using Butterknife to Bind the views.
compile 'com.jakewharton:butterknife:7.0.1'
Xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_light">
<com.CustomTextView
android:id="#+id/splash_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Activity :
#Bind(R.id.splash_text)
CustomTextView mSplashTv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ButterKnife.bind(this);
mSplashTv.setText("Splash");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finishSplash();
}
},3000);
}
Application is crashing at the line mSplashTv.setText("Splash");
Log :
Caused by: java.lang.IllegalStateException: Required view 'splash_text' with ID 2131492944 for field 'mSplashTv' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:12)
at com.sonymix.activities.SplashActivity$$ViewBinder.bind(SplashActivity$$ViewBinder.java:9)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237) 
at com.sonymix.activities.BaseActivity.onCreate(BaseActivity.java:16) 
at com.sonymix.activities.SplashActivity.onCreate(SplashActivity.java:33) 
at android.app.Activity.performCreate(Activity.java:5372) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 
at android.app.ActivityThread.access$700(ActivityThread.java:165) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5455) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
at dalvik.system.NativeStart.main(Native Method) 
Update:
BaseActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
}
Support Library used:
compile 'com.android.support:appcompat-v7:23.1.1'
Remove binding from base activity. When ButterKnife bind in base class, your layout not inflated yet.
Also you can add #Nullable annotation to field.
It's because your layout is not inflated yet. If you are using BaseActivity, do something like this :
public abstract class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(this.getLayoutContentViewID());
ButterKnife.bind(this); //Configure Butterknife
}
public abstract int getLayoutContentViewID();
}
Next, in your MainActivity :
public class MainActivity extends BaseActivity {
#BindView(R.id.main_activity_coordinator_layout) CoordinatorLayout coordinatorLayout;
...
#Override
public int getLayoutContentViewID() { return R.layout.activity_main; }
...
}
Check your support Library version. I was getting the above error since i upgraded support library to 23 version. may be that is causing error.NavigationView get/find header layout

How to replace Sherlock ActionBar in with AppCompat

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

The preferences looks strange ? (shift to right a little, font size larger a little)

I don't know why the location of the item created by DialogPreference would be
shift to right a little, and the font size ... a little larger ?
Preference Image
I have a class NumberPickerPreference which extends DialogPreference.
package com.example
public class NumberPickerPreference extends DialogPreference{
......
}
and a pref_general.xml for the preference
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="#string/header_system">
<!-- extends DialogPreference -->
<com.example.NumberPickerPreference
android:key="#string/key_refresh"
android:title="#string/title_refresh_time"
android:dialogTitle="#string/title_refresh_time"
android:summary="#string/summary_refresh_time"
android:negativeButtonText="#string/ui_cancel"
android:positiveButtonText="#string/ui_ok" />
<CheckBoxPreference
android:title="#string/title_reconnect_unlimit"
android:key="#string/key_reconnect_unlimit"
android:summaryOn="#string/summary_reconnect_unlimit"
android:summaryOff="#string/summary_reconnect_unlimit"
android:disableDependentsState="true" />
</PreferenceCategory>
</PreferenceScreen>
and a SettingsActivity class load the pref_general.xml
public class SettingsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new GeneralPreferenceFragment()).commit();
}
public static class GeneralPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
}
}
}

Using customview from library in project

I have CustomLayout class declared in a library customAndroidLibrary. This CustomLayout extends ViewGroup. Now I want to use this CustomLayout in my layout.xml which is in my project. I have included this library in my project.
CustomLayout class
package com.android.custom;
public class CustomLayout extends ViewGroup {
.....
}
layout.xml
<com.android.custom.CustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/animation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.android.custom.CustomLayout>
MainActivity
package com.android.ui;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
}
}
But it is throwing ClassNotFoundException.
Even it is not detecting my Activity also if I am using the custom view in layout.xml
If I am using simple TextView in layout.xml. Then it is not giving any error.
Solved it on my own.
<com.android.custom.CustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.android.custom.CustomLayout"
android:id="#+id/animation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.android.custom.CustomLayout>

Categories

Resources