Friday, 22 June 2012

simple android program using button and onclickListener

simple android program using button and onclickListener


RelativelayoutActivity.java


package a.b;

import android.app.Activity;
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;

public class RelativelayoutActivity extends Activity implements OnClickListener {
Button b1;
EditText e;
TextView t1;
String s1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        e=(EditText)findViewById(R.id.ediUserName);
        b1=(Button)findViewById(R.id.btnGo);
        t1=(TextView)findViewById(R.id.lblUserName);
        b1.setOnClickListener(this);
        
    }
public void onClick(View v) {
// TODO Auto-generated method stub
s1=e.getText().toString();
if(v==b1)
{
t1.setText(s1);
}
}
}



main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/myRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/lblUserName"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#ff000066"
android:text="User Name"
android:textStyle="bold"
android:textColor="#ff00ff00"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</TextView>
<EditText
android:id="@+id/ediUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lblUserName"
android:layout_alignParentLeft="true"
android:layout_alignLeft="@+id/myRelativeLayout"
 android:padding="20dip">
 </EditText>
 <Button 
 android:id="@+id/btnGo"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
  android:layout_below="@+id/ediUserName" 
  android:layout_alignRight="@+id/ediUserName" 
  android:text="Go" 
  android:textStyle="bold">
  </Button>
  <Button
   android:id="@+id/btnCancel" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"
 android:layout_toLeftOf="@+id/btnGo" 
 android:layout_below="@+id/ediUserName" 
 android:text="Cancel" 
 android:textStyle="bold">
 </Button>
 </RelativeLayout>

No comments:

Post a Comment