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.
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.
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.
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.
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.
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.
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 :
download this tutorial
423 total views, 1 views today