Powered by Blogger.

Followers

Sunday, 24 February 2013

TextView Button and EditText

Posted by Sanjeet A On 09:00 No comments
Introduction To Basic Widgets:-
Today we will make a simple app which will display some stuff given as input. Basic widgets are TextView , Button , EditText etc.So create a new android project
                    App Name:SimpleWidgets
                    ProjectName:SimpleWidgets
                    package Name:in.blogspot.simplewidget
                    Class Name: WidgetsActivity
                    Layout:activity_widget


Now Open Layout xml From Package Explorer and edtit activity_widget.xml as:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".WidgetActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Please Fill The Details" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etNo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Number"
        android:inputType="phone" >
    </EditText>

    <EditText
        android:id="@+id/etAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Address"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/bDisplay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Display" />

    <TextView
        android:id="@+id/tvShow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="-----" />

</LinearLayout>



Switch  it to  Graphical view you will see like this:-
Now select your class file from Package Explorer src>in.blogspot.simplewidget>WidgetsActivity.java
Edit it-




package in.blogspot.thenewtechnoworld;

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

public class WidgetActivity extends Activity implements OnClickListener {
TextView show;
Button display;
EditText name, address, no;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// seting reference to the layout stuffs
setContentView(R.layout.activity_widget);
show = (TextView) findViewById(R.id.tvShow);
display = (Button) findViewById(R.id.bDisplay);
name = (EditText) findViewById(R.id.etName);
address = (EditText) findViewById(R.id.etAdd);
no = (EditText) findViewById(R.id.etNo);

display.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_widget, menu);
return true;
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ur_name = name.getText().toString();
String ur_no = no.getText().toString();
String ur_address = address.getText().toString();

show.setText("name:" + ur_name + "\n" + "No:" + ur_no + "\n"
+ "Address:" + ur_address);

}

}


Save it and run. You will see out put as:-



So you finished this app! Enjoy...If getting problem Please comment!!
You can download the source code here, But you will have to be Loged in to Facebook.
Thanks for reading this blog.




0 comments:

Post a Comment

Site search