Hello World Example:
1.Layout XML File:
<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=".HelloWorldExample_MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="26dp"
android:text="Enter Name" />
<EditText
android:id="@+id/name_editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name_editText1"
android:layout_below="@+id/name_editText1"
android:layout_marginLeft="48dp"
android:layout_marginTop="30dp"
android:onClick="welcome"
android:text="Submit" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="28dp"
android:text="Welcome" />
</RelativeLayout>
After writing this much code you will get Following UI screen:
// We dont have to write everything by ourself ,we can do just drag and drop from Palette.
2. .JAVA file for our Activity :
package com.solomon.HelloWorldExample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class HelloWorldExample_MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_example__main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world_example__main, menu);
return true;
}
public void welcome(View view) {
//While clicking button1 this welcome method will call of onclick.
String name;
TextView welcomeTextView;
EditText nameEditText;
nameEditText=(EditText)findViewById(R.id.name_editText1);
//Using EditText we can get values from user
//Here,I am getting a name and storing in nameEditText
welcomeTextView=(TextView)findViewById(R.id.textView2);
//TextView is more like a Label.
name=nameEditText.getText().toString();
//Getting Text from EditText and storing it in name(local variable).
welcomeTextView.setText("Hello"+name+".Welcome to Android World");
//whatever value stored in name is added with 2 more string and stored in WelcomeTextView.
}
}
3.AndroidMainfest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solomon.HelloWorldExample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.solomon.HelloWorldExample.HelloWorldExample_MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment