Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE

Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE - Welcome to ANDROID KAWE, Ini adalah sebuah blog directory tentang burung,baik itu suara burung ataupun tips perawatan burung.Blog ini adalah situs perangkum otomatis dari pencarian google yang kali ini berjudul Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE.Ada ribuan artikel yang sudah terekam ke dalam situs ini,silahkan cari sesuai yang kalian kehendaki,untuk artikel selanjutnya tentang judul diatas bisa kalian baca di bawah ini.

Konten : Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE
Judul : Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE

lihat juga


Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE

Hi Friends

       Today i am going to explain Android SharedPreferences.

Android SharedPreferences:

Android supports the usage of the SharedPreferences class for persisting key-value pairs (preferences)of primitive data types in the Android file system.
The definition of these preferences can be done via an XML resource.
The PreferenceManager class provides methods to get access to preferences stored in a certain file. 

Step to Create SharedPreference:-

1. create object of SharedPreference in following way.


SharedPreferences spref = (SharedPreferences)PreferenceManager.getDefaultSharedPreferences(this);


2. Getting value from SharedPreference:


count = spref.getInt("appcount", 0);

3. Setting new value for preference:


SharedPreferences.Editor editor = spref.edit();
       editor.putInt("appcount", count);
        editor.commit(); // Very important

Now i am going to explain shared preference with very simple example that count how many times your application open.


Java Source code for application:-



package in.androidshivendra.sharedpreferenceexample; import android.os.Bundle; import android.preference.PreferenceManager; import android.app.Activity; import android.content.SharedPreferences; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView tv; SharedPreferences spref; int count ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spref= (SharedPreferences)PreferenceManager.getDefaultSharedPreferences(this); count = spref.getInt("appcount", 1); tv = (TextView)findViewById(R.id.textView1); tv.setText("you have "+count+"time open it"); count++; SharedPreferences.Editor editor = spref.edit(); editor.putInt("appcount", count); editor.commit(); // Very important } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }


Layout xml code:-


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

   
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


OutPutScreen:-

You can Download complete code here




Demikianlah Artikel Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE

Semoga artikel tentang Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE, mudah-mudahan bisa memberi manfaat untuk anda semua.

Anda sedang membaca artikel Android Tutorial-Android-SharedPreference-ExampleANDROIDKAWE dan artikel ini url permalinknya adalah http://androidkawe.blogspot.com/2016/12/android-tutorial-android.html Semoga artikel ini bisa bermanfaat.Sekali lagi,ini adalah situs auto yang tidak ditulis langsung oleh admin,Kami tidak menjamin akan kebenaran dari artikel yang tertulis.
Back To Top