Pages

Saturday, September 8, 2012

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

}


}