SharedPreferences in Android

SharedPreferences Example in Android

MainCLASS

package com.example.malik.test;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

EditText Message;
SeekBar seekBar;
float font_size;
String font_color;
String text_info;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Message = (EditText) findViewById(R.id.message);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
text_info = sharedPreferences.getString(getString(R.string.TEXT_INFO), “”);
Message.setText(text_info);
font_size = sharedPreferences.getFloat(getString(R.string.FONT_SIZE), 25);
font_color = sharedPreferences.getString(getString(R.string.FONT_COLOR), “”);
Message.setTextSize(TypedValue.COMPLEX_UNIT_PX, font_size);

if(font_size == 25){
seekBar.setProgress(0);
} else {
seekBar.setProgress((int) font_size);
}

if(font_color.equals(“RED”)){
Message.setTextColor(Color.parseColor(“#FC0116”));
} else if(font_color.equals(“GREEN”)){
Message.setTextColor(Color.parseColor(“#03FF20”));
} else if(font_color.equals(“BLUE”)){
Message.setTextColor(Color.parseColor(“#0810F5”));
}

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Message.setTextSize(TypedValue.COMPLEX_UNIT_PX, progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
font_size = Message.getTextSize();
}
});

}

public void changeColor(View view) {
switch (view.getId()) {
case R.id.id_red_color:
Message.setTextColor(Color.parseColor(“#FC0116”));
font_color = “RED”;
break;
case R.id.id_blue_color:
Message.setTextColor(Color.parseColor(“#0810F5”));
font_color = “BLUE”;
break;
case R.id.id_green_color:
Message.setTextColor(Color.parseColor(“#03FF20”));
font_color = “GREEN”;
break;
}
}

public void saveSetting(View view) {
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putFloat(getString(R.string.FONT_SIZE), font_size);
editor.putString(getString(R.string.FONT_COLOR), font_color);
editor.putString(getString(R.string.TEXT_INFO), Message.getText().toString());
editor.commit();
}

public void clearSetting(View view) {
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();

}
}

 

MainXML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:orientation=”vertical”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.malik.test.MainActivity”
android:id=”@+id/relative_layout”
android:background=”#000000″
>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Enter your message”
android:textAppearance=”?android:textAppearanceLarge”
android:textColor=”#FFFFFF”
/>
<EditText
android:layout_width=”match_parent”
android:layout_height=”0dp”
android:layout_weight=”1″
android:id=”@+id/message”
android:background=”#FFFFFF”
android:textColor=”#000000″
android:gravity=”top”
/>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Adjust font size”
android:textAppearance=”?android:textAppearanceLarge”
android:background=”#000000″
android:textColor=”#ffffff”
/>

<SeekBar
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:id=”@+id/seek_bar”
/>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Select Font Color”
android:textAppearance=”?android:textAppearanceLarge”
android:textColor=”#FFFFFF”
/>

<RadioGroup
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”#ffffff”
android:layout_marginBottom=”20dp”
>
<RadioButton
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/id_red_color”
android:text=”Red”
android:onClick=”changeColor”
/>

<RadioButton
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/id_blue_color”
android:text=”Blue”
android:onClick=”changeColor”
/>

<RadioButton
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/id_green_color”
android:text=”Green”
android:onClick=”changeColor”
/>
</RadioGroup>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Save Setting”
android:layout_gravity=”center_horizontal”
android:onClick=”saveSetting”
android:background=”#ffffff”
android:textColor=”#000000″
android:layout_marginBottom=”20dp”

/>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Clear Setting”
android:layout_gravity=”center_horizontal”
android:onClick=”clearSetting”
android:background=”#ffffff”
android:textColor=”#000000″
android:layout_marginBottom=”20dp”

/>
</LinearLayout>

 

String XML

<resources>
<string name=”app_name”>Test</string>
<string name=”action_settings”>Settings</string>
<string-array name=”android_versions”>
<item>Version 1</item>
<item>Version 2</item>
<item>Version 3</item>
<item>Version 4</item>
<item>Version 5</item>
<item>Version 6</item>
<item>Version 7</item>
<item>Version 8</item>
<item>Version 9</item>
<item>Version 10</item>
</string-array>

<string name=”PREF_FILE”>APP_PREF_FILE</string>
<string name=”FONT_SIZE”>APP_FONT_SIZE</string>
<string name=”FONT_COLOR”>APP_FONT_COLOR</string>
<string name=”TEXT_INFO”>APP_TEXT_INFO</string>
</resources>

 

Output :

pic

 

 

 

Share

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.