Modul Pembelajaran Android Studio
Modul Pembelajaran Android Studio
1
<!-- Button to display first fragment -->
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="#4CAF50"
android:fontFamily="@font/roboto"
android:onClick="selectFragment"
android:text="@string/fragment1_button"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
The android:name tag under the <fragment> element is containing the file name of default
fragment which is to be displayed when activity opens.
2
Step 4: Creating the two fragment class
These files contain only the onCreateView() method to inflate the UI of the fragment and
returns the root of the fragment layout. If the fragment does not have any UI, it will return
null.
1. First Fragment class:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
3
Step 5: Creating Layouts for both the fragments
Create two Layout Resource Files for both the fragments. Fragment displays a text on the
screen and have a background color to differentiate their area in the Activity layout. Below
is the code to implement this layout.
1. fragment_one.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5C52CC57"
android:orientation="vertical">
</LinearLayout>
2. fragment_two.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5C3473A6"
android:orientation="vertical">
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
FragmentManager fm = getFragmentManager();
6
Membuat RatingBar
Catatan: Untuk layout, ConstraintLayout cocok digunakan jika Anda seorang pemula
karena dapat menyesuaikan tampilan sesuai layar.
File XML ini mendefinisikan tampilan aplikasi.
Program untuk membuat tata letak MainActivity:
tools:context="com.example.hp.rating.MainActivity"
android:background="@color/colorPrimary">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
8
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate Me!!!"
android:textColor="@android:color/background_dark"
android:textSize="30sp"
android:textStyle="bold|italic"
tools:layout_editor_absoluteX="127dp"
tools:layout_editor_absoluteY="28dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="148dp"
android:textColorHint="@color/colorAccent"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:background="@android:color/holo_red_dark"
android:onClick="Call"
android:text="Submit"
android:textColor="@android:color/background_light"
android:textStyle="bold|italic"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
9
Di sini kita tidak perlu mengubah file manifes, tidak diperlukan izin untuk ratingBar . Secara
default, semua aktivitas baru yang dibuat disebutkan dalam file manifes.
Di bawah ini adalah kode untuk AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output :
Sumber : https://www.geeksforgeeks.org/android-creating-a-ratingbar/
10
Floating Action Button (FAB) di Android Studio
Tombol aksi mengambang adalah tombol yang sedikit berbeda dari tombol biasa. Tombol
tindakan mengambang diterapkan di UI aplikasi untuk tindakan utama (tindakan yang
dipromosikan) bagi pengguna dan tindakan di bawah tombol tindakan mengambang
diprioritaskan oleh pengembang. Misalnya tindakan seperti menambahkan item ke daftar
yang ada. Jadi dalam artikel ini telah ditunjukkan cara mengimplementasikan Floating Action
Button (FAB), dan juga tombol-tombol di bawah FAB ditangani dengan
pesan Toast sederhana . Perhatikan bahwa kami akan mengimplementasikan proyek ini
menggunakan bahasa Java.
Jenis Tombol Aksi Mengambang
Ada empat jenis tombol aksi mengambang yang tersedia di Android.
• Tombol Aksi Mengambang Normal/Reguler
• Tombol Aksi Mengambang Mini
• Tombol Aksi Mengambang yang Diperluas
• Tombol Aksi Mengambang Bertema
Pada artikel kali ini mari kita bahas Tombol Aksi Mengambang
Normal/Reguler dan Tombol Aksi Mengambang Mini dengan contoh contoh di Android.
FAB Reguler adalah FAB yang tidak diperluas dan berukuran biasa. Contoh berikut
menunjukkan FAB biasa dengan ikon plus.
11
Implementasi Langkah demi Langkah
Untuk membuat proyek baru di Android Studio silakan lihat Cara Membuat/Memulai
Proyek Baru di Android Studio . Kode untuk itu telah diberikan dalam Bahasa
Pemrograman Java dan Kotlin untuk Android.
Langkah 2: Menambahkan Ketergantungan ke File build.gradle
Buka file Module build.gradle dan tambahkan ketergantungan ini dan klik
tombol Sinkronkan Sekarang .
implementasi 'com.google.android.material:material:1.3.0-alpha02'
Lihat gambar di bawah jika Anda tidak bisa mendapatkan langkah-langkah yang disebutkan
di atas:
12
Sekarang pilih ikon vektor Anda
13
Langkah 4: Bekerja dengan File XML
Selanjutnya, buka file Activity_main.xml , yang mewakili UI proyek. Di bawah ini adalah kode
untuk file Activity_main.xml . Komentar ditambahkan di dalam kode untuk memahami kode secara
lebih detail.
Dalam file Activity_main.xml tambahkan tombol tindakan mengambang dan aktifkan kode
berikut. Sekarang aktifkan FAB normal. Yaitu radius 56dp. Kami telah merantai sub-FAB ke FAB induk
sehingga mereka berada dalam satu baris kunci. Komentar ditambahkan di dalam kode untuk memahami
kode secara lebih detail.
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity"
tools:ignore="HardcodedText">
<!-- After clicking the above button the following two buttons
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
14
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_add_black_24dp"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_alarm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/add_fab"
app:layout_constraintEnd_toEndOf="@+id/add_fab"
app:layout_constraintStart_toStartOf="@+id/add_fab"
app:srcCompat="@drawable/ic_add_alarm_black_24dp" />
<!-- Action name text for the add alarm button -->
15
the add Alarm FAB button -->
<TextView
android:id="@+id/add_alarm_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Add Alarm"
app:layout_constraintBottom_toBottomOf="@+id/add_alarm_fab"
app:layout_constraintEnd_toStartOf="@+id/add_alarm_fab"
app:layout_constraintTop_toTopOf="@+id/add_alarm_fab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_person_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/add_alarm_fab"
app:layout_constraintEnd_toEndOf="@+id/add_alarm_fab"
app:layout_constraintStart_toStartOf="@+id/add_alarm_fab"
app:srcCompat="@drawable/ic_person_add_black_24dp" />
16
<!-- Action name text for the add person button -->
<TextView
android:id="@+id/add_person_action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Add Person"
app:layout_constraintBottom_toBottomOf="@+id/add_person_fab"
app:layout_constraintEnd_toStartOf="@+id/add_person_fab"
app:layout_constraintTop_toTopOf="@+id/add_person_fab" />
</androidx.constraintlayout.widget.ConstraintLayout>
UI Output :
17
Langkah 5: Bekerja dengan File MainActivity
Buka File MainActivity dan lihat kode berikut. Di bawah ini adalah kode untuk File
MainActivity. Komentar ditambahkan di dalam kode untuk memahami kode secara lebih
detail.
Sekarang, kami menangani semua FAB menggunakan metode setOnClickListener() yang
dapat Anda rujuk ke peristiwa Menangani Klik di Button di Android. Dalam kode ini, telah
ditunjukkan bahwa kapan sub FAB terlihat dengan onClickListener. Komentar ditambahkan
di dalam kode untuk memahami kode secara lebih detail.
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
// These are taken to make visible and invisible along with FABs
TextView addAlarmActionText, addPersonActionText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register all the FABs with their IDs This FAB button is the Parent
mAddFab = findViewById(R.id.add_fab);
// FAB button
mAddAlarmFab = findViewById(R.id.add_alarm_fab);
mAddPersonFab = findViewById(R.id.add_person_fab);
// Now set all the FABs and all the action name texts as GONE
mAddAlarmFab.setVisibility(View.GONE);
mAddPersonFab.setVisibility(View.GONE);
addAlarmActionText.setVisibility(View.GONE);
addPersonActionText.setVisibility(View.GONE);
18
// make the boolean variable as false, as all the
// action name texts and all the sub FABs are invisible
isAllFabsVisible = false;
// below is the sample action to handle add alarm FAB. Here it shows simple Toast msg
// The Toast will be shown only when they are visible and only when user clicks on them
mAddAlarmFab.setOnClickListener(
view -> Toast.makeText(MainActivity.this, "Alarm Added", Toast.LENGTH_SHORT
).show());
}
}
19
Output :
20