how to set the shared preferences value to a layout
I have already put everything about shared preferences in place and in one of my activity I am also able to retrieve the shared preferences values like this in logcat.
String i = prefs.getString("bgColor", "#f2345");
System.out.println(i);
But in this activity I am using a layout like this
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row,c, from, to);
setListAdapter(sca);
where "country_row" is my xml layout file which is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff000099"
android:background="#ffffff80"
开发者_如何学Python android:padding="10dp"
android:textSize="16sp"
android:text="1964"/>
<TextView android:id="@+id/country"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff80"
android:background="#ff000099"
android:padding="10dp"
android:textSize="16sp"
android:text="Sweden"/>
Now using the values that I am already getting from the preferences, I want to change here for example background color or font size being displayed. All I want now is just to imply these shared prefernces values to my layout xml file. How can I do that, I am not being able to do that?
Actually I can get the values from shared preferences like this
boolean i = prefs.getBoolean("fontBold", false);
System.out.println(i);
if (i){
TextView tv = (TextView)findViewById(R.id.year);
tv.setTypeface(null, Typeface.BOLD);//null pointer
}
In the cursor adapter I am already applying a layout country_row.xml. So how can I use the preferences value I am getting to this layout. The boolean value that I am getting from checkbox is correct as I have also printed out to see it. But when I try to do like above it doesn't work and the program crashes saying the null pointer exception.
The thing I am stuck here is....I am getting the correct preferences value but don't know how to use it or apply it to my existing layout...or do i need to make different layout...i am not sure about it.
I think what you want to do is to change some layout parameters dynamically in codes. Generally, you can set attributes to a view(Layout, TextView,EditText,etc.) either by code or by .xml file. Take a layout as example. First add an id attribute to the layout.
<LinearLayoout android:id="@+id/layoutID" .../>
Then
LinearLayout layout=(LinearLayout)findViewById(R.id.layoutID) //get the layout object.
layout.setBackgroundColor (color from your preferences);
Above is the basic idea. Please read SDK document to find mor info.
As like Huang answer, 1) You should have to create the reference for the layout or view to whom you have to change the properties. 2) After that get the preference's value in to respective variable. 3) Set the value to that layout or view to get effect.
It will surly going to work. For more reference see the below Example for the in which selection of checkbox i do some action like to play Music.
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
lessResultSound = myPrefs.getBoolean("LessResultIsOn", false);
System.out.println("============================= The FullResultSound in Result Page is: "+fullResultSound);
System.out.println("============================= The LessResultSound in Result Page is: "+lessResultSound);
if(fullResultSound)
{
playSound(soundFileForFullResult);
}
else
{
playSound();
}
Hope it will works for you. If not then tell me.
Actually how I managed to change the layout of the view was by doing this. ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, sortOrder);
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new MySimpleCursorAdapter(this, R.layout.country_row,
c, from, to);
setListAdapter(sca);
class MySimpleCursorAdapter extends SimpleCursorAdapter{
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
@Override // Called when updating the ListView
public View getView(int position, View convertView, ViewGroup parent) {
/* Reuse super handling ==> A TextView from R.layout.list_item */
View v = super.getView(position,convertView,parent);
TextView tYear = (TextView) v.findViewById(R.id.year);
TextView tCountry = (TextView) v.findViewById(R.id.country);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean font_size = prefs.getBoolean("fontSize", false);
boolean italic_font = prefs.getBoolean("fontItalic", false);
String listpref = prefs.getString("bgColor", "#ffffff80");
//System.out.println(listpref);
tYear.setBackgroundColor(Color.parseColor(listpref));
tCountry.setBackgroundColor(Color.parseColor(listpref));
if (font_size){
tYear.setTextSize(25);
tCountry.setTextSize(25);
}
if (italic_font){
tYear.setTypeface(null, Typeface.ITALIC);
tCountry.setTypeface(null, Typeface.ITALIC);
}
//tv.setBackgroundColor(col);
return v;
}
}
精彩评论