• We just launched and are currently in beta. Join us as we build and grow the community.

Preference Page in Android

D4RKC40S

Performance Debugging Pro
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
126
Likes
87
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <

    Button


  2. android:

    id=

    "@+id/show_music"
  3. android:

    layout_width=

    "fill_parent"
  4. android:

    layout_height=

    "wrap_content"
  5. android:

    layout_alignParentLeft=

    "true"
  6. android:

    layout_alignParentRight=

    "true"
  7. android:

    layout_alignParentTop=

    "true"
  8. 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. import

    android.app.Activity

    ;
  3. import

    android.os.Bundle

    ;
  4. import

    android.view.View

    ;
  5. import

    android.view.View.OnClickListener

    ;
  6. import

    android.widget.Button

    ;

  7. public

    class

    MainActivity extends

    Activity implements

    OnClickListener {
  8. private

    Button

    select;
  9. @Override
  10. protected

    void

    onCreate(

    Bundle savedInstanceState)

    {
  11. super

    .onCreate

    (

    savedInstanceState)

    ;
  12. setContentView(

    R.layout

    .activity_main

    )

    ;
  13. select =

    (

    Button

    )

    findViewById(

    R.id

    .show_music

    )

    ;
  14. select.setOnClickListener

    (

    this

    )

    ;
  15. }
  16. @Override
  17. public

    void

    onClick(

    View

    v)

    {

  18. }
  19. }

xz.jpg

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. android:

    showDefault=

    "true"
  12. android:

    layout_width =

    "wrap_content"
  13. android:

    layout_height=

    "wrap_content"
  14. android:

    key=

    "Alarm"
  15. android:

    summary=

    "Alarm"
  16. android:

    ringtoneType=

    "notification|alarm"

    />
  17. </

    PreferenceCategory>

  18. </

    PreferenceScreen>

The corresponding activity file, that loads the option screen has the following structure:
  1. package

    com.example.testlist

    ;

  2. import

    android.os.Bundle

    ;
  3. import

    android.preference.PreferenceActivity

    ;


  4. public

    class

    SoundSelect extends

    PreferenceActivity{
  5. @Override
  6. protected

    void

    onCreate(

    Bundle savedInstanceState)

    {
  7. super

    .onCreate

    (

    savedInstanceState)

    ;
  8. addPreferencesFromResource(

    R.layout

    .sound_select

    )

    ;
  9. }
  10. }

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:
screenshot_2014-08-29-17-25-52.png


The sound names are shown according to the system locale. That's why you can see Russian letters in the screen.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,143

329,151

Top