0

I have a application in which i have a preference acitivty which loads my settings xml. Now, what i want to do is implement a listview of themes to enable for the application. i know how to setup a listview in the settings xml for the most part, but i dont know how to enable different themes for the application. Can anybody point me in the right direction. or have a little sample code.

Here is my Preference JAVA file, and here below that is my Settings XML.

 public class Prefs extends PreferenceActivity {

ListPreference listPreference;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settingsbasic);
        //New 
        Preference customPref = (Preference) findPreference("clearcache");
        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                                public boolean onPreferenceClick(Preference preference)    {
                                        Toast.makeText(getBaseContext(),
                                                        "Cache Cleared",
                                                        Toast.LENGTH_LONG).show();
                                        WebViewClientDemoActivity.web.clearCache(true);
                                        return false; 

                                }       

I do not have my List view yet in the code, any help would be great on how to implement. I just have a clear cache button.

Here is my settings XML.

   <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference android:key="themes"
            android:title="@string/themes" 
            android:summary="@string/themes_list"
            android:defaultValue="true" />

    <CheckBoxPreference android:key="something"
             android:title="@string/cache" 
             android:summary="@string/somethingelse"
             android:defaultValue="true" />
    <Preference
            android:title="Clear Cache"
            android:summary="Hopefully this Works"
            android:key="clearcache" />

    </PreferenceScreen>

I have 2 check boxes, i want to change the first check box to a list view, and have a array of the list of themes. i just dont know how to enable these themes . Do i create seperate layout files for each theme??? or so i use the themes.xml, if so how do i impletement those by click the object in the listview???

Sorry about all the questions. Any help would be great.

main.xml file, i am wanting my theme to change the contents of this main.xml. most of the attributes will stay the same.

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/boardimage"
  android:fadingEdge="vertical"
  android:orientation="vertical" >

  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/boarder"
   android:contentDescription="@string/title"
   android:gravity="center_horizontal"
   android:src="@drawable/banner" >

 </ImageView> 

  <ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/>

 <WebView
   android:id="@+id/webview01"
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_gravity="center"
   android:layout_weight="1.12" >

  </WebView>



  </LinearLayout>

So i want to know how i would go about changing the contents of this xml file. just as on my blue theme i will use a different drawable for the background or a different size of webview, or a image drawable in the imageview. how would i do that.

Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79

1 Answers1

2

If you want to know about themes and styles, see this link of android developer : http://developer.android.com/guide/topics/ui/themes.html

build two styles and then give the value of setting from prefrence and switch between theme , you can set themes programmatically in your activity with this code : setTheme(R.style.MyTheme);

You can do this with the following code in your themes.xml file in the values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Light">

        <!-- hide the Window Title -->
        <item name="android:windowNoTitle">true</item>

    </style>

</resources>

And you can have a ListPreference with this code and add your string arrays in String.xml :

 <ListPreference
  android:title="List Preference"
  android:summary="Select the option of the list"
  android:key="listPref"
  android:entries="@array/listDisplayWord"
  android:entryValues="@array/listReturnValue" />

This link is useful for you : How to add multiple theme attributes to the same activity on Android Manifest?

You can use a value that give from your listPreference and switch between your layout, like this :

if(myTheme==1)
     setContentView(R.layout.main1);
else
     setContentView(R.layout.main2);

but the name of components(buttons,textViews, EditText,...) in the these layout must same.

Community
  • 1
  • 1
AliSh
  • 10,085
  • 5
  • 44
  • 76
  • So all i want to do use make different versions of my main.xml file. as see above. – Jaison Brooks Jul 24 '12 at 20:36
  • Do i need a styles XML. What i have made so far is a style's xml with 2 styles one for the image view on the main.xml i want to change and the sencond of the layout atributes i want to change of the main xml file. No inside my themes.xml, how do i merge these together as a theme? – Jaison Brooks Jul 25 '12 at 00:03
  • Would the code you provided be in my preference java file or inside my main activity? – Jaison Brooks Jul 25 '12 at 16:08
  • And inside that, i am trying to make it so when the user opens my app depending on what (theme) they set it too inside my preferences, if will update the layout. which i think i get it somewhat with the code you provided other than my question about this one, however do you have any sample code of how i can save there preferences? and how i can ensure the preference is set, i know i can use the OnResume to ensure the main is updated with the theme clicked. however what would the source code look like for that? i appreciate all your response and any more support you give. – Jaison Brooks Jul 25 '12 at 17:07
  • I am still having troubles with figuring this out, how do i ensure the theme that the user selects will be saved? – Jaison Brooks Jul 25 '12 at 21:23
  • What would the code look like for my list preference would it be something like this? `Preference listPref = (ListPreference) findPreference("listPref"); ListviewPref.setOnPreferenceChange(new OnPreferenceChangeEventListener() { if(listviewentry==lighttheme) setContentView(R.layout.main1); else setContentView(R.layout.main2);` – Jaison Brooks Jul 26 '12 at 17:05
  • send an email for me: a.shariat.b@gmail.com , I will send you a sample code from email – AliSh Jul 26 '12 at 17:42
  • AliSh i have sent you a email. – Jaison Brooks Jul 26 '12 at 18:10