Sunday, March 2, 2014

Make stylish Button in android.

Write the below code in string.xml file.


<style name="ButtonText">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:textSize">30sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">2</item>
</style>

Make a file named btn_black.xml in drawable folder.


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#343434" />
            <stroke
                android:width="1dp"
                android:color="#171717" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#343434"
                android:endColor="#171717"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#171717" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

in main.xml


<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/ButtonText"
        android:background="@drawable/btn_black"
        android:text="@string/add" />

Output:


Friday, February 28, 2014

Example of scrollview in android.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
  android:layout_height="wrap_content">
 
  <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0">
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#000000"/>
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#440000" />
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#884400" />
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#aa8844" />
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#ffaa88" />
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#ffffaa" />
    </TableRow>
   
    <TableRow>
      <View
        android:layout_height="90dp"
        android:background="#ffffff" />
      </TableRow>
  </TableLayout>
</ScrollView>

Output:

Thursday, February 27, 2014

Example of how to pick an image from gallary.

acivity_main.xml


<?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">
 
    <ImageView
        android:contentDescription="@string/hello_world"
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0"
        android:text="@string/cp" />
 
</LinearLayout>

MainActivity.java


package com.mahi.imagepicker;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

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

Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
               
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
               
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
           
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
       
        }
    }

@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;
}

}

Output:




Sunday, February 23, 2014

Android ImageSwitcher.

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

 
   <ImageSwitcher
            android:id="@+id/imageSwitcher"
            android:layout_marginTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal" />
 
      <Button
          android:id="@+id/buttonNext"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="150dp"
          android:text="@string/nxt" />
   
</LinearLayout>

MainActivity.java


package com.mahi.imageswitcher;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

private ImageSwitcher imageSwitcher;
    Button btnNext;
    int imageIds[]={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,};
    int messageCount=imageIds.length;
    int currentIndex=-1;


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

        btnNext = (Button) findViewById (R.id.buttonNext);
        imageSwitcher = (ImageSwitcher) findViewById (R.id.imageSwitcher);

        imageSwitcher.setFactory(new ViewFactory()
        {
public View makeView() {
                    ImageView imageView = new ImageView(getApplicationContext());
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
                    return imageView;
            }
        });

        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
       
        imageSwitcher.setInAnimation(in);
        imageSwitcher.setOutAnimation(out);

        btnNext.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 currentIndex++;
                   // If index reaches maximum reset it
                    if(currentIndex==messageCount)
                        currentIndex=0;
                    imageSwitcher.setImageResource(imageIds[currentIndex]);
            }
        });
}

@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;
}
}

Output:



Thursday, February 20, 2014

android:ems attribute in EditText

The em is simply the font size.

In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion.

Declarations such as 'text-indent: 1.5em' and 'margin: 1em' are extremely common in CSS.

Wednesday, February 19, 2014

putExtra() in android

If you want add information to your intent you can use this method.
This information is represented as tuple (key, value).

There are the number of value types that can be included into the extras of intent (for instance, int, int[], Bundle, Parcelable, and so on). For each this method there is a corresponding"read" method that is used to get the information from the intent.

So here is a possible example how to use this.

Imagine that you want explicitly call the activity B from activity A and pass to it an array of integers:

int intArray[]={1,2,3,4};
Intent in=new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);

To read the information in activity B (in onCreate() method) you should use the following code:

Bundle extras = getIntent().getExtras();
int[] arrayInB = extras.getIntArray("my_array");

Tuesday, February 18, 2014

Adding the image to the layout.

<?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” >

<ImageView
    android:id=”@+id/icon”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_gravity=”center_horizontal”
    android:src=”@drawable/mahi5” />

</LinearLayout>

An ImageView allows you to project an image to the device’s screen.

android:id=”@+id/icon”


The id attribute defines the unique identifier for the view in the Android system.

layout_gravity


This property defines how to place the view, both its x- and y-axis, with its parent. Here, I have defined the value as the center_horizontal constant. This value informs the Android system to place the object in the horizontal center of its container, not changing its size. You can use many other constants, such as center_vertical, top, bottom, left, right, and many more.

android:src=”@drawable/mahi5”


This property is a direct child of the ImageView class. You use this property to set the image that you would like to show up on the screen.