Friends I have created a listpreferences screen ,and it is working fine but I want to change the color of text appearing on the list and the RadioGroup items. So can anyone help me out.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:entries="#array/gametype"
android:entryValues="#array/gametype"
android:key="listpref"
android:summary=""
android:title="Set Game Level" />
</PreferenceScreen>
This is my preferences screen.
After Clicking on listpreferences item it show a dialog box which has three options
so i want to change the color of that radiobutton text and the list item text.
here is java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences sp=getPreferenceScreen().getSharedPreferences();
Preference pref=findPreference("listpref");
ListPreference lp=(ListPreference)pref;
pref.setSummary(lp.getValue());
sp.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Preference pref=findPreference(key);
ListPreference lp=(ListPreference)pref;
pref.setSummary(lp.getValue());
}
public void setcolor(int position){
for(int i=0;i<4;i++)
{
if(i==position)
{
list.getChildAt(i).setBackgroundColor(Color.rgb(238, 180, 180));
}
else{
list.getChildAt(i).setBackgroundColor(Color.WHITE);
}
}
Try like this ;
RadioGroup.setTextColor(Color.GREEN);
This is good solution
If you want to keep a consistent colouring scheme in the whole application I'd suggest learning about adding a theme or styles. This should get you started http://developer.android.com/guide/topics/ui/themes.html.
Use Html.fromHtml to style your text.
mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" + mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" + mPodFolderPref.getSummary() + "</font>"));
Html.fromHtml can do a lot for you.
Mal
Related
I want to change text color of all view of PreferenceActivity when a SwitchPreference state changes.
prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:summary="Settings"
android:title="Settings" >
<SwitchPreference
android:key="NightMode"
android:summary="dark and light"
android:title="Night Mode" />
...
</PreferenceCategory>
</PreferenceScreen>
PrefsActivity.java
public class PrefsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
boolean nightMode = sharedPreferences.getBoolean("NightMode", false);
if(nightMode){
//I want to change text color here
}
}
}
I can change backgroundColor of layout this way:
getListView().setBackgroundColor(ContextCompat.getColor(this , R.color.colorBackground));
but how to change text color?
The way to change the text color is to call textviewinstance.setTextColor(color). You'd need to do that to every view independently though.
The best way to do this is to use the android theme system. If you set the theme before calling setContentView, all those views will be created with that theme. Make a theme with the text color you want for nightmode. When nightmode is selected, call recreate() to kill your activity and start a new one, just like what happens on configuration changes. Then in onCreate of the new instance, check if night mode is on and select your theme before setting the content view by calling setTheme(theme).
So I have EditText in my Preferences and i want to get the text it and use it as String.Then i want to use that string everytime when my button is clicked and it will set my another EditText text to the string from Preference. Codes :
<EditTextPreference android:title="Uredi Text"
android:key="ime"
android:summary="ime pjesme"
/>
Java code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String b1t = preferences.getString("ime", "DEFAULT");
et1 = (EditText) findViewById(R.id.editText1);
//Button Click
public void button(View view) {
et1.setText(b1t);
}
According to the developer docs, you can just use edittextpreference.getText().toString()
http://developer.android.com/reference/android/preference/EditTextPreference.html
I'm not sure where your error is so I'll just give you my code for doing this.
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getBaseContext();
String string = sharedPreferences
.getString("key", "default");
And in my preferences
<EditTextPreference
android:defaultValue="default"
android:key="key"
android:summary="I'm a summary"
android:title="I'm a title" />
For the onCLick method to modify the edit text
EditText editText = (EditText)
findViewById(R.id.editText);
editText.setText(String.valueOf(string));
This code works. If your application still crashes then your issue is either with your onClick event or something else. I personally prefer onClickListeners for buttons.
Heres the code for an onClickListener.
Button button = (Button)
findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
//Do some awesome stuff
}
});
edit: John posted his answer before mine, and his answer reflects most use cases, such as in a standard Activity. My answer is directed solely at the use of a PreferenceActivity.
Here's how you can add Preferences with defaults based on another Preference within a PreferenceActivity. Sorry if there are any errors, I'm typing on my phone.
Your XML-defined Preferences will have their values saved to SharedPreferences automatically, but code-created Preferences will need to be saved/persisted manually.
PreferenceActivity's onConfigurationChange() usually saves the state of code-created preferences (nested PreferenceScreens are a bit strange.)
And, of course, you'll want to do null checks (omitted to save my thumbs.)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Preferences"
android:key="root">
<EditTextPreference android:title="Default Pref Text"
android:key="ime"
android:summary="ime pjesme"
/>
<Preference android:title="Click to add a preference."
android:key="add"
android:summary=""
/>
</PreferenceScreen>
MyActivity.class
public class MyActivity extends PreferenceActivity {
public void onCreate(Bundle inState) {
super.onCreate(inState);
setContentView(R.layout.activity_main);
getPreferenceScreen.findPreference(PREFERENCE_BUTTON).
setOnPreferenceClickListener(new OnNewPrefClickListener());
}
private class OnNewPrefClickListener implements OnPreferenceClickListener {
public boolean onPreferenceClick(Preference preference) {
PreferenceScreen rootScreen = getPreferenceScreen();
EditTextPreference defaultPref =
(EditTextPreference)rootScreen.findPreference("ime");
String defaultText = defaultPref.getText();
EditTextPreference newPref = new EditTextPreference(getApplicationContext());
newPref.setText(defaultText);
rootScreen.addPreference(newPref);
return true;
}
}
}
I have a preference screen with an EditTextPreference.
How to set a hint
either in xml like
android:hint
or in code like
setHint(int), setHint(CharSequence hint)
on the EditTextPreference like on an EditText field?
I assumed that it´s like on the EditText but i didn´t find anything like this.
Thanks.
It's exactly the same to set a hint in XML for an EditTextPreference as it is for an EditText:
android:hint="This is my hint"
This gets passed through to the EditText in the dialog.
This behaviour is confirmed here where it says "It is a subclass of DialogPreference and shows the EditText in a dialog. This EditText can be modified either programmatically via getEditText(), or through XML by setting any EditText attributes on the EditTextPreference."
this is way better, as the accepted answer will not set a hint on the edittext but will set the text to
the EditText (long live the small differene).
To really set a hint, use this code:
YourEditTextPreference.getEditText().setHint(R.string.some_hint);
you may also consider adding a summary so the preference will not be displayed with an empty summary
If you are using the AndroidX preference library, you can assign a hint using an OnBindEditTextListener in your PreferenceFragmentCompat:
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceManager manager = getPreferenceManager();
EditTextPreference textPref = manager.findPreference("myTextPreference");
textPref.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
editText.setHint(R.string.hint);
}
});
}
When the input field is empty, the hint should then be displayed as expected.
Kotlin version (AndroidX preference library):
val editTextPref = findPreference<Preference>("prefKey") as EditTextPreference
editTextPref.setOnBindEditTextListener {
it.hint = "My hint"
}
Works in emulator with Android 10 (API level 29).
At the same time, the hint specified in xml does not work:
<EditTextPreference
android:key="prefKey"
android:title="Title"
android:hint="My hint"/>
Use android:summary to give a brief description of your preference.
You can also set this dynamically using setSummary(CharSequence summary) .
EDIT: for giving a 'hint' you could use android:defaultValue .
the way to set "Hints" is to use a summary provider as described in
https://developer.android.com/guide/topics/ui/settings/customize-your-settings
the idea is to print a default/hint when the value is null or empty - otherwise, just print out the preference value
val dobPreference: EditTextPreference? = findPreference("key_dob")
dobPreference?.summaryProvider = SummaryProvider<EditTextPreference> { pref ->
if (pref.text.isNullOrBlank()) {
"e.g. Enter your DOB in DDMMYYYY format" // the hint if pref is empty
} else {
pref.text // your preference value if it is not empty.
}
}
I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked.
i.e. exactly opposite of the android:dependancy attribute.
How can I do that?
Yes it's possible to do this out of the box.
Let's say you want to disable pref2 when pref1 is off
Here's the code(preference xml layout) to put in for pref1:
<CheckBoxPreference
android:title="Pref1"
android:key="pref1">
</CheckBoxPreference>
Here's the code(preference xml layout) to put in for pref2:
<EditTextPreference
android:dialogMessage="Pref 2 dialog"
android:title="Pref2"
android:key="pref2"
android:dependency="pref1">
</EditTextPreference>
Like sigmazero13 said, by default disableDependentsState is false so you don't need to include it in the pref1 attributes.
According to the docs here, you can add an attribute to the CheckBoxPreference tag like so:
android:disableDependentsState="true"
By default this is false, which means that the dependents are disabled when the checkbox is unchecked, but by setting it to true, it should have the opposite effect.
I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:
preference1.setOnPreferenceClickListener(pref1_click);
....
private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
preference2.setEnabled(!preference1.isChecked());
return true;
}
}
Android CheckBox??
I am assuming you are using the Android.widget.checkBox:
http://developer.android.com/reference/android/widget/CheckBox.html
Try this
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox1.isChecked()) {
checkBox2.setChecked(false);
}
}
}
GoodLUCK!!
I am testing my application on a Nexus One and i have some problems. My theme is Light and
when an inner sub PreferenceScreen is displayed, the window background
becomes black instead of keeping the PreferenceActivity's one.
<PreferenceScreen android:title="main preferences">
...
<PreferenceScreen android:title="sub screen">
</PreferenceScreen>
</PreferenceScreen>
What is the problem?
Wouter
Use this:
Create theme in style.xml file
<style name="Theme.SettingsBackground" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowBackground">#android:color/black</item>
</style>
and then in manifest file use:
<activity android:name=".Settings" android:theme="#style/Theme.SettingsBackground"></activity>
Do this for all sub activities which you want.
To best understand what is happening here you can refer to this piece of code from the source code for the PreferenceScreen class:
#Override
protected void onClick() {
if (getIntent() != null || getPreferenceCount() == 0) {
return;
}
showDialog(null);
}
private void showDialog(Bundle state) {
Context context = getContext();
ListView listView = new ListView(context);
bind(listView);
// Set the title bar if title is available, else no title bar
final CharSequence title = getTitle();
Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
? com.android.internal.R.style.Theme_NoTitleBar
: com.android.internal.R.style.Theme);
dialog.setContentView(listView);
if (!TextUtils.isEmpty(title)) {
dialog.setTitle(title);
}
dialog.setOnDismissListener(this);
if (state != null) {
dialog.onRestoreInstanceState(state);
}
// Add the screen to the list of preferences screens opened as dialogs
getPreferenceManager().addPreferencesScreen(dialog);
dialog.show();
}
The way that I work around it is to set the parent background color by overriding onCreateView in the first preference added to the preference screen. Of course this requires some custom code but it's not terribly complicated, for instance to set a white background:
package com.justinbuser.livewallpapers;
import android.preference.PreferenceCategory;
public class VideoChooserPreferenceCategory extends PreferenceCategory{
public VideoChooserPreferenceCategory(Context context) {
super(context);
}
#Override
protected View onCreateView(ViewGroup parent)
{
parent.setBackgroundColor(0xFFFFFFFF);
return super.onCreateView(parent);
}
}
You would then of course need to use that custom category by altering your xml, i.e.:
<PreferenceScreen android:title="main preferences">
<PreferenceScreen android:title="sub screen">
<com.justinbuser.livewallpapers.VideoChooserPreferenceCategory android:title="sub screen category" />
</PreferenceScreen>
</PreferenceScreen>
Also, if you notice the android PreferenceScreen changes the theme based on whether or not a title is set, i.e. if a title exists it enables a theme that includes the title bar. So if you want no title bar you should avoid setting the preferencescreen title and set it statically in xml or dynamically through code.
Have you tried this?
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference){
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
return false;
}
Add this method in your PreferenceActivity.
At comment #35 from this source.