Pages

Saturday, October 27, 2012

Android Simple Button click Example with String Resources



Your Activity
package pro2.pro3;

import android.app.Activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.widget.Toast;

public class ButtonExample extends Activity implements OnClickListener
{

  private Button hiButton;
  private Button helloButton;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    hiButton = (Button) findViewById(R.id.hi_button);
    hiButton.setOnClickListener(this);
    helloButton = (Button) findViewById(R.id.hello_button);
    helloButton.setOnClickListener(this);
  }

  public void onClick(View v)
  {

    EditText nameField = (EditText) findViewById(R.id.name_field);
    String name = nameField.getText().toString();
    if (name.length() == 0)
    {
      new AlertDialog.Builder(this).setMessage(R.string.error_name_missing)
          .setNeutralButton(R.string.error_ok, null).show();
      return;

    }
    if (v == hiButton || v == helloButton)
    {
      int resourceId = v == hiButton ? R.string.hi_greeting
          : R.string.hello_greeting;
      String greeting = getResources().getString(resourceId, name);
      Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();
      TextView greetingField = (TextView) findViewById(R.id.greeting_field);
      greetingField.setText(greeting);

    }
  }
}


Create main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
   android:id="@+id/greeting_field"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_width="match_parent"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:text="@string/enter_your_name"
android:textColor="#ff00ff"/>
<EditText
   android:id="@+id/name_field"
   android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
   android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">

<Button
    android:id="@+id/hi_button"
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
android:text="@string/hi_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

<Button
   android:id="@+id/hello_button"
   android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_button"
android:textSize="20dp"
android:layout_weight="1"
android:textColor="#ff00ff"/>

</LinearLayout>
</LinearLayout>



Define main.xml  strings in your strings.xml file

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="hello">Hello World, manu!</string>
    <string name="app_name">pro1</string>
    <string name="enter_your_name">Enter your name:</string>
    
    <string name="hi_button">Say Hi!</string>
<string name="hello_button">Say Hello!</string>

<string name="error_name_missing">Please enter your name</string>
<string name="error_ok">OK</string>

<string name="hi_greeting">Hi %s!</string>
<string name="hello_greeting">Hello %s!</string>
</resources>

Simple Example to move one activity to another in android


First Create your main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
      android:id="@+id/et_data"
      android:hint="EditText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
</EditText>
<Button
     android:text="Next Screen"
     android:id="@+id/next_screen_btn"
    android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

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.EditText;

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity xml


<?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">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NextScreen extends Activity
{
  private TextView tvText;

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

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".NextScreen"/>
    </application>
</manifest>



















Android Simple Tutorial to Send Data from First Activity to Second Activity



First Create your main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
     android:id="@+id/et_data"
     android:hint="EditText"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
</EditText>
<Button
    android:text="Next Screen"
    android:id="@+id/next_screen_btn"
  android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>

</LinearLayout>



Define your first Activity screen


package but.clk;

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.EditText;

public class clicking extends Activity
{
private Button nextBtn;
private EditText etData;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextBtn=(Button)findViewById(R.id.next_screen_btn);
    etData=(EditText)findViewById(R.id.et_data);
    nextBtn.setOnClickListener(new OnClickListener() {
   
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
        String Data=etData.getText().toString();
        Intent intent=new Intent(clicking.this,NextScreen.class);
        intent.putExtra("DATA",Data );
        startActivity(intent);
      }
    });
 
  }

}




Second Activity xml


<?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">
<TextView android:text="text"
android:id="@+id/data_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:textColor="#ff00ff">
</TextView>
</LinearLayout>




Define your next Activity screen

package but.clk;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NextScreen extends Activity
{
  private TextView tvText;

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

    String Data=getIntent().getExtras().getString("DATA");
    tvText=(TextView)findViewById(R.id.data_text);
    tvText.setText(Data);
  }
}





and don't forget to add second activity in manifest file
<activity android:name=".NextScreen"/>

Your manifest file



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="but.clk"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".clicking"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".NextScreen"/>
    </application>
</manifest>




















Saturday, September 8, 2012

Android Spinner Example



package sample.spin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerEx extends Activity implements OnItemSelectedListener {
 
String[] listcontent={"c","java","php","c++","jsp","c#","servlet","struts"

,"c","java","php","c++","jsp","c#","servlet","struts"};

TextView tv;

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text);
        Spinner spin=(Spinner)findViewById(R.id.spin_id);
        spin.setOnItemSelectedListener( this);
 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listcontent);

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
    }



public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub

tv.setText(listcontent[position]);

}



public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

tv.setText("");
}
}


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 
    xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
   
    android:id="@+id/text"
    />
    <Spinner
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spin_id"
    />
</LinearLayout>


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

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

}

Android Table Layout Example Code Without Xml


package example.withoutxml;

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.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;

public class LayoutsExample extends Activity  {


Button b1,b2,b3,b4;
EditText et1,et2,et3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);


TableLayout tl=new TableLayout(this);


//    tl.setShrinkAllColumns(true);

tl.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                                            TableRow.LayoutParams.WRAP_CONTENT));
tl.setStretchAllColumns(true);
tl.setBackgroundResource(R.drawable.day);

//TableRow tr=new TableRow(this);

// tr.addView(b1);
// tl.addView(tr);


TableRow tr1=new TableRow(this);
//tr1.addView(b1);
//tr1.setBackgroundResource(R.drawable.bunch);

// TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
//        TableRow.LayoutParams.WRAP_CONTENT);


et1=new EditText(this);
et1.setHeight(50);
et1.setWidth(50);
et1.setText("first row text box");

b1=new Button(this);
b1.setHeight(50);
b1.setWidth(50);
b1.setText("first row button");

//  b1.setLayoutParams(params);
tr1.addView(et1);  
tr1.addView(b1);
   tl.addView(tr1);
  // tr1.addView(et1,params);
//tr1.setGravity(Gravity.CENTER_HORIZONTAL);

TableRow tr2=new TableRow(this);
//tr2.setBackgroundResource(R.drawable.day);
// TableRow.LayoutParams params1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
//       TableRow.LayoutParams.WRAP_CONTENT);
// b2.setLayoutParams(params1);
et2=new EditText(this);
et2.setHeight(50);
et2.setWidth(50);
et2.setText("second row text box");

b2=new Button(this);
b2.setHeight(50);
b2.setWidth(50);
b2.setText("second row button");
b4=new Button(this);
b4.setHeight(50);
b4.setWidth(50);
b4.setText("second row second button");
tr2.addView(b4);
tr2.addView(et2);
tr2.addView(b2);

tl.addView(tr2);




TableRow tr3=new TableRow(this);
//tr3.setBackgroundResource(R.drawable.day);
// TableRow.LayoutParams params2 = new TableRow.LayoutParams
//                               (TableRow.LayoutParams.MATCH_PARENT,
//                              TableRow. LayoutParams.WRAP_CONTENT);
// b2.setLayoutParams(params2);

et3=new EditText(this);
et3.setHeight(50);
et3.setWidth(50);
et3.setText("third row text box");

b3=new Button(this);
b3.setHeight(50);
b3.setWidth(50);
b3.setText("third row button");
b3.setGravity(android.view.Gravity.RIGHT);
b3.setOnClickListener(this);
tr3.addView(et3);
tr3.addView(b3);

tl.addView(tr3);

setContentView(tl);
}


}

Android Example of Relative Layout in Code without Xml


package example.withoutxml;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class RelativeExample extends Activity implements OnClickListener
{
Button b,b1,b2;
  EditText et,et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


RelativeLayout rl=new RelativeLayout(this);
rl.setBackgroundResource(R.drawable.day);
// rl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
//                             LayoutParams.MATCH_PARENT));

b=new Button(this);
b.setWidth(50);
        b.setHeight(50);
       //b.setBackgroundResource(R.drawable.pinkrose);
   b.setText("click here");
        b.setGravity(Gravity.BOTTOM);
   b.setId(2);
     
   b1=new Button(this);
     b1.setWidth(50);
b1.setHeight(50);
b1.setId(4);
   
b2=new Button(this);
b2.setWidth(50);
b2.setHeight(50);
b2.setId(6);
b2.setText("click me");
     
   et=new EditText(this);
   et.setHeight(50);
   et.setWidth(50);
   et.setId(1);
     
   
   et1=new EditText(this);
   et1.setHeight(50);
   et1.setWidth(50);
   et1.setId(3);
     
   et2=new EditText(this);
   et2.setHeight(50);
   et2.setWidth(50);
   et2.setId(5);
     
   RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams
                                                       ( LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);



   rl.setLayoutParams(params);





// params.addRule(RelativeLayout.ALIGN_BOTTOM,1);
//b.setLayoutParams(params);

rl.setGravity(Gravity.FILL_VERTICAL);

//    
//
       et.setText("text");
       //et.setLayoutParams(params);
       rl.addView(et);
     
       RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams
            ( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);



       params1.addRule(RelativeLayout.BELOW,et.getId());
       params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
       et1.setLayoutParams(params1);
     
       //rl.addView(et1);
//        RelativeLayout.LayoutParams params7= new RelativeLayout.LayoutParams
//            ( LayoutParams.FILL_PARENT,
//               LayoutParams.WRAP_CONTENT);
//        params7.addRule(RelativeLayout.ALIGN_LEFT);
//        et1.setLayoutParams(params7);
       rl.addView(et1);
     
     
     
     
     
       RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams
                                                 ( LayoutParams.WRAP_CONTENT,
                                                      LayoutParams.WRAP_CONTENT);
   
                        params2.addRule(RelativeLayout.RIGHT_OF,et.getId());
                        b.setLayoutParams(params2);
                   
            rl.addView(b);





       RelativeLayout.LayoutParams params3= new RelativeLayout.LayoutParams
                                                   ( LayoutParams.WRAP_CONTENT,
                                                     LayoutParams.WRAP_CONTENT);
                params3.addRule(RelativeLayout.BELOW,b.getId());
                params3.addRule(RelativeLayout.RIGHT_OF,et1.getId());
                b1.setLayoutParams(params3);
               // rl.updateViewLayout(b1, params3);
                rl.addView(b1);
     
                 
                 
       RelativeLayout.LayoutParams params8= new RelativeLayout.LayoutParams
                                             ( LayoutParams.WRAP_CONTENT,
                               LayoutParams.WRAP_CONTENT);
       params8.addRule(RelativeLayout.BELOW,et1.getId());
       et2.setLayoutParams(params8);
       rl.addView(et2);
     
     
     
//      
//      
       RelativeLayout.LayoutParams params5= new RelativeLayout.LayoutParams
                                               ( LayoutParams.WRAP_CONTENT,
                                  LayoutParams.WRAP_CONTENT);
       params5.addRule(RelativeLayout.RIGHT_OF,et2.getId());
       params5.addRule(RelativeLayout.BELOW,b1.getId());
       b2.setLayoutParams(params5);
       rl.addView(b2);
       b2.setOnClickListener(this);
     
//    
//       rl.set
   
       //et.setLayoutParams(params);
//        rl.addView(et);
//        params.addRule(RelativeLayout.RIGHT_OF,et.getId());
//    b.setLayoutParams(params);      
//
//   rl.addView(b);
     
     
  setContentView(rl);

}


}

Android Example of Relative Layout in Java Code


package example.withoutxml;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class RelativeExample extends Activity implements OnClickListener
{
Button b,b1,b2;
  EditText et,et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);


RelativeLayout rl=new RelativeLayout(this);
rl.setBackgroundResource(R.drawable.day);
// rl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
//                             LayoutParams.MATCH_PARENT));

b=new Button(this);
b.setWidth(50);
        b.setHeight(50);
       //b.setBackgroundResource(R.drawable.pinkrose);
   b.setText("click here");
        b.setGravity(Gravity.BOTTOM);
   b.setId(2);
     
   b1=new Button(this);
     b1.setWidth(50);
b1.setHeight(50);
b1.setId(4);
   
b2=new Button(this);
b2.setWidth(50);
b2.setHeight(50);
b2.setId(6);
b2.setText("click me");
     
   et=new EditText(this);
   et.setHeight(50);
   et.setWidth(50);
   et.setId(1);
     
   
   et1=new EditText(this);
   et1.setHeight(50);
   et1.setWidth(50);
   et1.setId(3);
     
   et2=new EditText(this);
   et2.setHeight(50);
   et2.setWidth(50);
   et2.setId(5);
     
   RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams
                                                       ( LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);



   rl.setLayoutParams(params);





// params.addRule(RelativeLayout.ALIGN_BOTTOM,1);
//b.setLayoutParams(params);

rl.setGravity(Gravity.FILL_VERTICAL);

//    
//
       et.setText("text");
       //et.setLayoutParams(params);
       rl.addView(et);
     
       RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams
            ( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);



       params1.addRule(RelativeLayout.BELOW,et.getId());
       params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
       et1.setLayoutParams(params1);
     
       //rl.addView(et1);
//        RelativeLayout.LayoutParams params7= new RelativeLayout.LayoutParams
//            ( LayoutParams.FILL_PARENT,
//               LayoutParams.WRAP_CONTENT);
//        params7.addRule(RelativeLayout.ALIGN_LEFT);
//        et1.setLayoutParams(params7);
       rl.addView(et1);
     
     
     
     
     
       RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams
                                                 ( LayoutParams.WRAP_CONTENT,
                                                      LayoutParams.WRAP_CONTENT);
   
                        params2.addRule(RelativeLayout.RIGHT_OF,et.getId());
                        b.setLayoutParams(params2);
                   
            rl.addView(b);





       RelativeLayout.LayoutParams params3= new RelativeLayout.LayoutParams
                                                   ( LayoutParams.WRAP_CONTENT,
                                                     LayoutParams.WRAP_CONTENT);
                params3.addRule(RelativeLayout.BELOW,b.getId());
                params3.addRule(RelativeLayout.RIGHT_OF,et1.getId());
                b1.setLayoutParams(params3);
               // rl.updateViewLayout(b1, params3);
                rl.addView(b1);
     
                 
                 
       RelativeLayout.LayoutParams params8= new RelativeLayout.LayoutParams
                                             ( LayoutParams.WRAP_CONTENT,
                               LayoutParams.WRAP_CONTENT);
       params8.addRule(RelativeLayout.BELOW,et1.getId());
       et2.setLayoutParams(params8);
       rl.addView(et2);
     
     
     
//      
//      
       RelativeLayout.LayoutParams params5= new RelativeLayout.LayoutParams
                                               ( LayoutParams.WRAP_CONTENT,
                                  LayoutParams.WRAP_CONTENT);
       params5.addRule(RelativeLayout.RIGHT_OF,et2.getId());
       params5.addRule(RelativeLayout.BELOW,b1.getId());
       b2.setLayoutParams(params5);
       rl.addView(b2);
       b2.setOnClickListener(this);
     
//    
//       rl.set
   
       //et.setLayoutParams(params);
//        rl.addView(et);
//        params.addRule(RelativeLayout.RIGHT_OF,et.getId());
//    b.setLayoutParams(params);      
//
//   rl.addView(b);
     
     
  setContentView(rl);

}


}