Friday, December 27, 2013

Create rounded Imageview

This post is to create rounded imageview in android application.
Use this below method for creare rounded shape image view in android.

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);

canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}


Using this methods you can create your imageview rounded in application.

int targetWidth = 50;
int targetHeight = 50;

Using above variable you can set your image view size.

How to set rounded shape imageview in activity
In your activity use this method like this.


public class MainActivity extends Activity {

private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.img);
Bitmap bitmap = getRoundedShape(BitmapFactory.decodeResource(
MainActivity.this.getResources(), R.drawable.ic_launcher));

img.setImageBitmap(bitmap);
}

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);

canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}

}

Output of ImageView:


Start multiple Activities using Intent

This blog is related to start multiple Activity using Intent.

Generally we are using Intent for start new Activity in android.
But now you can start multiple activity using single intent.

method : startActivities(Intent[] intent)

Keep in mind that this method is used from Above API 11

See more update about android here

Use of this method

In your Activity

public class MainActivity extends Activity {

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

Intent[] array = {};

Intent demo1  = new Intent(MainActivity.this,YOURACTIVITY.class);
Intent demo2 = new Intent(MainActivity.this,YOURACTIVITY.class);
Intent demo3  = new Intent(MainActivity.this,YOURACTIVITY.class);
Intent demo4  = new Intent(MainActivity.this,YOURACTIVITY.class);

startActivities(array);
}

}

Note that unlike that approach, generally none of the activities except the lase in the array will be created at this point, but rather will be created when the user first visits them (due to pressing back from the activity on top).

See more update about android here

Thursday, February 28, 2013

Send SMS from android programmatically in background

Sending SMS in background from android mobile using code.


For sending SMS from android using code follow below code snippet.
It's very easy and simple.

See below link for more update about android

http://www.varemads.com/category/android/

Java Activity for this 

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendsms("******Mobile Number Here******", "***Message Content Here***");
}
});
}

public static void sendsms(String address,String msgContent)
    {
        try
        {
            SmsManager sms = SmsManager.getDefault();
            ArrayList<String> smsString = sms.divideMessage(msgContent); 
            sms.sendMultipartTextMessage(address, null, smsString, null, null);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Xml file 

<RelativeLayout 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"
    tools:context=".MainActivity" >

   

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="92dp"
        android:text="Button" />

</RelativeLayout>


Wednesday, February 27, 2013

Gallery View in Android

Here we learn how to create gallery view in android application.


For Create gallery View in android we need create one Image Adapter class for display image in fullscreen in gallery view.

For create Gallery view Activity looks like this.
public class MainActivity extends Activity implements OnItemClickListener
{
private static final String tag = "Main";
private Gallery _gallery;
private ImageAdapter _imageAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

_gallery = (Gallery) this.findViewById(R.id.gallery1);
_imageAdapter = new ImageAdapter(this);
_gallery.setAdapter(_imageAdapter);
_gallery.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView arg0, View view, int position, long duration)
{
int resourcId = (Integer) _imageAdapter.getItem(position);
Drawable drawable = getResources().getDrawable(resourcId);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourcId);

Toast.makeText(this, "Selected Image: " + getResources().getText(resourcId) + "\nHeight: " + bitmap.getHeight() + "\nWidth: " + bitmap.getWidth(), Toast.LENGTH_SHORT).show();
}
}

Xml File for this activity looks like this

<?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:gravity="center"
>

<Gallery
android:id="@+id/gallery1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:spacing="10dip"
>
</Gallery>
</LinearLayout>


Image Adapter class

Friday, January 4, 2013

Attached Drawable to Textview in android.

In this Example How to attached drawable to textview in android.


A quick dig through the documentation for TextView lead me to the setCompoundDrawableWithIntrinsicBounds() method which is a method of attaching drawables to a TextView. 

See more update android

Using Xml Code:



<TextView

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"
  android:drawableTop="@drawable/ic_launcher"
  android:gravity="center"
  android:text="Hello" />

You can Drawable to any side of textview like top,bottom,right,left.


android:drawableBottom="@drawable/ic_launcher"
  android:drawableTop="@drawable/ic_launcher"
  android:drawableRight="@drawable/ic_launcher"
  android:drawableLeft="@drawable/ic_launcher"


Using Java Code:

@Override
public void onCreate( Bundle savedInstanceState )
{
   super.onCreate( savedInstanceState );
   setContentView( R.layout.main );
 
   TextView tv = (TextView) findViewById( R.id.textView );
   tv.setCompoundDrawablesWithIntrinsicBounds( 0, 
       R.drawable.ic_launcher, 0, 0 );
}

See more update about android
http://www.varemads.com/category/android/