Membuat Launcher Sederhana

Bagaimana membuat launcher di Android Studio? Kalau sederhana sih mudah, tinggal tempel link dan beri alamat link-nya. Di bawah ini dijelaskan langkah menyusun launcher sederhana itu khusus untuk link ke Google Service. Silahkan dikembangkan lagi.

Untuk memudahkan latihan ini silahkan download dulu file Android Studionya di link download ini. Lalu ekstrak dan bukalah sebagai proyek aplikasi Android Studio. File apk-nya dapat didownload melalui link berikut: download.

Okay, kita bahas secara singkat langkah-langkah penyusunannya.

1. Siapkan gambar/ikon/button sesuai link yang akan ditempelkan di layout. Contohnya seperti dalam gambar di bawah ini

google

2. Buatlah proyek Android Studio dari menu File –> New Project, seperti tulisan sebelumnya. Sebagai contoh nama aplikasnya (Application name) = Google Service, Package Name = google.service, dan file xml layoutnya adalah google_service.xml.

3. Isi file layout google_service.xml dapat dilihat seperti berikut

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".GoogleService" >

   <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/search"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/google"
            android:contentDescription="@string/todo"
            android:text="@string/search" />

        <Button
            android:id="@+id/gmail"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/gmail"
            android:contentDescription="@string/todo"
            android:text="@string/gmail" />
    </LinearLayout>

      <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/translate"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/translate"
            android:contentDescription="@string/todo"
            android:text="@string/translate" />

        <Button
            android:id="@+id/drive"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/drive"
            android:contentDescription="@string/todo"
            android:text="@string/drive" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/gplus"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/gplus"
            android:contentDescription="@string/todo"
            android:text="@string/gplus" />

        <Button
            android:id="@+id/maps"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/maps"
            android:contentDescription="@string/todo"
            android:text="@string/maps" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/play"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/play"
            android:contentDescription="@string/todo"
            android:text="@string/play" />

        <Button
            android:id="@+id/youtube"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/youtube"
            android:contentDescription="@string/todo"
            android:text="@string/youtube" />
    </LinearLayout>

   <ImageView
       android:id="@+id/imageView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:contentDescription="@string/todo"
       android:src="@drawable/googleapps" />

   <TextView
       android:id="@+id/credits"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:layout_marginBottom="22dp"
       android:text="@string/credits"
       android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

Penjelasan:

Masing-masing gambar/icon/button yang disediakan dimasukkan ke dalam folder drawable untuk button menggunakan komponen widget button dengan properties drawableLeft

<Button
            android:id="@+id/search"
            android:layout_width="140sp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/google"
            android:contentDescription="@string/todo"
            android:text="@string/search" />

Untuk memudahkan pemasangan 2 button dalam 1 baris horisontal maka digunakan <LinearLayout … >

 

4. Sedangkan isi dari file strings.xml adalah sebagai berikut

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Google Service</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Beberapa Layanan Google</string>
    <string name="todo">TODO</string>
    <string name="search">Search</string>
    <string name="gmail">Email</string>
    <string name="translate">Terjemah</string>
    <string name="drive">Drive</string>
    <string name="gplus">Plus</string>
    <string name="maps">Maps</string>
    <string name="play">Android</string>
    <string name="youtube">Youtube</string>
    <string name="credits">Copyleft 2014 Kasmui</string>
</resources>

 

5. Isi file GoogleService.java dapat dilihat seperti berikut

package google.service;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GoogleService extends Activity {
    private Button search,gmail,translate,drive,gplus,maps,play,youtube;

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

        search = (Button) this.findViewById(R.id.search);
        search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.google.com/")));
            }
        });

        gmail = (Button) this.findViewById(R.id.gmail);
        gmail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://mail.google.com/")));
            }
        });

        translate = (Button) this.findViewById(R.id.translate);
        translate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://translate.google.com/")));
            }
        });

        drive = (Button) this.findViewById(R.id.drive);
        drive.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://drive.google.com/")));
            }
        });

        gplus = (Button) this.findViewById(R.id.gplus);
        gplus.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://plus.google.com/+KasmuiRasidjan/")));
            }
        });

        maps = (Button) this.findViewById(R.id.maps);
        maps.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://maps.google.com")));
            }
        });

        play = (Button) this.findViewById(R.id.play);
        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/")));
            }
        });

        youtube = (Button) this.findViewById(R.id.youtube);
        youtube.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.youtube.com/")));
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.google, menu);
        return true;
    }

}

 

Penjelasan:

Semua gambar/icon dideklarasikan sebagai komponen button seperti berikut

private Button search,gmail,translate,drive,gplus,maps,play,youtube;

Selanjutnya panggil alamat url yang akan dihubungkan dengan masing-masing button, sebagai contoh seperti di bawah ini button gambar search digunakan untuk mengakses link “https://www.google.com” menggunakan perintah Uri.parse(“”);

        search = (Button) this.findViewById(R.id.search);
        search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("https://www.google.com/")));
            }
        });

 

Lakukan hal yang sama untuk button dengan link-link lainnya.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *