Pages

Saturday, September 8, 2012

Android Flipper Example


Flipper class is used for simply flip the views.

package example.flip;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

public class FlipperEx extends Activity {
   Button b;
   ViewFlipper flip;
   LinearLayout ll;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       ll=(LinearLayout)findViewById(R.id.l_l);
       
        b=(Button)findViewById(R.id.button2);
        flip=(ViewFlipper)findViewById(R.id.viewFlipper1);

    }

    public void ClickHandler(View v)
    {

    flip.showNext();
     //flip.setFlipInterval(1000);
         // flip.setAutoStart(true);
   // flip.startFlipping();
    b.setGravity(Gravity.BOTTOM);
    ll.setGravity(Gravity.CENTER);
    }
    }

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"
    android:gravity="top"
    android:id="@+id/l_l">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<EditText android:text="EditText"
android:layout_height="wrap_content"
android:id="@+id/editText1"
 android:layout_width="match_parent"
 android:gravity="right">
 </EditText>
<Button android:text="Button"
android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="ClickHandler"
 android:gravity="top">
 </Button>
<ViewFlipper android:layout_width="match_parent"
 android:id="@+id/viewFlipper1"
  android:layout_height="wrap_content">

<TextView
android:text="first text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="second text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="third text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:text="fourth one is button"
android:id="@+id/button1"
 android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  </Button>


</ViewFlipper>
</LinearLayout>



Android Custom Dialog Box Example



create your 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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 </Button>
</LinearLayout>


custom.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:orientation="vertical">
    <ImageView android:layout_height="wrap_content"
    android:id="@+id/image"
     android:src="@drawable/bunch"
      android:layout_width="wrap_content">
      </ImageView>
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"
    android:id="@+id/text"
    >
    </TextView>
    <Button android:text="Button"
    android:id="@+id/button"
    android:layout_below="@id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    </Button>
</RelativeLayout>


create your Custom Dialog Activity


package example.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogExample extends Activity {
    
Context con=this;
Button b1;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    
    b1=(Button)findViewById(R.id.button1);
    
    b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(con);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.day);
Button b=(Button)dialog.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
});
    
    }
}

Android Grid View Example


<?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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
 
 
    <GridView
    android:id="@+id/grid"
    android:columnWidth="100px"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:numColumns="auto_fit"
    />

</LinearLayout>


set your Activity........

package adapter.grid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GridAdapterEx extends Activity {
    
GridView gv;
String[] listcontent={"c","java","php","c++","jsp","c#","servlet","struts"
 
,"c","java","php","c++","jsp","c#","servlet","struts",
"c","java","php","c++","jsp","c#","servlet","struts"};
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        gv=(GridView)findViewById(R.id.grid);
        ArrayAdapter<String> adapter
        = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,listcontent);
   gv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> v0, View v1, int v2, long v3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),((TextView)v1).getText(),Toast.LENGTH_SHORT).show();
}

   
   
   });
        gv.setAdapter(adapter);
    }
}

Android Simple Login Screen


<?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="vertical"
  android:background="@drawable/opel">
   
   
   
    <EditText android:text=""
    android:id="@+id/username"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="20dp"
     android:layout_marginLeft="20dp"
      android:layout_marginRight="20dp"
       android:layout_marginTop="50dp"></EditText>
   
    <EditText android:text=""
     android:id="@+id/password"
     android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:layout_marginTop="10dp"
      android:layout_marginRight="20dp"
       android:layout_marginLeft="20dp" android:layout_marginBottom="10dp"></EditText>
     
      <Button android:text="submit"
     android:id="@+id/but"
      android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="10dp"
        android:layout_marginRight="20dp"
         android:layout_marginLeft="20dp"
          android:layout_marginTop="10dp"></Button>
</LinearLayout>

Android Horizontal Scroll View Example



create your xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="wrap_content">
  <ImageView
android:id="@+id/IVDisplay"
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:src="@drawable/opel"
  android:layout_marginRight="20dp"
android:layout_marginLeft="40dp"></ImageView>
   <Button
android:text="Choose"
   android:id="@+id/bChoose"
   android:layout_width="fill_parent"
  android:layout_height="wrap_content"
android:layout_marginLeft="40dp"></Button>

  <HorizontalScrollView
    android:layout_width="200dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center">

  <LinearLayout
    android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <ImageView
  android:id="@+id/image1"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/opel"></ImageView>

  <ImageView
  android:id="@+id/image2"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/alfa"></ImageView>

  <ImageView
  android:id="@+id/image3"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/honda2"></ImageView>

  <ImageView
  android:id="@+id/image4"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/sports"></ImageView>

  <ImageView
  android:id="@+id/image5"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/honda"></ImageView>

  <ImageView
  android:id="@+id/image6"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/car"></ImageView>

  <ImageView
  android:id="@+id/image7"
  android:layout_width="125dp"
  android:layout_height="125dp"
  android:src="@drawable/carr"></ImageView>

  </LinearLayout>
  </HorizontalScrollView>
  </LinearLayout>




create Activity for horizontal scroll and remind one thing select your own images.

package car.care;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class hori extends Activity implements OnClickListener{
     ImageView Display;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.horizontalscroll);
Display=(ImageView)findViewById(R.id.IVDisplay );
Button btn5= (Button) findViewById(R.id.bChoose);
ImageView im1 =(ImageView)findViewById(R.id.image1);
ImageView im2 =(ImageView)findViewById(R.id.image2);
ImageView im3 =(ImageView)findViewById(R.id.image3);
ImageView im4 =(ImageView)findViewById(R.id.image4);
ImageView im5 =(ImageView)findViewById(R.id.image5);
ImageView im6 =(ImageView)findViewById(R.id.image6);
ImageView im7 =(ImageView)findViewById(R.id.image7);

btn5.setOnClickListener(this);
im1.setOnClickListener(this);
im2.setOnClickListener(this);
im3.setOnClickListener(this);
im4.setOnClickListener(this);
im5.setOnClickListener(this);
im6.setOnClickListener(this);
im7.setOnClickListener(this);





}
public void onClick(View v) {
// TODO Auto-generated method stub

switch(v.getId()){
case R.id.image1:
Display.setImageResource(R.drawable.opel);
break;

case R.id.image2:
Display.setImageResource(R.drawable.alfa);
break;

case R.id.image3:
Display.setImageResource(R.drawable.honda2);

break;
case R.id.image4:
Display.setImageResource(R.drawable.sports);

break;
case R.id.image5:
Display.setImageResource(R.drawable.honda);

break;
case R.id.image6:
Display.setImageResource(R.drawable.car);

break;
case R.id.image7:
Display.setImageResource(R.drawable.carr);

break;
case R.id.bChoose:


}








}

}



https://play.google.com/store/apps/developer?id=Manu+Jaggi