AndroidActivity

Android Activity – 2

From the previous tutorial, our Activity is ready for implementing our defined functionalities. Every View that we define inside the layout file will be referred here to perform the required feature with it. Well as there are XML tags present inside the layout, so every XML tag is nothing but a JAVA class of android. It is easy to declare the object for that class and can manage the operations on it.

As we have below four Views inside login layout as XML tags-

Layout to arrange views – LinearLayout

input field to enter username – EditText

input field to enter password – EditText

button to submit the details – Button

these XML tags are nothing but JAVA classes in Android. Hence, we can just create an object for them inside over activity class and refer them to XML tags. Here is the code for them.

package esprinkle.tastingandroid;

import android.app.Activity;

import android.widget.Button;

import android.widget.EditText;

public class Login extend Activity{

EditText username, password;

Button loginBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);

     }

}

As we just created the object for the Edittext and Button classes. They can now act as the reference to the Views defined inside layout. So we call the views here by using the below method.

findViewById(R.id.username);

The defined view inside the layout is fetched by its ‘Id’.  The ‘username’ with ‘R.id.username’ is the parameter mentioned as the id of the EditText. This will fetch us the username EditText from the layout. The fetched value can be assigned to username object by typecasting it as below.

username = (EditText) findViewById(R.id.username);

Now the username EditText is ready to fetch the value entered inside it. So for the EditText object, there are several set of methods which can be used to use the perform operations on it. Here is the method to get the text from the EditText.

String usernameText = username.getText().toString();

Using getText() method, we can fetch the text entered by the user inside it. The same above method is repeated for the password EditText View to fetch the password text entered by the user. The code after defining the above methods will get updated as below.

package esprinkle.tastingandroid;

import android.app.Activity;

import android.widget.Button;

import android.widget.EditText;

public class Login extend Activity{

EditText username, password;

Button loginBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);

username = (EditText) findViewById(R.id.username);

password = (EditText) findViewById(R.id.password);

loginBtn = (Button) findViewById(R.id.loginBtn);

}

}

As we have to define the logic of the login on click of the ‘Login’ Button. So there is a method for the Button class which helps in performing the same.

 

loginBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

            }

        });

setOnClickListener method which will get triggered when the Login Button is clicked. The parameter it takes is the object of View’s onClickListener. Hence as shown above the logic should be written inside the onClick method.

The Complete Code :

import android.app.Activity;

import android.widget.Button;

import android.widget.EditText;

import android.view.View;

public class Login extend Activity{

EditText username, password;

Button loginBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);

username = (EditText) findViewById(R.id.username);

password = (EditText) findViewById(R.id.password);

loginBtn = (Button) findViewById(R.id.loginBtn);

loginBtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

            }

        });

}

}

download this tutorial

314 total views, 1 views today

Leave a Reply