Pages

Saturday, September 8, 2012

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

Android Simple List Example


Define Your main.xml



<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="30dp"
    />


package sample.listex;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListExample extends ListActivity {
 
String[] languages={"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#",
"c","c++","java","php","asp","c#"};





/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
       
        setListAdapter(new ArrayAdapter<String>(this,R.layout.main,languages));
        ListView list=getListView();
        list.setTextFilterEnabled(true);
 
    list.setOnItemClickListener(new OnItemClickListener(){
     
       
       
       

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

Android Frame Layout Example Code


package example.withoutxml;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FrameExample extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

LinearLayout ll=new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                 LayoutParams.MATCH_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
       ll.setBackgroundResource(R.drawable.bunch);
       ll.setGravity(LinearLayout.VERTICAL);
FrameLayout fl=new FrameLayout(this);

fl.setLayoutParams(new FrameLayout.LayoutParams
(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));

ImageView iv=new ImageView(this);
iv.setLayoutParams(new FrameLayout.LayoutParams
(FrameLayout.LayoutParams.MATCH_PARENT,
          FrameLayout.LayoutParams.MATCH_PARENT));

iv.setPadding(30, 30, 30, 30);
iv.setImageResource(R.drawable.pinkrose);
fl.addView(iv);
TextView tv1=new TextView(this);
FrameLayout.LayoutParams params= new FrameLayout.LayoutParams
        ( LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
// tv1.setLayoutParams(new FrameLayout.LayoutParams
// (FrameLayout.LayoutParams.FILL_PARENT,
// FrameLayout.LayoutParams.WRAP_CONTENT));
tv1.setGravity(Gravity.TOP);
tv1.setText("this is upper part of image");
tv1.setTextColor(0xffff0000);
tv1.setTextSize(40);
tv1.setLayoutParams(params);
fl.addView(tv1);
TextView tv2=new TextView(this);
FrameLayout.LayoutParams params1= new FrameLayout.LayoutParams
         ( LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
// tv2.setLayoutParams(new FrameLayout.LayoutParams
// (FrameLayout.LayoutParams.FILL_PARENT,
// FrameLayout.LayoutParams.WRAP_CONTENT));
tv2.setLayoutParams(params1);
//tv2.setGravity(Gravity.BOTTOM);
tv2.setGravity(android.view.Gravity.BOTTOM);
tv2.setText("this is lower part of image");
tv2.setTextColor(0xff00ff00);
tv2.setTextSize(40);
fl.addView(tv2);


ll.addView(fl);
setContentView(ll);
}

}