- Create Android project visit my previous example First Android Application in Eclipse Hello world Android Example, Un check the
Add OnClickListener to Button and Send Data to next Activity
// Set OnClickListner to the login button
Button login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get user Name
EditText loginName = (EditText) findViewById(R.id.txt_userName);
String name = loginName.getText().toString();
// create Intent and set LoginSuccess Activity
Intent intent = new Intent(getApplicationContext(),
LoginSuccess.class);
// put values to intent which will get in the LoginSuccess Activity
intent.putExtra("name", name);
// Start LoginSuccess Activity
startActivity(intent);
}
});
Create AndroidLoginActivity
Right the project and create Java class and name it as AndroidLoginActivity, copy and past bellow code.
package jsupport.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidLoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
/* Set OnClickListner to the login button */
Button login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText loginName = (EditText) findViewById(R.id.txt_userName);
String name = loginName.getText().toString();
Intent intent = new Intent(getApplicationContext(),LoginSuccess.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
}
}
Create new layout for AndroidLoginActivity
Right click on res/layout , New-> Android XML give the file name as login , copy and past bellow xml
Get Value from Intent
Create LoginSuccess Activity
Right click on res/layout , New-> Android XML give the file name as login , copy and past bellow 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:text="Login here" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="180dp" ></TextView>
<TextView android:text="User Name " android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView2" android:layout_height="wrap_content" android:layout_width="126dp"></TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/txt_userName" android:layout_width="272dp">
<requestFocus></requestFocus>
</EditText>
<TextView android:text="Password" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/password" android:layout_width="272dp"></EditText>
<Button android:text="Login" android:layout_height="wrap_content" android:layout_width="126dp" android:id="@+id/btn_login" ></Button>
</LinearLayout>
Get Value from Intent
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("name");
Create LoginSuccess Activity
Right the project and create Java class and name it as LoginSuccess , copy and past bellow code.
package jsupport.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class LoginSuccess extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
TextView txt_loggedName = (TextView) findViewById(R.id.txt_loggedName);
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("name");
txt_loggedName.setText(name);
}
}
Right click on res/layout , New-> Android XML give the file name as success, copy and past bellow xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:weightSum="1">
<TextView android:text="Hello" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_width="158dp" android:layout_height="wrap_content" android:layout_weight="0.06"></TextView>
<TextView android:text="Java Srilankan Support" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/txt_loggedName"></TextView>
</LinearLayout>
Add AndroidLoginActivity and LoginSuccess to AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jsupport.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="LoginSuccess"></activity>
</application>
</manifest>
AndroidLoginActivity is the main activity
Download application source code from here Simple Android Login

5 comments:
Hi....In that above code it shows one error for me...whats that mean...in that AndroidloginActivity file On that XML file name is the error...I dont know how can i clear that ...plz help me...
What is the error ? did not you download the Android Login source code . download Simple Android Login from here
package test.android;
import com.mkyong.android.R;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Screen1 extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
*** button.setOnClickListener(new OnClickListener() { *********
public void onClick(View arg0) {
Intent intent = new Intent(context, Activity.class);
startActivity(intent);
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
}
}
where i put ******* sympol know,that was the error (setOnClickListener).
I got it :) :)
Thank U :)
How to pass the edittext to a listview
Post a Comment