Back to Posts

Android – Creating First New Project – 2

This tutorial is the continuation of this tutorial Android – Creating First New Project – 1. In the previous tutorial we learnt to create a new Android Project. Here In this tutorial first of all we will learn about the project structure and then we will learn about the coding in Java and XML and run our app. 

After creating our new project we will get the project structure as shown in image.

Now we will learn about some basic files in project structure.

  • src – Directory for your app’s main source files. By default, it includes an Activity class that runs when your app is launched using the app icon.
  • gen – It contains auto generated code like R.java. This files creates a link between your activity file (java file) and layout file (xml file).
  • res –  It contains several sub directories for app resources –
    • drawable-hdpi/ldpi/mdpi/xhdpi/xxhdmi –Directory for drawable objects (such as bitmaps) that are designed for high-density (hdpi) ,low-density (ldpi), medium-density (mdpi), extra high-density (xhdpi) and double extra high-density (mdpi) screens.
    • Layout – Directory for files that define your app’s user interface.
    • Values – Directory for other various XML files that contain a collection of resources, such as string definitions.
  • Manifest file – The manifest file describes the fundamental characteristics of the app and defines each of its components.

Here we are going to make an app in which there will be three objects – EditText, TextView and Button. EditText is just a text box where we input something, TextView is just a lable where we can display any kind of text, and you must be familiar about the work of Button. Here we will enter any text inside the EditText and click on the button, after clicking the text that we have entered must be printed on the TextView. So this is what we are going to make in this app. So let’s start !!

1. First of all we will create our layout. So go to res –> layout –> activity_main.xml and double click on this file.

2. Now copy and paste the code there as given below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/LinearLayout1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<EditText
 android:id="@+id/text"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:ems="10"
 android:hint="Enter Text" >

<requestFocus />
 </EditText>

<Button
 android:id="@+id/print"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginTop="20dp"
 android:onClick="click"
 android:text="Print" />

<TextView
 android:id="@+id/label"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginTop="20dp"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

3. Now save the file with combination of ctrl+s and close the file.

4. So now our layout will look like this as shown in figure.

5. Now go to src –> com.apptronix.firstproject –> MainActivity.java and open this file.

6. Now copy and paste the code there as given below.

package com.apptronix.firstproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

// Declare variable for each object in xml file
 // ctrl+space for suggestion

Button b1;
 EditText e1;
 TextView t1;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

// linking each variable with id on xml

b1 = (Button) findViewById(R.id.print);
 e1 = (EditText) findViewById(R.id.text);
 t1 = (TextView) findViewById(R.id.label);

}

// create method for onclick on button

public void click(View v) {
 String name = e1.getText().toString(); // fetching text from edittext
 // and converting it into string

t1.setText(name); // printing the text from editext to textview

e1.setText(""); // clearing the edittext
 }

}

7. Now save the file with combination of ctrl+s and close the file.

8. Now we have to run the file. Right click on project, go to Run As –> Android Application.

Complete Video Tutorial :

Thank you for visiting our tutorial. If you have any doubts or queries regarding this, you can comment below in our comment section. Don’t forget to share the tutorial if you like it.

Previous Part : Android – Creating First New Project – 1

Share this post

Comments (8)

  • job search Reply

    Excellent article! We are linking to this great content on our website. Keep up the great writing.

    June 25, 2017 at 4:54 pm
  • student loans Reply

    I would like to thank you for the efforts you’ve put in writing this site. I really hope to view the same high-grade blog posts by you later on as well. In fact, your creative writing abilities has encouraged me to get my own, personal website now 😉

    June 28, 2017 at 12:16 am
  • career guidance Reply

    Hey! Someone in my Myspace group shared this site with us so I came to take a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Excellent blog and wonderful style and design.

    June 29, 2017 at 3:29 am
  • finance Reply

    I enjoy what you guys are usually up too. This type of clever work and coverage! Keep up the superb works guys I’ve included you guys to my personal blogroll.

    July 2, 2017 at 3:16 am
  • finance Reply

    Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I am going to return yet again since I book-marked it. Money and freedom is the best way to change, may you be rich and continue to help others.

    July 2, 2017 at 11:29 pm
  • financial advice for young people Reply

    Very nice write-up. I certainly love this website. Keep writing!

    July 3, 2017 at 10:16 pm
  • health Reply

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but other than that, this is wonderful blog. An excellent read. I’ll certainly be back.

    July 5, 2017 at 4:59 am
  • health services Reply

    I visited various sites except the audio quality for audio songs present at this web page is genuinely wonderful.

    July 6, 2017 at 1:52 am

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Posts