Sunday, August 4, 2013

Trigger Media Scanner Programmatically

sendBroadcast (
    new Intent(Intent.ACTION_MEDIA_MOUNTED, 
        Uri.parse("file://" + Environment.getExternalStorageDirectory()))
);

Friday, August 17, 2012

Tile Bitmap

Many times you would like to repeat an image in the background.
That is possible in android like css repeat.

Create tile_bitmap.xml in drawable
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/rs"
    android:tileMode="repeat"></bitmap>
   
and use it like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   
    android:background="@drawable/tile_bitmap">

Friday, July 20, 2012

AlertDialog With CheckBox


MyClass.java

AlertDialog.Builder alert = new AlertDialog.Builder(OfferPager.this);
        LayoutInflater adbInflater = LayoutInflater.from(OfferPager.this);
        View checkboxLayout = adbInflater.inflate(R.layout.offer_chk, null);
        dontShowAgain = (CheckBox)checkboxLayout.findViewById(R.id.skip);
        alert.setView(checkboxLayout);
        alert.setTitle("Alert");
        alert.setIcon(android.R.drawable.ic_dialog_alert);
        alert.setMessage(getString(R.string.msg));
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int which) {
                  boolean checkBoxResult = false;
                  if (dontShowAgain.isChecked())  checkBoxResult = true;
                      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                      SharedPreferences.Editor editor = settings.edit();
                      editor.putBoolean("skipMessage", checkBoxResult);   
                      editor.commit();
                  return; 
              } });
       
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int which) {
              boolean checkBoxResult = false;
              if (dontShowAgain.isChecked())  checkBoxResult = true;
              SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                  SharedPreferences.Editor editor = settings.edit();
                  editor.putBoolean("skipMessage", checkBoxResult);
                  editor.commit();
                  return; 
          } });
       
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean skipMessage = settings.getBoolean("skipMessage", false);
       
        if (!skipMessage )alert.show();



view_chk.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >
   
    <CheckBox
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/offer_dont_display" >
    </CheckBox>

</LinearLayout>

Wednesday, May 16, 2012

Write Object in Database

To Write in DB

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos); 
out.writeObject(myObj);
byte[] yourBytes = bos.toByteArray();
out.close();
bos.close();

String data = Base64.encodeToString(yourBytes, Base64.DEFAULT);
dbHelper.insertWeather(db, data);

To Read from DB

String data = cursor.getString(0);
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(data, Base64.DEFAULT));
ObjectInput in = new ObjectInputStream(bis);
myObj = (POJO_My_Info)in.readObject();
bis.close();
in.close();;

Monday, May 7, 2012

Way to install Application on by defualt SD Card


Here i found one good solution for installing Android app to SD Card by default.

adb shell pm setInstallLocation 2 OR adb shell pm set-install-location 2