Thursday, 12 October 2017

ANDRIOD PROGRAMMING.

Hi, folks in this post I am teaching the basics of the android studio.

What is an android?
Android is a programming language and it is based on Linux for mobile devices such as tablet computers and smartphones. It is developed by Google and later the OHA (Open Handset Alliance). so its open source so any OEM(original equipment manufacturer can modify it.

What is an android studio?
Android Studio is a tool to develop android apps and it is the official IDE(integrated development environment) for Android programming language from Google.

How do I install the android studio in my Computer?
You can visit developer.android.com to download the latest version of the android studio for your platform.after installing android studio you need to install JDK(java development kit)because android uses java to write the logic so to download latest JDK click here. 


Let's start, creating a simple Android app(Hello Word app).
After successful installation of  Android studio follow these steps.
  1. In Android Studio, create a new project:
    • If you don't have a project opened, in the Welcome to Android Studio window, click Start a new Android Studio project.
    • If you have a project opened, select File > New Project.
  2. In the New Project screen, enter the following values:
    • Application Name: "My First App"
    • Company Domain: "example.com"
    You might want to change the project location, but leave the other options as they are.
  3. Click Next.
  4. In the Target Android Devices screen, keep the default values and click Next.
    If you're curious about how these SDK versions affect your app, read Supporting Different Platform Versions.
  5. In the Add an Activity to Mobile screen, select Empty Activity and click Next.
  6. In the Customize the Activity screen, keep the default values and click Finish.
  7. After some processing, Android Studio opens the IDE. Now take a moment to review the most important files.
    First, be sure the Project window is open (select View > Tool Windows > Project) and the Android view is selected from the drop-down list at the top of that window. You can then see the following files:
    app > java > com.example.myfirstapp > MainActivity.java
    This is the main activity (the entry point for your app). When you build and run the app, the system launches an instance of this Activity loads its layout.
    app > res > layout > activity_main.xml
    This XML file defines the layout for the activity's UI. It contains a TextView element with the text "Hello world!".
    app > manifests > AndroidManifest.xml
    The manifest file describes the fundamental characteristics of the app and defines each of its components.





That's it your hello world app is ready you don't need to write a code for it, it is already predefined in android studio,
however, if you are looking for code then see this

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

you can replace "Hello World!" with any text .
EX:<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hi wasup!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

and also you can change the text sizes and you can add colors and lot more to your text please refer google documentation if you want to learn more about them.



then click the green triangle button to run your app, there are two ways to run your app
1)if you don't have an android device then you can run your app in the emulator.
2)if you have an android device then connect it to your computer through USB cable and enable USB debugging mode and then run it.I recommend running on your device only because emulator needs a lot of memory and it is slower than your real device.


3)Simple Addition program
java code:
package com.example.kirankumar.simple;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText et1,et2;
    TextView tv;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=(EditText)findViewById(R.id.et1);
        et2=(EditText)findViewById(R.id.et2);
        tv=(TextView) findViewById(R.id.tv);
        btn=(Button)findViewById(R.id.btn);
    }

    public void btn(View view) {
        int a,b,sum;
         a=Integer.parseInt(et1.getText().toString());
         b=Integer.parseInt(et2.getText().toString());
        sum=a+b;

        tv.setText(Integer.toString(sum));




    }
}


XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kirankumar.simple.MainActivity">


    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et2"
        android:onClick="btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="ADD" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="57dp"
        android:text=""
        android:textSize="25sp"
        android:textColor="@android:color/holo_blue_bright"
        android:layout_below="@+id/btn"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>


feel free to ask if you any doubts from above topic I will meet you in next post with a new topic.



No comments:

Post a Comment