Saturday, January 26, 2013

Create custom dialog :


code:

final Dialog d = new Dialog(myActivity.this);
d.setContentView(R.layout.mylayout);
d.setTitle("my title");
 final EditText edit = (EditText)d.findViewById(R.id.txtedit);
 Button btn = (Button)d.findViewById(R.id.txtbtn);
d.show();

layout:

<Button
        android:id="@+id/btnAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      />

    <EditText
        android:id="@+id/txtItem2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text" />

Change color of alertdialog button


 Button b = dialogf.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null){
   b.setBackgroundDrawable(getResources().getDrawable(R.drawable.loginbutton));
  b.setTextColor(getResources().getColor(android.R.color.white));
 }

Iterate through array values and insert to sqlite db:


String [] arrayA ={"a","b","c"};
Value[] adst =new Value[arrayA.length];
for( int i = 0; i < arrayA.length; i++)
{
String something = arrayA[i];
   Value dist =new Value();
    dist.setname(something);
     adst[i]=dist;
}

valuedao.insertInTx(adst);

Delete a row from sqlitetable:


 db.delete("Tablename","coloumnname"+"="+valuetocompare,null);



Removing duplicate rows from sqlite table


db.execSQL("delete from TABLENAME where rowid not in (select min(rowid) from TABLENAME group by COLOUMNNAME);");


Count number of rows in a sqlite table

long numRows = DatabaseUtils.queryNumEntries(db,"Table_name");

Get image from sdcard and put in imageview


File imageFile2 =new File(Environment.getExternalStorageDirectory(),"/images/"+imagef2);

      if(imageFile2!=null)
      {
Bitmap bitmap2 = BitmapFactory.decodeFile(imageFile2.getAbsolutePath());
     imageview.setImageBitmap(bitmap2);
      }