Preference Page in Android

Few days ago I encountered a problem while developing an Android Application. My goal was to get a list of all system ringtones with the user sounds, stored on SD-card. I find some information about it in the web, by the full solution came to me only after some hours of work. To start the work on these project we will create an application with a blank activity. The next step is to add a button to show new Activity with the ringtone preferences. Now, navigate to your activity_main.xml which is situated in the res/layout folder. Delete the element TextView. You need to delete this piece of code:
  1.     <TextView
  2.         android:id="@+id/textView1"
  3.         android:layout_width="wrap_content"
  4.         android:layout_height="wrap_content"
  5.         android:text="@string/hello_world" />
After this, let's set the background of your activity. For this scope, please, add this line to the RelativeLayout element:
  1. android:background="#000000"
Now, you can add the button. The button can be added to the Relative Layout in this way:
  1.         android:id="@+id/show_music"
  2.         android:layout_width="fill_parent"
  3.         android:layout_height="wrap_content"
  4.         android:layout_alignParentLeft="true"
  5.         android:layout_alignParentRight="true"
  6.         android:layout_alignParentTop="true"
  7.         android:text="Button" />
Notice, that id is assigned to this button:
  1. android:id="@+id/show_music"
We will use this attribute letter. The next step is to handle click event on this button. For this scope, we will use the id of the button and findviewById(int id) method. Open MainActivity.java and add a private member to the MainActivity class:
  1. private Button select;
After this we can
link
this button with the button from the xml file. It's done using button id:
  1. select = (Button)findViewById(R.id.show_music);
  2.                 select.setOnClickListener(this);
Setting the current class to be click listener for this button forces as to implement the interface OnClickListener. At this moment, activity class looks in the following way:
  1. package com.example.testlist;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends Activity implements OnClickListener {
  10.         private Button select;
  11.         @Override
  12.         protected void onCreate(Bundle savedInstanceState) {
  13.                 super.onCreate(savedInstanceState);
  14.                 setContentView(R.layout.activity_main);
  15.                 select = (Button)findViewById(R.id.show_music);
  16.                 select.setOnClickListener(this);
  17.         }
  18.         @Override
  19.         public void onClick(View v) {
  20.                
  21.         }
  22. }
Screen We are ready now to start the most interesting part of this project: the implementation of preferences option. For this purpose we will use the PreferenceScreen with RingtonePreference We need to add a new activity to the project. So, we need to add a record to the manifest file first:
  1. <activity
  2.     android:name=".SoundSelect"
  3.     android:theme="@android:style/Theme.Black.NoTitleBar"/>
I called my activity SoundSelect. Now, we can add the activity to the project and the layout file, called sound_select.xml to the project. The sound select is corresponding for the selection layout and it looks in this way:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_height="wrap_content"
  4.     android:layout_width="fill_parent"
  5.     android:layout_gravity="center" >
  6.         <PreferenceCategory
  7.                    android:summary="Alarm sound"
  8.                    android:title="Select Sound" >
  9.                           <RingtonePreference
  10.                               android:id="@+id/ringtone"
  11.                              
  12.                               android:showDefault="true"
  13.                                   android:layout_width ="wrap_content"
  14.                                   android:layout_height="wrap_content"
  15.                               android:key="Alarm"
  16.                           android:summary="Alarm"
  17.                   android:ringtoneType="notification|alarm" />
  18.                          </PreferenceCategory>
  19.  
  20. </PreferenceScreen>
The corresponding activity file, that loads the option screen has the following structure:
  1. package com.example.testlist;
  2.  
  3. import android.os.Bundle;
  4. import android.preference.PreferenceActivity;
  5.  
  6.  
  7. public class SoundSelect extends PreferenceActivity{             
  8.         @Override
  9.         protected void onCreate(Bundle savedInstanceState) {
  10.            super.onCreate(savedInstanceState);
  11.            addPreferencesFromResource(R.layout.sound_select);
  12.         }
  13. }
You can see, that just one line is added to the standard declaration of an activity:
  1. addPreferencesFromResource(R.layout.sound_select);
The work is almost done! Now, we come back to the main activity and specify the on click listener for the sound button:
  1. Intent intent = new Intent(MainActivity.this,
  2.                               SoundSelect.class);
  3.                               startActivity(intent);
That's all. Now the result is: RESULT The sound names are shown according to the system locale. That's why you can see Russian letters in the screen.

Comments

Add new comment