Python Tutorial

Showing posts with label Android button. Show all posts
Showing posts with label Android button. Show all posts

Friday, January 14, 2011

Android button/Android button action/AbsoluteLayout/Positioning

This example is very similar to my previous example on button action.

Here main difference is positioning your sereen UI component using AbsoluteLayout.
By using android AbsoluteLayout we can easily make positioning.We can positioning using xml or from code.
In this example i showed both.


//my_button_interface.xml

<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20px" android:layout_y="200px" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" /> </AbsoluteLayout>

//HelloWorldAndroid.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;

public class AndroidButton extends Activity implements OnClickListener {
 /** Called when the activity is first created. */
  
 Button btn;
 TextView tv;
  
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.my_button_interface);
     
  btn = (Button) findViewById(R.id.myButton);
  tv = (TextView) findViewById(R.id.myTextViev);
  
  AbsoluteLayout.LayoutParams layoutParams= new
                AbsoluteLayout.LayoutParams(100,50, 200, 200);
  btn.setLayoutParams(layoutParams);
     
  tv.setText("This is sample text");
  btn.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v.getId() == R.id.myButton) {
   tv.setText("welcome to AbsoluteLayout");
  }
 }
}

output:



Before click button


Image not found


After click button


Image not found

Android button/Android button action

UI design in android is very faster using xml

Here i am using linearLayout.
From this example we will know how to create a button and then give this button action.Lets see

Xml file description:
     At first i declared here a textview wich will changed after button action performed.
Each elements need a ID, for example textview id is myTextViev. we also define each content layout size and position (AbsoluteLayout).
Then i declared a button (id=myButton,name=Submit) and set button width height


Now you need to create a file named 'my_button_interface.xml' then paste the code below

//my_button_interface.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:id="@+id/myTextViev" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/myButton" android:layout_width="100px" android:layout_height="50px" android:text="Submit" /> </LinearLayout>

Code description:

First you need to set layout using setContentView(R.layout.my_button_interface).Now you get all elements of this layout.
If we want to get declare text view code is tv = (TextView) findViewById(R.id.myTextViev),now you
can set text in this textview.
For button action you need to implements OnClickListener,then everything is easy....

//AndroidButton.java

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class AndroidButton extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button btn; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_button_interface); btn = (Button) findViewById(R.id.myButton); tv = (TextView) findViewById(R.id.myTextViev); tv.setText("This is old text"); btn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.myButton) { tv.setText("This is new text"); } } }
output:


Before click button


Image not found
After click button


Image not found