Pages

Saturday, September 8, 2012

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

}


}

Example of Linear Layout in Java 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.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class examplenoxml extends Activity {
 



    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);
       
   
        LinearLayout ll=new LinearLayout(this);
        ll.setBackgroundColor(R.drawable.day);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setBackgroundResource(R.drawable.bunch);
        ll.setGravity(LinearLayout.VERTICAL);
       // ll.setLayoutParams(LinearLayout.)
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                                    LayoutParams.MATCH_PARENT));
     
        EditText et=new EditText(this);
        EditText et1=new EditText(this);
        ProgressBar pb=new ProgressBar(this);
        pb.setPressed(false);
        pb.setLayoutParams(new LayoutParams
                    (LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));
     
        et1.setWidth(100);
        et1.setText("text");
        et1.setTextSize(50);
        et1.setSelection(et1.getText().length());
       // et.setGravity(Gravity.LEFT);
        ll.addView(et);
        ll.addView(et1);
        ll.addView(pb);
       // ll.addView(list);
        Button b1=new Button(this);
        ll.addView(b1);
        b1.setWidth(50);
        b1.setHeight(50);
        b1.setBackgroundResource(R.drawable.pinkrose);
        b1.setText("click here");
        b1.setGravity(Gravity.BOTTOM);
       
        b1.setOnClickListener(this);
       setContentView(ll);
    }
}

Android Example of Linear 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"
    >
<TextView android:text="Register Page"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout


android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="username"
    />


<EditText android:id="@+id/editText1"
android:layout_width="match_parent"
android:text=""
android:gravity="center"
android:layout_gravity="center_horizontal"
 android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp">
 </EditText>
 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="password"
    />
<EditText android:id="@+id/editText2"
 android:layout_width="match_parent"
  android:text="EditText"
   android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp">
   </EditText>
   <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="re-enter password"
    />
<EditText android:id="@+id/editText3"
android:layout_width="match_parent"
android:text="EditText"
 android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:layout_marginBottom="20dp">
 </EditText>
 <TextView
 android:text="gender"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
 <RadioGroup
 android:tag="gender"
 android:orientation="vertical"
 android:gravity="left"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">

 <RadioButton
 android:text="male"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/male_radio"
 />

 <RadioButton
 android:text="female"
  android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/female_radio"
 />


 </RadioGroup>
  <TextView
  android:id="@+id/txtRadio"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="nothing"
    />
 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="choose hobby"
    />
<CheckBox android:text="singing"
 android:id="@+id/checkBox1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  </CheckBox>
<CheckBox android:text="playing"
 android:id="@+id/checkBox2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  </CheckBox>
<CheckBox android:text="reading"
 android:id="@+id/checkBox3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 </CheckBox>

<Button android:text="submit"
 android:id="@+id/button1"
  android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   </Button>
   </LinearLayout>
</ScrollView>
</LinearLayout>

Android Example of Table Layout


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row1 text" >
        </EditText>

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row1 Button" >
        </Button>
    </TableRow>

    <TableRow>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-2 Text" >
        </EditText>

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-2 Button" >
        </Button>

        <EditText
            android:id="@+id/editText3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-2 Text" >
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-2 Button" >
        </Button>
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-3 Button" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-3 Button" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="row-3 Button" >
        </Button>
    </TableRow>

</TableLayout>

Android Example of relative layout


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <TextView
    android:layout_height="wrap_content"
    android:text="username"
    android:id="@+id/first_text"
   android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_width="wrap_content"/>
 
 
    <EditText android:text="EditText"
    android:id="@+id/editText1"
     android:layout_height="wrap_content"
     android:layout_toRightOf="@id/first_text"
 
      android:layout_width="fill_parent">
     </EditText>
     <TextView
    android:layout_height="wrap_content"
    android:text="password"
    android:id="@+id/second_text"
     android:layout_below="@id/first_text"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
     android:layout_width="wrap_content"/>
    <EditText android:text=""
      android:id="@+id/editText2"
      android:layout_height="wrap_content"
       android:layout_toRightOf="@id/second_text"
       android:layout_below="@id/editText1"    
       android:layout_width="fill_parent">
       </EditText>
 
      <RadioButton
      android:text="male"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
      android:layout_below="@id/editText2"
      android:id="@+id/radio_button1"
      android:layout_toRightOf="@id/second_text"
      />
      <RadioButton
         android:text="female"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
      android:layout_below="@id/radio_button1"
      android:layout_toRightOf="@id/second_text"
      />
      <Button
       android:layout_alignParentBottom="true"
      android:text="Button"
     android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
    >
      </Button>
    <Button
    android:layout_alignParentBottom="true"
    android:text="Button"
     android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button1">
      </Button>
</RelativeLayout>