Skip to main content

First App Android: How To Create An Android App With Android Studio (Hello World and Simple Login with Toast)

Here’s the few step to make first app on android studio, just 5 steps. Okay, let’s begin.

1. First, create Main.java for main activity. Here’s the code:

package com.example.helloworld;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Main extends AppCompatActivity {

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

        Button btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickNext();
            }
        });

        Button btnExit = (Button) findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public void onClickNext() {
        Intent intent = new Intent().setClass(this, LoginActivity.class);
        startActivity(intent);
    }

}

2. Second, put code below to main.xml for main activity interface

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello_world"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next"
            android:id="@+id/btnNext"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dip"
            android:text="Exit"
            android:id="@+id/btnExit"/>


    </LinearLayout>

</LinearLayout>

main.xml

3. Third, create new activity for LoginActivity.java

package com.example.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    Button btnLogin, btnExit;
    EditText txtUser, txtPass;

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

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnExit = (Button) findViewById(R.id.btnExit);
        txtUser = (EditText) findViewById(R.id.txtUser);
        txtPass = (EditText) findViewById(R.id.txtPass);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String txt = "Your username is: "
                        + txtUser.getText().toString()
                        + "\nYour password is: "
                        + txtPass.getText().toString();
                Toast.makeText(LoginActivity.this, txt.trim(),
                        Toast.LENGTH_LONG).show();
            }
        });

        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                return;
            }
        });
    }
}

4. Fourth, then put this code to login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dip">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal">

        <TextView
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="Username:"
            android:id="@+id/lblUser"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtUser"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:orientation="horizontal">

        <TextView
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="Password:"
            android:id="@+id/lblPass"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txtPass"
            android:password="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:gravity="right"
        android:id="@+id/linearLayout3">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Login"
            android:id="@+id/btnLogin"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exit"
            android:id="@+id/btnExit"/>

    </LinearLayout>

</LinearLayout>

2

5. Fifth, modify your AndroidManifest.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld" >

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

    </application>
<uses-sdk android:minSdkVersion="7"/>
</manifest>

Finally, Run your app with emulator or your mobile device

3

4

you can set textview as a password field for make the text of password to asterisk or dot like the picture above

thanks for reading my blog, any error or something you can comment below

Comments

Popular posts from this blog

Tutorial Java Servlet

Kumpulan Video Coding

https://www.youtube.com/user/TheGeekStyle/playlists https://www.youtube.com/playlist?list=PLxOOFFsKqfQQfgkcRBS0gJZv7Eo4DlzW-

Software Developer Hiring - Coding Test at HackerRank

       Hi all, wherever you are, whatever you do, just whatever. Ya Allah gua udah kaga ngoding lagi malah dapet coding interview di hackerRank, gua udah milih jalan menjadi system admin seperti di sekolah dulu yang berhubungan dengan Teknik Komputer dan Jaringan, tapi gua ga pilih jaringan yang dimana seharusnya itu menjadi pilihan gua karena itu ga ada coding codingan tapi gua malah pilih devops karena sedang booming padahal itu sama aja sebenernya kayak sysadmin cuma versi 4 kata mas Lazuardi.      Di umur gua yang ke 25 waktu itu gua udah memilih dan memutuskan untuk pergi dari karir programmer/software developer/software engineer/whatever they callled. Gua lebih memilih dan nyaman dan lain-lain dengan kerjaan yang berhubungan dengan server/vps seperti menginstall web server, generate ssl dari certbot nya lestencrypt, pointing domain, install mysql server, konfigurasi web server untuk laravel, etc.      Tidak menyebutkan brand tapi a...