Android Application Development is a growing at a high pace. And in order to keep up with the programming world, it is always advised to keep learning new things as soon as they come. But remember that learning everything is not possible. You have to be selective on what you want to learn and master upon. If you choose to learn Android Application Development, you must master it using whichever way you find easy. You can develop android applications using various methods, but it is your choice which one to learn and master!
Without taking much of your time, I would like to introduce you to the second Android Application that you would make. If you have no experience in developing Android applications and the very basics of it, i suggest you to read these articles before proceeding further.
- Getting Started with Android App Development : Get your Tools Ready
- Set up an Android Virtual Device on Eclipse
- Creating Your First Android Application
After you make yourselves aware with the basics of Android Application development and how to create a new application and also how to run it on an Android Virtual Device, proceed under.
Basic Elements of An Android Application
In this section, I would like to tell you how to add some basic elements to an Android Application. These basic elements will include a Text View (an element where any text can be displayed), an Edit Text field (where an input from the user can be taken) and at last, a button (which can be clicked to do some action). We are going to develop an application that takes and input string from the user and displays it below the button. Let’s get started with this now.
- Open your project in Eclipse and open the “activity_main.xml” file from the layouts folder by double clicking on it. You should see some similar Graphical Layout.
- On the Left side, you will see the Palette menu. Under the “Form Widgets”, click and drag Large Text and Button one after the other on to the Application layout. Now arrange them like in the image below.
- Now under the “Text Fields” in the Palette menu, click and drag Plain Text right above the button and now it should look like this.
- Click on the button and on the right side, in the properties menu change the id to “@+id/submit” and when asked if you want to update references, update them at once. Next, change the Text filed in the Properties to “Submit Text”.
- Next, click on the Large Text view we created and change the Text in the Properties Menu to anything you want. I changed it to “Text Displayed Here”.
- Now, click on the Edit Text field and here, enter the hint for the edit text in the properties menu again. I changed it to “Enter a string here”. This is how it should look right now. Save the file ( Ctrl + S).
- Now we are done with designing the User Interface. Now let us go into java and define the code for the thing we need done. Open the “MainActivity.java” file from src -> com.slashcoding.helloworld under the Hello World Project in the Project Explorer on the right.
- Now in the onCreate method, add the following lines of code. After you add these lines, press Ctrl + Shift + O to import the required classes into your java file. Basically by adding these lines of code, we are defining variables of Button, EditText and TextView classes to java and then referencing them with the elements in the XML file we created earlier.
Button b = (Button) findViewById(R.id.submit); final EditText et = (EditText) findViewById(R.id.editText1); final TextView tv = (TextView) findViewById(R.id.textView1);
- Now, we will sent a on Click Listener for the button. This basically defines all the things we need to do when the button is clicked. Add this code to the onCreate function as well, just below the TextView definition.
b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText(et.getText().toString()); } });
In this code, we have defined an onClickListener for the button and asked it to set the text in our Text View to be equal to the text in the Edit Text field, which is entered by the user.
- That’s all. You are done. Now save this java file and run your application either on a physical Android Device or on an Android Virtual Device. This is how your onCreate Function should look like now.
Well, that’s all for now. This is a simple tutorial to understand the use of Edit Text, Button and a Text View. You can use these to create powerful applications that can do various things. I will come back to on your various applications that you can make by just using the things I described here and some common sense. Stay updated with all the new posts from Slash Coding right on your Facebook Wall, Twitter feed or in your Favorite RSS Reader. Also, if you face any problem, feel free to contact me using the contact page or commenting below!
Evie Marian says
I am sure this article has touched all the internet viewers,
its really really nice piece of writing on building up new application.
Braxton says
I constantly spent my half an hour to read this weblog’s content every day along with a mug of
coffee.
Aneesh Bhatnagar says
Thank you for such a valuable feedback Braxton! 🙂