Preference Screen with custom views

Quickly share your Android Code Snippets here...

Preference Screen with custom views

Postby MOnline » Sat Oct 17, 2009 10:40 am

I was creating a preference screen in code and wanted to add my own custom title bar. Its quite easy if you know how ;).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.testPreference;
  2.  
  3.  
  4.  
  5. import android.content.SharedPreferences;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.preference.CheckBoxPreference;
  10.  
  11. import android.preference.PreferenceActivity;
  12.  
  13. import android.preference.PreferenceCategory;
  14.  
  15. import android.preference.PreferenceManager;
  16.  
  17. import android.preference.PreferenceScreen;
  18.  
  19. import android.view.Window;
  20.  
  21. import android.view.ViewGroup.LayoutParams;
  22.  
  23. import android.widget.LinearLayout;
  24.  
  25. import android.widget.ListView;
  26.  
  27. import android.widget.TextView;
  28.  
  29.  
  30.  
  31. public class preferences extends PreferenceActivity {
  32.  
  33.     private LinearLayout rootView;
  34.  
  35.     private LinearLayout titleView;
  36.  
  37.     private ListView preferenceView;
  38.  
  39.     private TextView textView;
  40.  
  41.    
  42.  
  43.     @Override
  44.  
  45.     protected void onCreate(Bundle savedInstanceState) {
  46.  
  47.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  48.  
  49.         super.onCreate(savedInstanceState);
  50.  
  51.  
  52.  
  53.         rootView = new LinearLayout(this);
  54.  
  55.         rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  56.  
  57.         rootView.setOrientation(LinearLayout.VERTICAL);
  58.  
  59.        
  60.  
  61.         textView = new TextView(this);
  62.  
  63.         textView.setText(R.string.app_name);
  64.  
  65.        
  66.  
  67.         titleView = new LinearLayout(this);
  68.  
  69.         titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 26));
  70.  
  71.         titleView.setBackgroundResource(R.drawable.bar_back);
  72.  
  73.         titleView.addView(textView);
  74.  
  75.        
  76.  
  77.         preferenceView = new ListView(this);
  78.  
  79.         preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  80.  
  81.         preferenceView.setId(android.R.id.list);
  82.  
  83.        
  84.  
  85.         PreferenceScreen screen = createPreferenceHierarchy();
  86.  
  87.         screen.bind(preferenceView);
  88.  
  89.         preferenceView.setAdapter(screen.getRootAdapter());
  90.  
  91.        
  92.  
  93.         rootView.addView(titleView);
  94.  
  95.         rootView.addView(preferenceView);
  96.  
  97.        
  98.  
  99.         this.setContentView(rootView);
  100.  
  101.         setPreferenceScreen(screen);
  102.  
  103.     }
  104.  
  105.  
  106.  
  107.     private PreferenceScreen createPreferenceHierarchy() {
  108.  
  109.         // Root
  110.  
  111.         PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
  112.  
  113.         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  114.  
  115.        
  116.  
  117.         // Inline preferences
  118.  
  119.         PreferenceCategory cat1 = new PreferenceCategory(this);
  120.  
  121.         cat1.setTitle("Cat 1");
  122.  
  123.         root.addPreference(cat1);
  124.  
  125.  
  126.  
  127.         for(int i = 0; i < 5; i ++)
  128.  
  129.         {
  130.  
  131.             // Toggle preference
  132.  
  133.             CheckBoxPreference togglePref = new CheckBoxPreference(this);
  134.  
  135.             togglePref.setKey("toggle_check" + Integer.toString(i));
  136.  
  137.             togglePref.setTitle("Toggle Me");
  138.  
  139.             togglePref.setChecked(prefs.getBoolean("toggle_check" + Integer.toString(i), false));
  140.  
  141.             togglePref.setSummary("Checkbox " + Integer.toString(i));
  142.  
  143.             cat1.addPreference(togglePref);
  144.  
  145.         }
  146.  
  147.        
  148.  
  149.      // Inline preferences
  150.  
  151.         PreferenceCategory cat2 = new PreferenceCategory(this);
  152.  
  153.         cat2.setTitle("Cat 2");
  154.  
  155.         root.addPreference(cat2);
  156.  
  157.  
  158.  
  159.         for(int i = 6; i < 13; i ++)
  160.  
  161.         {
  162.  
  163.             // Toggle preference
  164.  
  165.             CheckBoxPreference togglePref = new CheckBoxPreference(this);
  166.  
  167.             togglePref.setKey("toggle_check" + Integer.toString(i));
  168.  
  169.             togglePref.setTitle("Toggle Me");
  170.  
  171.             togglePref.setChecked(prefs.getBoolean("toggle_check" + Integer.toString(i), false));
  172.  
  173.             togglePref.setSummary("Checkbox " + Integer.toString(i));
  174.  
  175.             cat2.addPreference(togglePref);
  176.  
  177.         }
  178.  
  179.        
  180.  
  181.         return root;
  182.  
  183.     }
  184.  
  185. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
MOnline
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Sep 21, 2009 9:51 pm

Top

Re: Preference Screen with custom views

Postby Tony_GPR » Sat Apr 23, 2011 5:45 pm

Thanks!
Tony_GPR
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 09, 2011 3:15 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests