Demo 1 (contd...)
In the last blog we designed the User Interface for our application. Now we will write the business logic so that when the user enters the name in the Edit Text and clicks on submit then the Application displays the User name.
The business logic is written in the java file. We have one java file with the name MainActivity.java.
Now write the following code in the MainActivity.java
package com.example.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
EditText name;
Button submit;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText1);
display=(TextView)findViewById(R.id.textView1);
submit=(Button)findViewById(R.id.button1);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user_name;
user_name=name.getText().toString();
display.setText("Your Name is: "+user_name);
}
});
}
}
In the last blog we designed the User Interface for our application. Now we will write the business logic so that when the user enters the name in the Edit Text and clicks on submit then the Application displays the User name.
The business logic is written in the java file. We have one java file with the name MainActivity.java.
Now write the following code in the MainActivity.java
package com.example.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
EditText name;
Button submit;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText1);
display=(TextView)findViewById(R.id.textView1);
submit=(Button)findViewById(R.id.button1);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user_name;
user_name=name.getText().toString();
display.setText("Your Name is: "+user_name);
}
});
}
}
This comment has been removed by the author.
ReplyDeleteThanks for sharing the code!! It's really working.
ReplyDeleteIt's my pleasure.
Delete