December 2014

Wednesday, December 17, 2014

Creating Shared Preferences in android.....


This article shows how to create SharedPreferences in android.


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class MyPreferences {
    public static boolean putInt(Context activity,String key,int value){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();                      
        return true;      
    }
 
    public static int getInt(Context activity,String key,int defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        int temp = sharedPreferences.getInt(key, defaultValue);
        return temp;      
    }
 
    public static boolean putString(Context activity, String key, String value)
    {
    SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();                      
        return true;         }
 
    public static String getString(Context activity,String key,String defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        String temp = sharedPreferences.getString(key, defaultValue);
        return temp;      
    }
 
    public static boolean putLong(Context activity, String key, long value)
    {
    SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
return true;         }
 
    public static long getLong(Context activity,String key,long defaultValue){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("pref", Activity.MODE_PRIVATE);
        long temp = sharedPreferences.getLong(key, defaultValue);
        return temp;      
    }
 
}

By implementing above code you can easily Create SharedPreferences.

Thursday, December 4, 2014

Creating notification in android.....


This article shows how to create notification in android.


public MyNotification(Context ctx){
super();
this.ctx=ctx;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
notification=builder.getNotification();
notification.when=when;
notification.icon=R.drawable.mp3_icon;
remoteViews=new RemoteViews(ctx.getPackageName(),                        R.layout.service_layout);
notification.contentView = remoteViews;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(5, notification);
}



Wednesday, December 3, 2014

Creating Custom views in a PreferenceActivity in android...



This article shows how to add custom view in PreferenceActivity in android.

Create main.xml, the only necessary view is a ListView, with id: android:id="@android:id/list".

<?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" android:weightSum="1">
        <ListView 
            android:id="@android:id/list" 
            android:layout_weight="1"
            android:layout_width="fill_parent"
                android:layout_height="0dp">
        </ListView>
        <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


Create CustomPreferenceActivity.java

public class CustomPreferenceActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                addPreferencesFromResource(R.xml.settings);
               
                //setup any other views that you have
                TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("View Added");
        }
}