Today we will learn to display message with new things. for example you will write your name in edittext you will het hie name...
- to make this thing we need a..
- Button for click
- edittext to write your name
- toast for display message
About application:
- this simple app contain.when you write your name on edittext it will display message with hie.
Names :
- java file::MainActivity.java
- xml file::activity_main.xml
STEP 1:: activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:ems="10" />
<Button
android:id="@+id/bnt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="clickme" />
</RelativeLayout>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:ems="10" />
<Button
android:id="@+id/bnt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt1"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="clickme" />
</RelativeLayout>
STEP 2:: MainActivity.java
package com.example.athome;
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.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button bnt1;
EditText txt1;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1=(EditText) findViewById(R.id.txt1);
bnt1=(Button)findViewById(R.id.bnt1);
str=txt1.getText().toString();
bnt1.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.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId()==R.id.bnt1)
{
Toast.makeText(this,"hie "+txt1.getText(), Toast.LENGTH_LONG).show();
}
}
}
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.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button bnt1;
EditText txt1;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1=(EditText) findViewById(R.id.txt1);
bnt1=(Button)findViewById(R.id.bnt1);
str=txt1.getText().toString();
bnt1.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.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId()==R.id.bnt1)
{
Toast.makeText(this,"hie "+txt1.getText(), Toast.LENGTH_LONG).show();
}
}
}
Here ends our 2 lesson.if you like then comment it.
No comments:
Post a Comment