Android list preference settings page example

Activity example list preference example

Android list preference mainly used in settings purpose. Save color code use it as layout background color or any other purpose. Video tutorial – Put value in list which one will be selected by user it will be preference for app user it value will be set until it is changed even your app is closed and reopened again. Get more latest android code.

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

@Override
protectedvoidonCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);
String backColorS = spf.getString(getString(R.string.key_color),"#DC143C");

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.backColor);
mRelativeLayout.setBackgroundColor(Color.parseColor(backColorS));

Button mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {

@Override
publicvoidonClick(Viewview) {

finish();
Intent setPref = newIntent(getApplicationContext(), SettingsActivity.class);
startActivity(setPref);
}
});
}
}

Source code

package attraction.listprefe;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class SettingsActivity extends PreferenceActivity {

@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);

getFragmentManager().beginTransaction().replace(android.R.id.content,new SettingsFragment()).commit();

}
publicstaticclassSettingsFragmentextendsPreferenceFragment {

@Override
publicvoidonCreate(finalBundlesavedInstanceState) {
super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);
}
}
@Override
publicvoidonBackPressed() {
super.onBackPressed();
finish();
Intent i =newIntent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
Layout .xml file
<?xml version=”1.0″ encoding=”utf-8″ ?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/backColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
  <Button
android:id=”@+id/btn
android:layout_width=”wrap_content
android:layout_height=”wrap_content
android:text=”Settings />
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ATTRACTION"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#0285ff"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Create xml folder inside res create a new xml file name it as preferences
<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key=”@string/key_color
android:title=”Background Color
android:entries=”@array/names_array
android:entryValues=”@array/values_array
android:defaultValue=”#DC143C
android:icon=”@mipmap/ic_launcher
android:dialogIcon=”@mipmap/ic_launcher
android:summary=”Choose a background color />
</PreferenceScreen>
Add .SettingsActivity name in androidManifext.xml
<?xml version="1.0" encoding="utf-8" ?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="attraction.listprefe">
<application
android:allowBackup=”true
android:icon=”@mipmap/ic_launcher
android:label=”@string/app_name
android:supportsRtl=”true
android:theme=”@style/AppTheme>
<activity
android:name=”.MainActivity>
<intent-filter>
<action
android:name=”android.intent.action.MAIN />
<category
android:name=”android.intent.category.LAUNCHER />
</intent-filter>
</activity>
<activity
android:name=”.SettingsActivity />
</application>
</manifest>

Add a Comment

Your email address will not be published. Required fields are marked *