Icons in Preference Fragment not adapting to night mode - android

I'm wirting a android app and wanted to make a SettingsActivity.
The problem I have that my vector drawable won't adapt it's color to night or day theme.
ic_palette_black_24dp.xml:
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#color/icon_color_on_surface" android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>
values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="icon_color_on_surface">#000000</color>
</resources>
values-night/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="icon_color_on_surface">#FFFFFF</color>
</resources>
And in my root_preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root_preferences">
<PreferenceCategory android:title="#string/title_preference_design">
<ListPreference
android:entries="#array/preference_list_theme"
android:entryValues="#array/preference_list_theme_values"
android:icon="#drawable/ic_palette_black_24dp"
android:key="list_preference_theme"
android:tint="#color/icon_color_on_surface"
android:tintMode="src_atop"
android:title="#string/preference_category_theme"
app:icon="#drawable/ic_palette_black_24dp" />
</PreferenceCategory>
</PreferenceScreen>
[Edit]
Here also my SettingsActivity.java that is basicly the default by AndroidStudio:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import java.util.ArrayList;
public class SettingsActivity extends AppCompatActivity {
private ColorHelper colorHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Indicate here the XML resource you created above that holds the preferences
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
}
[/EDIT]
As you can see in root_preferences.xml I already tried setting it by android:tint="" and android:tintMode="" but no matter what I do the icon color stayes white.
In other Activities the workaround with android:tint="" does work.
I hope someone can help me with this.
Thanks

The way to go about this is to define a resource attribute to the fillColor. Also, if I recall correctly, the tint that you have defined in your vector's XML is either overriding or has no effect on SVG drawables. My drawables don't have the tint color set, and the fillColor property is dynamic for all drawables.
Below, android:fillColor="?android:attr/textColorSecondary" is defined by Android System and is already handled by Theme.MaterialComponents.DayNight. You can also create your own attr values.
<vector
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="?android:attr/textColorSecondary"
android:pathData="..."
/>
</vector>
Depending on your use case, you can also use textColorPrimary, textColorTertiary from ?android:attr for different shades.

Related

why my app crashes when i want to make a movign background in android studio?

i am making a project in which i want to make a moving gradient background, like the background colour will change after a specific seconds. but when i run my code, my app crashes although there are no errors. here's my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear_layout"
android:background="#drawable/gradient_list"
tools:context=".MainActivity">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
mainActivity.java:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
AnimationDrawable animationDrawable = (AnimationDrawable) linearLayout.getBackground();
animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
}
}
all my gradient files:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/play"
android:duration="4000"/>
<item android:drawable="#drawable/play1"
android:duration="4000"/>
<item android:drawable="#drawable/play2"
android:duration="4000"/>
</animation-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#66ff66"
android:endColor="#ff99cc"
android:angle="225" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#003366"
android:endColor="#66ffcc"
android:angle="135" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffcc00"
android:endColor="#ff99cc"
android:angle="45" />
</shape>
thnx!! in advance.
You have to change this line
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
to
CoordinatorLayout linearLayout = (CoordinatorLayout) findViewById(R.id.linear_layout);
your layout was wrong.

Enter and Exit Animations not working in Dialog Fragment

I have checked all the Stack Overflow Q/A on this, still can't find a solution.
Here are the files:
DialogFragment.java
package app.com.thetechnocafe.mealsquickie.Dialogs;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import app.com.thetechnocafe.mealsquickie.R;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by gurleensethi on 26/01/17.
*/
public class NewCategoryDialog extends DialogFragment {
#BindView(R.id.category_name_text_input_layout)
TextInputLayout mCategoryNameTextInputLayout;
#BindView(R.id.category_name_text_input_edit_text)
TextInputEditText mCategoryNameTextInputEditText;
#BindView(R.id.cancel_button)
Button mCancelButton;
#BindView(R.id.add_button)
Button mAddButton;
private OnAddCategoryListener mListener;
//Interface for callbacks
public interface OnAddCategoryListener {
void onCategoryAdded(String category);
}
//Instance method
public static NewCategoryDialog getInstance() {
return new NewCategoryDialog();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//Inflate the custom dialog
View root = LayoutInflater.from(getContext()).inflate(R.layout.dialog_new_category, container, false);
ButterKnife.bind(this, root);
setEventListeners();
//Set properties
getDialog().requestWindowFeature(STYLE_NO_TITLE);
setCancelable(false);
return root;
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
private void setEventListeners() {
mCancelButton.setOnClickListener(view -> getDialog().dismiss());
mAddButton.setOnClickListener(view -> validateAndSubmitFields());
}
private void validateAndSubmitFields() {
if (mListener != null) {
//Remove all the already existing errors
mCategoryNameTextInputLayout.setErrorEnabled(false);
String category = mCategoryNameTextInputEditText.getText().toString();
if (category.equals("")) {
mCategoryNameTextInputLayout.setError(getString(R.string.category_name_cannot_be_empty));
return;
}
mListener.onCategoryAdded(category);
} else {
Toast.makeText(getContext(), "No Listener attached for adding new category. Please contact the developer.", Toast.LENGTH_SHORT).show();
}
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
return dialog;
}
}
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0%" />
</set>
Showing the dialog:
DialogFragment dialog = NewCategoryDialog.getInstance();
dialog.show(getFragmentManager(), DIALOG_NEW_CATEGORY_TAG);
I have tried both getAttributes().windowAnimations and setWindowAnimations(), and have also tried it putting it in onActivityCreated, onCreateDialog, onCreateView, but it doesn't seem to work.
No matter which the solutions you pick you might have had the same problem as me.
I need to UNINSTALL the game from my development device before installing the new version for the changes to take effect.
I am not sure why but I guess it has to do with the optimized deployment on Android studio not recognizing the changes.
Try it as this.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getDialog().getWindow() != null) {
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation;
}
}
In your onStart of the fragment,
do the following:
// safety check
if (getDialog() == null) {
return;
}
// set the animations to use on showing and hiding the dialog
getDialog().getWindow().setWindowAnimations(
R.style.dialog_animation_slide);
The style set above should be defined like:
<style
name="dialog_animation_slide" >
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slid_down</item>
</style>
Be sure to put your slide_up.xml and slide_down.xml in res/anim directory
Do remove dialog.getWindow().getAttributes().windowAnimations = R.style.DialogSlideFromBottomAnimation; from onCreateDialog()
Let me know if this works for you!!!
If nothing work, maybe the device has animations deactivated from the developer options. restore default options and check.
Animations are a bit tricky when it comes to DialogFragment.
When you need to change the visibility or position of views in your layout, you should include subtle animations to help the user understand how the UI is changing.
So in order to achieve that lets take in mind that DialogFragment is a wrapper for the Dialog class, you should set a theme to your base Dialog to get the animation you want:
public class CustomDialogFragment extends DialogFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Set a theme on the dialog builder constructor!
AlertDialog.Builder builder =
new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
builder
.setTitle( "Your title" )
.setMessage( "Your message" )
.setPositiveButton( "OK" , new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%" />
</set>
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="0%" />
</set>
styles.xml
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/slide_down</item>
<item name="android:windowExitAnimation">#anim/slide_up</item>
</style>
And that's all... Hope it helps. That is thanks to this answer: Show DialogFragment with animation growing from a point

Displaying Item's (Icons) evenly in Toolbar

I want to display all five Icons evenly in my toolbar. But currently there is only one icon displayed on the left hand side of the toolbar, the rest of the space inside the toolbar is empty. I tried to manage the items with a Method called setupEvenlyDistributedToolbar.
I got this Code in MainActivity:
package com.example.timomichel.myapplication;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.Point;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setupEvenlyDistributedToolbar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.items, menu);
return true;
}
public void setupEvenlyDistributedToolbar() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.items);
toolbar.setContentInsetsAbsolute(0, 0);
int childCount = toolbar.getChildCount();
int screenWidth = (width/5);
Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < childCount; i++) {
View childView = toolbar.getChildAt(i);
childView.setLayoutParams(toolbarParams);
}
}
}
My XML file for MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.timomichel.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<include layout="#layout/toolbar" />
</RelativeLayout>
Here is my XML for the toolbar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
</android.support.v7.widget.Toolbar>
And finally the XML for the items in the menu folder:
<?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">
<item
android:id="#+id/profile"
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="profile" />
<item
android:id="#+id/friends"
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="friends"/>
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="maps"/>
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="events" />
<item
app:showAsAction="always"
android:icon="#drawable/android_icon"
android:title="Settings" />
</menu>
Hope you can also help me with this question, thanks a lot!

setCompoundDrawables on drawable shape with no effect

I'm trying to set an overline to a TextView using Compound Drawables.
I've created a line shape in an XML drawable resource which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#00000000" />
</shape>
Now I'm trying to put it as an overline to my TextView like this:
TextView root=(TextView)findViewById(R.id.root);
Drawable background=getResources().getDrawable(R.drawable.overline);
root.setCompoundDrawables(null,background,null,null);
But it completely ignores the instruction and the overline doesn't appear.
I've also considered using CompoundDrawablesWithIntrinsicBounds, but that doesn't work either.
Is the XML for the shape wrong or am I using the method in the wrong way?
Thank you very much in advance!
EDIT: I tried with the XML android:drawableTop property, but still no luck.
At this point I think that there's clearly a problem with the shape itself.
Complete code here:
activity_my.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"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hello"
android:textSize="30sp"
android:drawableTop="#drawable/overline"
android:text="Ciao" />
</RelativeLayout>
overline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#00000000" />
</shape>
My java is the exact same one as the "Hello World" default example given in Android Studio:
package tesi.bordengsberiment;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.TextView;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I had experienced the same problem before. You want to use
root.setCompoundDrawablesWithIntrinsicBounds();

addPreferencesFromResource

I know this might be a duplicate from: What to use instead of "addPreferencesFromResource" in a PreferenceActivity?
But I still can not get the code to work and would appreciate some help. I can not get the:
"addPreferencesFromResource(R.xml.preferences);" to work.
preferences is not resolved, even though it is in the R.xml folder.
I followed the example in the above link to the letter.
Please help!!
package com.example.oasisreference;
import android.R;
import android.R.xml;
//import android.annotation.TargetApi;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
preferences.xml in the Res.xml folder
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="User Name"
android:key="name"
android:summary="Enter your name"
></EditTextPreference>
<CheckBoxPreference
android:title="Euro Currency"
android:defaultValue="false"
android:key="euro"
android:summary="Check for Use Cost Calculator to be in Euros/Liters">
</CheckBoxPreference>
</PreferenceScreen>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/aboutUs"
android:title="About the Author">
</item>
<item
android:id="#+id/preferences"
android:title="Preferences">
</item>
<item
android:id="#+id/exit"
android:title="Exit">
</item>
</menu>
delete import android.R
it will work
The answer is, it's not actually deprecated....
They just want you to use preferenceFragments (see below)
It turns out that it's the same routine name but it is instantiated as part of a preferenceFragment which is a different class. There's it's not deprecated. This is subtle but the examples at the developer website reference it from within a preference fragment. Paste the deprecated call into a preferenceFragment class and see for yourself the deprecation goes away
In the preferenceFragment class declaration:
package android.preference;
public abstract class PreferenceFragment extends android.app.Fragment {
// ...
public void addPreferencesFromIntent(android.content.Intent intent) {
/* compiled code */
}
  
public void addPreferencesFromResource(int preferencesResId) {
/* compiled code */
}
// ...
and in the other class
package android.preference;
public abstract class PreferenceActivity extends android.app.ListActivity implements android.preference.PreferenceFragment.OnPreferenceStartFragmentCallback {
/**
* #deprecated
*/
#java.lang.Deprecated
public void addPreferencesFromResource(int preferencesResId) {
/* compiled code */
}
Add #SuppressWarnings("deprecation") before OnCreate method.

Categories

Resources