Changing the Background of a Specific Preference Item is not working - android

I have a preference, to be used as settings of my application. I want to change the background color of a particular item in listpref (listview). I used the code below:
Drawable background = getResources().getDrawable(R.drawable.bg_barcode_des);
addPreferencesFromResource(R.xml.admin_preferences);
Preference credentials = findPreference("demo");
View credentialsView = credentials.getView(null, null);
credentialsView.setBackgroundDrawable(background);
//**OR** credentialsView.setBackground(background);
//**OR** credentialsView.setBackgroundColor(Color.RED);
//Non of these or working...
But nothing change in my preference activity. Any correction that I need to do???

Related

Changing Preference title text color the correct way

I want a text of a title of a preference to change its color to white when another preference (a SwitchPreference) is changed. When the other preference is changed I'm invoking this method from the onPreferenceChange listener:
private void enableSettingsGui(boolean enable)
{
View preferenceView = getListView().getChildAt(myPref.getOrder());
TextView title = (TextView)preferenceView.findViewById(android.R.id.title);
title.setTextColor(getResources().getColor(R.color.white));
}
Now there are 2 scenarios:
1) If I'm returning true in the onPreferenceChange method, the switch of the SwitchPreference is changed correctly (meaning sliding left or right as to its new state of enabled or disabled), but the color of the title of the affected preference is not changed.
2)If I'm returning false from the onPreferenceChange, the switch does not change, however the title text color suddenly does change.
How can I do it correctly?
I have figured out the problem.
According the documentation regarding onPreferenceChange, the return value is
"True to update the state of the Preference with the new value. "
That means that the switch changed because the return value told him to update the state. The color seemed like it did not set, but it actually did, just that in order to change the state, the onCreate method of the preference was called again, thus overriding the color change.
What I did was to override the setEnabled method like this:
#Override
public void setEnabled(boolean enabled)
{
super.setEnabled(enabled);
shouldEnable = enabled;
}
And then in onCreate added the line: setTitleTextColor(shouldEnable); which color the text according to the enabled state. This way the changes were made AFTER the view was recreated and the color remained.

retrieve a default preference key

I am trying to retrieve a default value (which is the last value saved)
I am making some changes in the background color of the layout
so before the setContentView
I put this code in.
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
setTheme(preferences.getInt("color",R.style.AppTheme));
But it won't work, the R.style.Apptheme's background color (which I set in the style.xml) works but I have changed the bg's color with this code
SharedPreferences preferences = getSharedPreferences("UID",MODE_PRIVATE);
preferences.edit().putInt("color",R.style.red_theme).commit();
finish();
startActivity(getIntent());
what is the proper way of retrieving the last saved style?

How to use findPreference outside of PreferenceActivity?

I'm trying to gain access to a (custom) list preference and change its selected value (i.e. if index 3 is selected, change the selection to index 1). However, findPreference() can only be used inside a PreferenceActivity. I need to access the preference and change its selection inside a regular Activity. Is there a way this can be done? I don't see anything in SharedPreferences that I can use to change the selection, only a list preferences value.
This is how you do it,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference(this);
prefs.getString(PREF_KEY, "default");

Changing default xml image on application load

I have an android application that displays, by default, a series of ImageButtons using xml. The user changes the image based on their input. I am trying to display the changed ImageButtons the next time the user loads the app.
Example:
ImageButton starts as Android.png (loaded from default xml page)
User enters text
ImageButton is changed to Correct.png
The next time that the app loads I want Correct.png to display instead of Android.png. Is there a way to iterate through the ImageButtons before the app starts (the buttons are NOT created programatically) to set the source value for each one before the application loads?
You can use findViewById(R.id.button_1) for each button and set it as needed. You'll then need to store which values it should have so that you can load these values and set the ImageButton source for each one. You can store it with either SharedPreferences or a SQLite database, depending on how much you have to change.
So, for example:
ImageButton b = (ImageButton)findViewById(R.id.button_1);
b.setImageDrawable(drawable);
You can load a drawable up for the checkbox so that you do it only once, which is more efficient, or you could setImageResource to R.drawable.correct, which would be a bit slower since I assume that the checkbox would probably be set for multiple images.
A better way to perform this by using SharedPreferences.
SharedPreferences sharedPref = getSharedPreferences("userpref", Activity.MODE_PRIVATE);
String image = pref.getString("userchoice","no-image");
if(image.equeals("no-image")
{
imageView.setBackgroundResource(R.drawable.android);
}
else
{
//compare here your image
imageView.setBackgroundResource(R.drawable.Correct);
}
And whenever user selects any other image, just edit your preference file by using following code.
SharedPreferences.Editor editor = pref.edit();
editor.putString("userchoice","Correct.png");

Android: Is there a way to show DialogPreference from code?

I want to open some preferences (which are made by extending DialogPreference) on first app startup. Also, these preferences are used as usual preferences.
Is there a way of accomplishing this?
EDIT:
I have my custom preference, made like this:
public class CitySelectPreference extends DialogPreference {
// Some code here
}
And as the solution I want it to be shown from the code, without the need of user getting to preference screen.
Just do this :
CitySelectPreference preference = (CitySelectPreference) findPreference("city_pref_key")
//You have to set a key to yout PreferenceScreen
PreferenceScreen screen = (PreferenceScreen) findPreference("screen_pref_key");
//Retrieve the index of the preference in preferenceScreen
int index = preference.getOrder();
//Perform a click
screen.onItemClick(null, null, index, 0);

Categories

Resources