Pages

Monday, August 15, 2011

Tab Control






Basic description of algorithm in step by step form:
1.) Create a project TabSample.
2.) Put the following code snippet in main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <RelativeLayout
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="3dp">
       <FrameLayout
           android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1" />
       <TabWidget
           android:id="@android:id/tabs"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_alignBottom = "@android:id/tabcontent"
           />
    </RelativeLayout>
</TabHost>
3.) To create tab layout at bottom of the screen use the following in TabWidget in main.xml:
android:layout_alignBottom = "@android:id/tabcontent"
4.) For tab content create two more activities first OptionsActivity.java and second EditActivity.java.
5.) OptionsActivity.java will look like following:
package app.tabsample; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class OptionsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView t = new TextView(this);
        t.setText("text");
        setContentView(t);
    }
}
6.) EditActivity.java will look like following:
package app.tabsample; import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class EditActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent));  
    }
    private static String[] mListContent={"Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3"};
}
7.) You must need to register these activities into your manifest file.Your manifest file will look like below:
<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:label="@string/app_name" android:name=".TabSample">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".OptionsActivity"></activity>
        <activity android:name=".EditActivity"></activity>
 </application>
8.) Run the Application.


Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. TabSample. Enter following information:
Project name: TabSample
Build Target: Google APIs
Application name: TabSample
Package name: app.TabSample
Create Activity: TabSample

On Clicking Finish TabSample code structure is generated with the necessary Android Packages being imported along with TabSample.java. TabSample class will look like following:
package app.tabsample;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent; public class TabSample extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabHost = getTabHost();      
     
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TEXT").setContent(new Intent(this, OptionsActivity.class)));
     
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("LIST").setContent(new Intent(this, EditActivity.class)));
        tabHost.setCurrentTab(1);
    }
}

Output –The final output:

Create a Toggle Button



Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project ToggleButtonDemo.
2.) Put the following code snippet in res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
        <ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="yello" android:textOff="red">
        </ToggleButton>
</LinearLayout>
3.) Run the application.


Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ToggleButtonDemo. Enter following information:
Project name: ToggleButtonDemo
Build Target: Android APIs2.1
Application name: ToggleButtonDemo
Package name: com.app.ToggleButtonDemo
Create Activity: ToggleButtonDemo

On Clicking Finish ToggleButtonDemo code structure is generated with the necessary Android Packages being imported along with ToggleButtonDemo.java. ToggleButtonDemo class will look like following:
package com.app.ToggleButtonDemo; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ToggleButtonDemo extends Activity implements OnClickListener
{
    ToggleButton tg;
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        tv.setBackgroundColor(0xfff00000);
        tg = (ToggleButton) findViewById(R.id.toggleButton1);
        tg.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if((tg.isChecked()))
        {
                System.out.println("checked");
                tv.setBackgroundColor(0xffffff00);
        }
        else
        {
                System.out.println("Unchecked");
                tv.setBackgroundColor(0xfff00000);
        }
    }
}



Output –The final output:

Location Manager

Basic description of algorithm in step by step form:
1.) Create a Project LocManageDemo.
2.) Add the following permissions to your application in AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.app.LocManageDemo" android:versionCode="1"
   android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-library android:name="com.google.android.maps" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
       
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LocManageDemo" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
    <uses-library android:required="true" android:name="com.google.android.maps">
    </uses-library>
    </application>
</manifest>
3.) Put the following code in res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/mainlayout"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >     <com.google.android.maps.MapView
       android:id="@+id/mapview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:clickable="true"
       android:apiKey="Your Maps API Key"
   />
</RelativeLayout>
4.) Replace “Your Maps API Key” with your Google API key.
5.) The next step requires that you have to generate your Google Map key. Steps to generate Google Map Key :
i) Run your cmd prompt.
ii) Reach your folder where you have installed java because you have to use a tool called keytool.exe located here.
Example :C:/Program Files/Java/jdk1.6.0_22/bin/java
iii) Enter the following in cmd prompt.
keytool.exe -list -alias androiddebugkey -keystore C:\myandroid\.android\debug.keystore -storepass android -keypass android
iv) You will get a MD5 fingerprint something like the text below
94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
v) Copy the MD5 fingerprint and put it here
http://code.google.com/android/maps-api-signup.html
vi) You will get a Google API key something like this :
0cgwXB2sO7JhRnX4YhkuytZQCxLgh6hOxUbu01g0cgwXB2sO7JhRnX4YhkuytZQCxLgh6hOxUbu01g
6.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project. Give the respective project name i.e. LocManageDemo. Enter following information:
Project name: LocManageDemo
Build Target: Google APIs
Application name: LocManageDemo
Package name: com.app.LocManageDemo
Create Activity: LocManageDemo
Make sure to select the “Google API” as Target.

On Clicking Finish LocManageDemo code structure is generated with the necessary Android Packages being imported along with LocManageDemo.java. LocManageDemo class will look like following:
package com.app.LocManageDemo; import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.RelativeLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class LocManageDemo extends MapActivity
{
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main); // bind the layout to the activity
        // create a map view
        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(14); // Zoom 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());
   }
   @Override
   protected boolean isRouteDisplayed() {
        return false;
   }
   public class GeoUpdateHandler implements LocationListener {
     @Override
     public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        mapController.animateTo(point);
     }
     @Override
     public void onProviderDisabled(String provider) { }
     @Override
     public void onProviderEnabled(String provider) { }
     @Override
     public void onStatusChanged(String provider, int status, Bundle extras) { }
  }
}
Output –The final output:

Detect USB connection

Basic description of algorithm in step by step form:
1.) Create a Project DetactUSB.
2.) Create a class DetactUSB.java, which will extends BroadcastReceiver.
3.) Put the following code in DetactUSB.java:
package com.app.DetactUSB; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
public class DetactUSB extends BroadcastReceiver
{
    private static final String TAG = "DetactUSB";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB connected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
                Log.i(TAG,"USB connected..");
        }
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setBackgroundColor(Color.MAGENTA);
                textView.setTextColor(Color.BLUE);
                textView.setPadding(10,10,10,10);
                textView.setText("USB Disconnected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
        }
    }
}
4.) Add receiver in AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.DetactUSB"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO">
    </uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".MyActivity" android:label="@string/app_name">
                <intent-filter>
                     <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
    <receiver android:name=".DetactUSB">
       <intent-filter>
            <action android:name="android.intent.action.UMS_CONNECTED" />
            <action android:name="android.intent.action.UMS_DISCONNECTED" />
       </intent-filter>
    </receiver>
    </application>
</manifest>
5.) To detect USB :
android.intent.action.UMS_CONNECTED
android.intent.action.UMS_DISCONNECTED
6.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DetactUSB. Enter following information:
Project name: DetactUSB
Build Target: Android 2.1
Application name: DetactUSB
Package name: com.app.DetactUSB
Create Activity: MyActivity

On Clicking Finish DetactUSB code structure is generated with the necessary Android Packages being imported along with MyActivity.java. MyActivity class will look like following:
package com.app.DetactUSB; import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity
{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
        }
}
Output : The final output will look like following :

Read Contacts from device

1.) Create a Project ReadContacts.
2.) Put the following code snippet in res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView  
   android:id ="@+id/con"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>
3.) Add the following permission in AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.ReadContacts"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ReadContacts"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>     </application>
</manifest>
4.) Import the following packages in activity:
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.TextView;
5.) Add some contacts in device/emulator.
6.) Run the Application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ReadContacts. Enter following information:
Project name: ReadContacts
Build Target: Android APIs2.1
Application name: ReadContacts
Package name: com.app. ReadContacts
Create Activity: ReadContacts

On Clicking Finish ReadContacts code structure is generated with the necessary Android Packages being imported along with ReadContacts.java. ReadContacts class will look like following:
package com.app.ReadContacts; import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.TextView;
public class ReadContacts extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                TextView contactView = (TextView) findViewById(R.id.con);
                Cursor cursor = getContacts();
                while (cursor.moveToNext()) {
                        String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                        contactView.append("Name: ");
                        contactView.append(displayName);
                        contactView.append("\n");
                }
        }
        private Cursor getContacts() {
                // Run query
                Uri uri = ContactsContract.Contacts.CONTENT_URI;
                String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
                String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ‘" + ("1") + "’";
                String[] selectionArgs = null;
                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
                return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        }
}
Output –The final output:

Android Preferences

Preferences are an important part of an Android application. It is important to let the users have the choice to modify and personalize their application depending on their needs.
In the screenshots "custom" is spelled "costum". The typo is fixed in the code examples.
Android preferences can be set in two ways. You can create a preferences.xml file in the res/xml directory, or you can set the preferences from code.
The first example shows a preferences.xml file. Every preference needs to have a android:key value, that we call to get the preference's value. The android:title is the preference's title, and the android:summary is a summary about the preference. The android:defaultValue is the default value of the preference - fx. true or false.
Currently there are 5 different preference views:
  • The CheckBoxPreference is a simple checkbox, that can return true or false.
  • The ListPreference, which shows a radioGroup where only 1 item can be selected a time. The android:entries links to an array in the res/values/arrays, and the android:entryValues is an other array with the items to be returned.
  • The EditTextPreference shows a dialog with an editText view. This returns a String.
  • The RingtonePreference shows a radioGroup that shows the ringtones.
  • The Preference is a custom preference. This works like a Button.
  • The PreferenceScreen is a screen with preferences. When you have a PreferenceScreen inside an other PreferenceScreen, it simply opens a new screen with other preferences.
  • The PreferenceCategory is a category with preferences.
  • Custom preference
    Custom Preference
  • Edit Text Preference
    Edit Text Preference
  • List Preference
    List Preference
  • Ringtone Preference
    Ringtone Preference
  • Preference Screen
    Preference Screen
This is an example on the preferences.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="First Category">
                <CheckBoxPreference
                        android:title="Checkbox Preference"
                        android:defaultValue="false"
                        android:summary="This preference can be true or false"
                        android:key="checkboxPref" />
                <ListPreference
                        android:title="List Preference"
                        android:summary="This preference allows to select an item in a array"
                        android:key="listPref"
                        android:defaultValue="digiGreen"
                        android:entries="@array/listArray"
                        android:entryValues="@array/listValues" />
        </PreferenceCategory>
        <PreferenceCategory
                android:title="Second Category">
        <EditTextPreference
                android:name="EditText Preference"
                android:summary="This allows you to enter a string"
                android:defaultValue="Nothing"
                android:title="Edit This Text"
                android:key="editTextPref" />
        <RingtonePreference
                android:name="Ringtone Preference"
                android:summary="Select a ringtone"
                android:title="Ringtones"
                android:key="ringtonePref" />
        <PreferenceScreen
                android:key="SecondPrefScreen"
                android:title="Second PreferenceScreen"
                android:summary="This is a second PreferenceScreen">
                <EditTextPreference
                        android:name="An other EditText Preference"
                        android:summary="This is a preference in the second PreferenceScreen"
                        android:title="Edit text"
                        android:key="SecondEditTextPref" />
        </PreferenceScreen>
        <Preference
                android:title="Custom Preference"
                android:summary="This works almost like a button"
                android:key="customPref" />
        </PreferenceCategory>
</PreferenceScreen>
To show the preference screen, we create a class which extends PreferenceActivity. This is an example on the preference class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.kaloer.preferenceexample;
 
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
 
public class Preferences extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
                // Get the custom preference
                Preference customPref = (Preference) findPreference("customPref");
                customPref
                                .setOnPreferenceClickListener(new OnPreferenceClickListener() {
 
                                        public boolean onPreferenceClick(Preference preference) {
                                                Toast.makeText(getBaseContext(),
                                                                "The custom preference has been clicked",
                                                                Toast.LENGTH_LONG).show();
                                                SharedPreferences customSharedPreference = getSharedPreferences(
                                                                "myCustomSharedPrefs", Activity.MODE_PRIVATE);
                                                SharedPreferences.Editor editor = customSharedPreference
                                                                .edit();
                                                editor.putString("myCustomPref",
                                                                "The preference has been clicked");
                                                editor.commit();
                                                return true;
                                        }
 
                                });
        }
}
We can call this activity when we click a button:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                Button prefBtn = (Button) findViewById(R.id.prefButton);
                prefBtn.setOnClickListener(new OnClickListener() {
 
                        public void onClick(View v) {
                                Intent settingsActivity = new Intent(getBaseContext(),
                                                Preferences.class);
                                startActivity(settingsActivity);
                        }
                });
        }

To read these preferences from code, we should create a getPrefs() method, which we can call in the onStart() method. When we call it in the onStart() method instead of onCreate(), we can be sure that the preferences load when we have set them and returned to our main activity,
The getPrefs() method could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
boolean CheckboxPreference;
        String ListPreference;
        String editTextPreference;
        String ringtonePreference;
        String secondEditTextPreference;
        String customPref;
 
        private void getPrefs() {
                // Get the xml/preferences.xml preferences
                SharedPreferences prefs = PreferenceManager
                                .getDefaultSharedPreferences(getBaseContext());
                CheckboxPreference = prefs.getBoolean("checkboxPref", true);
                ListPreference = prefs.getString("listPref", "nr1");
                editTextPreference = prefs.getString("editTextPref",
                                "Nothing has been entered");
                ringtonePreference = prefs.getString("ringtonePref",
                                "DEFAULT_RINGTONE_URI");
                secondEditTextPreference = prefs.getString("SecondEditTextPref",
                                "Nothing has been entered");
                // Get the custom preference
                SharedPreferences mySharedPreferences = getSharedPreferences(
                                "myCustomSharedPrefs", Activity.MODE_PRIVATE);
                customPref = mySharedPreferences.getString("myCusomPref", "");
        }
Remember to add the following tag in your androidmanifest.xml file and add a new string item with the name "set_preferences" with the preference screen's title, for example "Preferences"
1
2
3
4
<activity
        android:name=".Preferences"
        android:label="@string/set_preferences">
</activity>

This is the final result
  • The main activity screen

  • The preference activity

Popular Posts