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

© Copyright 2016 – Apptronix Technologies. All Rights Reserved. ISO 9001:2015 Certified