Safira A. // girl who's interesting to everything with rice.

Month: February 2018

Lessons 5 :Delightful User Experience

NAMA           : SAFIRA AINI

NIM                : 1202154315

KELAS          : SI-39-03

Lessons 5 :Delightful User Experience

5.1: Drawable, Gaya, dan Tema

Tugas 1: Membuat Aplikasi Scorekeeper

1.1 Buat proyek “Scorekeeper”

 

1.2 Buat layout untuk aktivitas utama

 

Tambahkan aset vektor

 

1.3 Inisialisasi TextView Anda dan variabel hitungan skor

1.4 Implementasikan fungsionalitas onClick untuk tombol Anda.

 

package com.example.safiraaini.scorekeeper;
 
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 
 public class MainActivity extends AppCompatActivity {
 
     private int mScore1;
     private int mScore2;
 
     private TextView mScoreText1;
     private TextView mScoreText2;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         mScoreText1 = (TextView)findViewById(R.id.tvscore1);
         mScoreText2 = (TextView)findViewById(R.id.tvscore2);
 
     }
 
     public void decreaseScore(View view) {
         //Get the ID of the button that was clicked
         int viewID = view.getId();
         switch (viewID){
             //If it was on Team 1
             case R.id.ib1min:
                 //Decrement the score and update the TextView
                 mScore1--;
                 mScoreText1.setText(String.valueOf(mScore1));
                 break;
             //If it was Team 2
             case R.id.ib2min:
                 //Decrement the score and update the TextView
                 mScore2--;
                 mScoreText2.setText(String.valueOf(mScore2));
     }
     }
 
     public void increaseScore(View view) {
         //Get the ID of the button that was clicked
         int viewID = view.getId();
         switch (viewID) {
             //If it was on Team 1
             case R.id.ib1plus:
                 //Increment the score and update the TextView
                 mScore1++;
                 mScoreText1.setText(String.valueOf(mScore1));
                 break;
             //If it was Team 2
             case R.id.ib2plus:
                 //Increment the score and update the TextView
                 mScore2++;
                 mScoreText2.setText(String.valueOf(mScore2));
         }
     }
Tugas 2: Membuat sumber daya Drawable

2.1 Buat Drawable Bentuk

2.2 Terapkan drawable bentuk sebagai latar belakang

Hasil :

 

Tugas 3: Tata gaya tampilan Anda

3.1 Buat gaya tombol

 

3.2 Buat gaya TextView

3.3 Memperbarui gaya

Hasil :

Tugas 4: Tema dan Sentuhan Akhir

4.1 Jelajahi tema

Hasil :

 

4.2 Tambahkan tombol tema ke menu

5.2: Desain Material Daftar, Kartu, dan Warna

Tugas 1: Mengunduh Kode Starter

1.1   Buka dan Jalankan Proyek Material Me

Tugas 2: Menambahkan CardView dan Gambar

2.1 Tambahkan CardView

 

2.2 Unduh gambar

 

2.3 Modifikasi objek Sport

 

2.4 Perbaiki metode initializeData()

 

2.5 Tambahkan ImageView ke item daftar

 

2.6 Muat gambar menggunakan Glide

Tugas 3: Membuat CardView Anda bisa digesek, dipindahkan, dan diklik

3.1 Implementasikan gesek untuk menutup

3.2 Implementasikan seret dan lepas

3.3 Implementasikan tampilan detail

 

Tugas 4: Tambahkan FAB dan pilih Palet Warna Material

4.1 Tambahkan FAB

 

4.2 Pilih Palet Material

Hasil :

 

5.3 Mendukung Lanskap, Beberapa Ukuran Layar, dan Pelokalan

Tugas 1: Mendukung Orientasi Lanskap

1.1 Ubah ke GridLayoutManager

1.2 Modifikasi MainActivity

 

Hasil :

 

Tugas 2 : Dukung Tablet

2.1 Buat Layout Menyesuaikan Tablet

 

2.2 Perbarui gaya item daftar tablet

2.3 Perbarui gaya detail olahraga tablet

Hasil :

 

Tugas 3: Melokalkan Aplikasi Anda

3.1 Tambahkan file strings.xml

Hasil :

Lesson 4: User Interaction

NAMA : SAFIRA AINI
NIM : 1202154315
KELAS : SI-39-03

4.1. Menggunakan Keyboard, Kontrol Masukan, Peringatan, dan Picker

Tugas 1: Bereksperimen dengan atribut keyboard entri teks

1.1 Membuat layout utama dan metode showText

 

Hasil :

 

1.2 Setel keyboard untuk menjadikan huruf besar di awal kalimat

 

Hasil :

1.3 Setel keyboard untuk menyembunyikan sandi saat memasukkannya

Hasil :

Tugas 2. Mengubah tipe keyboard

2.1 Menggunakan keyboard email

Hasil :

2.2 Menggunakan keypad ponsel

Hasil :

Tugas 3: Menambahkan kontrol masukan spinner untuk memilih label telepon

3.1 Salin proyek KeyboardSamples dan ubah layout

3.2 Tambahkan kode untuk mengaktifkan spinner dan listener-nya

3.3 Tambahkan kode untuk merespons pilihan pengguna

Hasil :

Tugas 4: Menggunakan dialog untuk peringatan yang memerlukan keputusan

4.1 Buat proyek baru dengan layout untuk menampilkan dialog peringatan

Hasil :

Tugas 5: Menggunakan picker untuk masukan pengguna

5.1 Membuat layout aktivitas utama

5.2 Buat fragmen baru untuk picker tanggal

 

5.3 Buat fragmen baru untuk picker waktu

Date Picker :

 

package com.example.android.DateTimePickers;
 
 
 import android.app.DatePickerDialog;
 import android.app.Dialog;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.app.Fragment;
 import android.widget.DatePicker;
 
 import java.util.Calendar;
 
 public class DatePickerFragment extends DialogFragment
         implements DatePickerDialog.OnDateSetListener {
 
     /**
      * Creates the date picker dialog with the current date from Calendar.
      * @param savedInstanceState    Saved instance
      * @return DatePickerDialog     The date picker dialog
      */
     @NonNull
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Use the current date as the default date in the picker.
         final Calendar c = Calendar.getInstance();
         int year = c.get(Calendar.YEAR);
         int month = c.get(Calendar.MONTH);
         int day = c.get(Calendar.DAY_OF_MONTH);
 
         // Create a new instance of DatePickerDialog and return it.
         return new DatePickerDialog(getActivity(), this, year, month, day);
     }
 
         public void onDateSet(DatePicker view, int year, int month, int day) {
         // Convert the date elements to strings.
         // Set the activity to the Main Activity.
         MainActivity activity = (MainActivity) getActivity();
         // Invoke Main Activity's processDatePickerResult() method.
         activity.processDatePickerResult(year, month, day);
     }
 
 }





Main Activity

package com.example.android.DateTimePickers;
 
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Toast;
 
 /**
  * This app shows the time and date pickers when you click the appropriate button.
  */
 public class MainActivity extends AppCompatActivity {
 
     /**
      * Creates the view based on the layout for the main activity.
      * @param savedInstanceState    Saved instance
      */
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
     public void showDatePickerDialog(View v) {
         DialogFragment newFragment = new DatePickerFragment();
         newFragment.show(getSupportFragmentManager(), getString(R.string.date_picker));
     }
 
     public void showTimePickerDialog(View view) {
         DialogFragment newFragment = new TimePickerFragment();
         newFragment.show(getSupportFragmentManager(), getString(R.string.time_picker));
     }
 
     public void processDatePickerResult(int year, int month, int day) {
         // The month integer returned by the date picker starts counting at 0
         // for January, so you need to add 1 to show months starting at 1.
         String month_string = Integer.toString(month + 1);
         String day_string = Integer.toString(day);
         String year_string = Integer.toString(year);
         // Assign the concatenated strings to dateMessage.
         String dateMessage = (month_string + "/" + day_string + "/" + year_string);
         Toast.makeText(this, getString(R.string.date) + dateMessage, Toast.LENGTH_SHORT).show();
     }
 
     public void processTimePickerResult(int hourOfDay, int minute) {
         // Convert time elements into strings.
         String hour_string = Integer.toString(hourOfDay);
         String minute_string = Integer.toString(minute);
         // Assign the concatenated strings to timeMessage.
         String timeMessage = (hour_string + ":" + minute_string);
         Toast.makeText(this, getString(R.string.time) + timeMessage, Toast.LENGTH_SHORT).show();
     }
 
 } Time Picker :



package com.example.android.DateTimePickers;
 
 
 import android.app.Dialog;
 import android.app.TimePickerDialog;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.app.Fragment;
 import android.text.format.DateFormat;
 import android.widget.TimePicker;
 
 import java.util.Calendar;
 
 /**
  * A simple {@link Fragment} subclass for a time picker.
  * Sets the current time for the picker using Calendar.
  */
 public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
 
     /**
      * Creates the time picker dialog with the current time from Calendar.
      * @param savedInstanceState    Saved instance
      * @return TimePickerDialog     The time picker dialog
      */
     @NonNull
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Use the current time as the default values for the picker.
         final Calendar c = Calendar.getInstance();
         int hour = c.get(Calendar.HOUR_OF_DAY);
         int minute = c.get(Calendar.MINUTE);
 
         // Create a new instance of TimePickerDialog and return it.
         return new TimePickerDialog(getActivity(), this, hour, minute,
                 DateFormat.is24HourFormat(getActivity()));
     }
 
     /**
      * Grabs the time and converts it to a string to pass
      * to the Main Activity in order to show it with processTimePickerResult().
      * @param view          The time picker view
      * @param hourOfDay     The hour chosen
      * @param minute        The minute chosen
      */
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
         // Set the activity to the Main Activity.
         MainActivity activity = (MainActivity) getActivity();
         // Invoke Main Activity's processTimePickerResult() method.
         activity.processTimePickerResult(hourOfDay, minute);
     }
 }

Hasil :

 

Tugas 6: Menggunakan tampilan gambar sebagai tombol

6.1 Mulai proyek baru

 

6.2 Tambahkan gambar

 

6.3 Tambahkan metode onClick untuk tampilan gambar

Tugas 7: Menggunakan tombol radio

7.1 Tambahkan aktivitas lain

7.2 Tambahkan layout untuk tombol radio

7.3 Tambahkan handler klik tombol radio

 

Hasil :

 

4.2: Menggunakan Menu Opsi

Tugas 1: Menambahkan item ke menu opsi opsi

1.1 Periksa kode bilah aplikasi

Hasil :

Hasil:

H

Hasil :

4.3: Menggunakan Bilah Aplikasi dan Tab untuk Navigasi

Tugas 1. Menambahkan tombol Naik untuk navigasi ancestral

Hasil :

Tugas 2. Menambahkan navigasi tab dengan tampilan gesek

2.1 Buat layout untuk navigasi tab

 

2.2 Buat layout dan kelas untuk setiap fragmen

2.3 Add a PagerAdapter

2.4 Inflate the Toolbar and TabLayout

Hasil :

 

4.4 Membuat RecyclerView

Tugas 1. Membuat dan mengonfigurasi proyek baru

1.1Proyek

1.2 Buat layout untuk satu item daftar

 

Tugas 2. Membuat kumpulan data

Tugas 3: Membuat RecyclerView

3.1 Buat layout utama di activity_main.xml

3.2 Buat layout untuk satu item daftar

 

3.3 Buat gaya dari atribut TextView

3.4. Buat adapter dengan holder tampilan

 

Tugas 4. Membuat daftar agar interaktif

Tugas 5. Menambahkan FAB untuk menyisipkan item

5.1. Menambahkan Tombol Aksi Mengambang (FAB)

5.2 Tambahkan perilaku ke FAB

Hasil :

 

Lesson 3 : Testing, Debugging and using Support Library

Nama              : SAFIRA AINI

NIM                : 1202154315

Kelas               : SI-39-03

Lesson 3 : Testing, Debugging and using Support Library

3.1 : Menggunakan Debugger

Tugas 1. Membuat Proyek dan Aplikasi SimpleCalc

1.1   Unduh dan buka proyek SimpleCalc

 

1.2 Menjelajahi Layout

Hasil :

 

1.3 Jelajahi kode aplikasi

 

Tugas 2. Menjalankan SimpleCalc di Debugger

2.1 Memulai dan Menjalankan aplikasi dalam mode debug

 

2.2 Men-debug aplikasi yang berjalan

 

Tugas 3: Menjelajahi Fitur Debugger

3.1 Menyusuri eksekusi aplikasi

 

3.2 Bekerja dengan Breakpoint

3.3 Memeriksa dan memodifikasi variabel

 

3.2: Menguji Aplikasi dengan Pengujian Unit

Tugas 1. Menjelajahi dan menjalankan SimpleCalc di Android Studio

1.1 Menjelajahi rangkaian sumber dan SimpleCalc

package com.example.android.SimpleCalc;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.closeTo;

import static org.junit.Assert.assertThat;

/**
* JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed
*/
@RunWith(JUnit4.class)
@SmallTest
public class CalculatorTest {

private Calculator mCalculator;

/**
* Set up the environment for testing
*/
@Before
public void setUp() {
mCalculator = new Calculator();
}

/**
* Test for simple addition
*/
@Test
public void addTwoNumbers() {
double resultAdd = mCalculator.add(1d, 1d);
assertThat(resultAdd, is(equalTo(2d)));
}

}

1.2   Menjalankan pengujian dalam Android Studio

 

Tugas 2. Menambahkan lebih banyak pengujian unit ke CalculatorTest

2.1 Menambahkan lebih banyak pengujian untuk metode add()

 

2.2 Menambahkan pengujian unit untuk metode penghitungan lain


/*
  * Copyright 2016, Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.example.android.SimpleCalc;
 
         import org.junit.Before;
         import org.junit.Test;
         import org.junit.runner.RunWith;
         import org.junit.runners.JUnit4;
         import android.test.suitebuilder.annotation.SmallTest;
 
         import static org.hamcrest.CoreMatchers.equalTo;
         import static org.hamcrest.CoreMatchers.is;
         import static org.hamcrest.Matchers.closeTo;
 
         import static org.junit.Assert.assertThat;
 
 /**
  * JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed
  */
 @RunWith(JUnit4.class)
 @SmallTest
 public class CalculatorTest {
 
     private Calculator mCalculator;
 
     /**
      * Set up the environment for testing
      */
     @Before
     public void setUp() {
         mCalculator = new Calculator();
     }
 
     /**
      * Test for simple addition
      */
     @Test
     public void addTwoNumbers() {
         double resultAdd = mCalculator.add(1d, 1d);
         assertThat(resultAdd, is(equalTo(2d)));
     }
 
     @Test
     public void addTwoNumbersNegative() {
         double resultAdd = mCalculator.add(-1d, 2d);
         assertThat(resultAdd, is(equalTo(1d)));
     }
 
     @Test
     public void addTwoNumbersFloats() {
         double resultAdd = mCalculator.add(1.111f, 1.111d);
         assertThat(resultAdd, is(closeTo(2.222, 0.01)));
     }
 
     @Test
     public void subTwoNumbers() {
         double resultSub = mCalculator.sub(1d, 1d);
         assertThat(resultSub, is(equalTo(0d)));
     }
     @Test
     public void subWorksWithNegativeResult() {
         double resultSub = mCalculator.sub(1d, 17d);
         assertThat(resultSub, is(equalTo(-16d)));
     }
     @Test
     public void mulTwoNumbers() {
         double resultMul = mCalculator.mul(32d, 2d);
         assertThat(resultMul, is(equalTo(64d)));
     }
     @Test
     public void divTwoNumbers() {
         double resultDiv = mCalculator.div(32d,2d);
         assertThat(resultDiv, is(equalTo(16d)));
     }
     @Test
     public void divTwoNumbersZero() {
         double resultDiv = mCalculator.div(32d,0);
         assertThat(resultDiv, is(equalTo(Double.POSITIVE_INFINITY)));
     }
 }


3.3: Menggunakan Pustaka Dukungan Android

Tugas 1. Menyiapkan proyek

1.1   Memverifikasi bahwa Pustaka Dukungan Android tersedia

 

1.2 Menyiapkan Proyek dan memeriksa build.gradle

 

1.3   Menambahkan layout dan warna

 

1.4 Menambahkan perilaku ke MainActivity

 

Tugas 2. Mengimplementasikan perilaku tombol

2.1 Tambahkan handler onClick changeButton()

 

2.2 Implementasikan tindakan tombol

 

activity_main.xml

<?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"
     android:paddingTop="@dimen/ptop"
     android:paddingLeft="@dimen/pleft"
     android:paddingRight="@dimen/pright"
     android:paddingBottom="@dimen/pbottom"
     tools:context="com.example.safiraaini.hellocompat.MainActivity">
 
     <TextView
         android:id="@+id/hello_textview"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello_text_string"
         android:gravity="center"
         android:textSize="100sp"
         android:textStyle="bold"
 
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
     <Button
         android:id="@+id/color_button"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="34dp"
         android:onClick="changeColor"
 
         android:text="@string/button_label" />
 
 </RelativeLayout>

MainActivity.java

package com.example.safiraaini.hellocompat;
 
 import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.TextView;
 
 import java.util.Random;
 
 public class MainActivity extends AppCompatActivity {
 
     private TextView mHelloTextView;
 
     private String[] mColorArray = {"red", "pink", "purple", "deep_purple",
             "indigo", "blue", "light_blue", "cyan", "teal", "green",
             "light_green", "lime", "yellow", "amber", "orange", "deep_orange",
             "brown", "grey", "blue_grey", "black"
     };
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         mHelloTextView = (TextView) findViewById(R.id.hello_textview);
 
         if (savedInstanceState != null) {
             mHelloTextView.setTextColor(savedInstanceState.getInt("color"));
         }
     }
 
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
         // save the current text color
         outState.putInt("color", mHelloTextView.getCurrentTextColor());
     }
 
     public void changeColor(View view) {
 
         Random random = new Random();
         String colorName = mColorArray[random.nextInt(20)];
         int colorResourceName = getResources().getIdentifier(colorName, "color",
                 getApplicationContext().getPackageName());
 
         int colorRes = ContextCompat.getColor(this, colorResourceName);
         mHelloTextView.setTextColor(colorRes);
     }
 }

colors.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <color name="colorPrimary">#3F51B5</color>
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorAccent">#FF4081</color>
     <color name="red">#F44336</color>
     <color name="pink">#E91E63</color>
     <color name="purple">#9C27B0</color>
     <color name="deep_purple">#673AB7</color>
     <color name="indigo">#3F51B5</color>
     <color name="blue">#2196F3</color>
     <color name="light_blue">#03A9F4</color>
     <color name="cyan">#00BCD4</color>
     <color name="teal">#009688</color>
     <color name="green">#4CAF50</color>
     <color name="light_green">#8BC34A</color>
     <color name="lime">#CDDC39</color>
     <color name="yellow">#FFEB3B</color>
     <color name="amber">#FFC107</color>
     <color name="orange">#FF9800</color>
     <color name="deep_orange">#FF5722</color>
     <color name="brown">#795548</color>
     <color name="grey">#9E9E9E</color>
     <color name="blue_grey">#607D8B</color>
     <color name="black">#000000</color>
 </resources>

 

dimens.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<dimen name=”ptop”>16sp</dimen>
<dimen name=”pleft”>16sp</dimen>
<dimen name=”pright”>16sp</dimen>
<dimen name=”pbottom”>16sp</dimen>
</resources>

Hasil:

Lesson 2 Activities

2.1: Membuat dan Memulai Aktivitas

Tugas 1. Membuat proyek TwoActivities

1.1   Membuat proyek TwoActivities

Menggunakan cara yang sama dengan pembuatan proyek sebelumnya

 

1.2 Definisikan layout untuk aktivitas utama

 

1.3 Mendefinisikan tindakan tombol

 

 

package com.example.safiraaini.twoactivities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG =
MainActivity.class.getSimpleName();

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

public void launchSecondActivity(View view) {
Log.d(LOG_TAG, “Button clicked!”);
}
}

Hasil :

Tugas 2. Membuat dan meluncurkan aktivitas kedua

2.1 Membuat aktivitas kedua

2.2 Memodifikasi manifes Android

 

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.safiraaini.twoactivities">
 
     <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" />
 
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
         <activity android:name=".SecondActivity"
             android:label="@string/activity2_name"
             android:parentActivityName=".MainActivity">
             <meta-data
                 android:name="android.support.PARENT_ACTIVITY"
                 android:value="com.example.safiraaini.twoactivities.MainActivity" />
         </activity>
     </application>
 
 </manifest>

2.3 Mendefinisikan layout untuk aktivitas utama

 

2.4 Menambahkan intent ke aktivitas utama

Pada MainActivity.java:

package com.example.safiraaini.twoactivities;
 
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 
 public class MainActivity extends AppCompatActivity {
     private static final String LOG_TAG = MainActivity.class.getSimpleName();
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }
 
     public void launchSecondActivity(View view) {
         Log.d(LOG_TAG, "Button clicked!");
         Intent intent = new Intent(this, SecondActivity.class);
         startActivity(intent);
     }
 
 }

 

Pada second_activity.xml

<?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.safiraaini.twoactivities.SecondActivity”
>

<TextView
android:id=”@+id/text_header”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”@dimen/left_Margin”
android:layout_marginStart=”@dimen/start_Margin”
android:layout_marginTop=”@dimen/top_Margin”
android:text=”@string/text_header”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:textStyle=”bold”
tools:layout_editor_absoluteX=”16dp”
tools:layout_editor_absoluteY=”6dp”
/>
</RelativeLayout>

Hasil :

 

Tugas 3. Mengirim data dari aktivitas utama ke aktivitas kedua

3.1 Menambahkan EditText ke layout aktivitas utama

 

3.2 Menambahkan string ke ekstra intent aktivitas utama

 

3.3 Menambahkan TextView ke aktivitas kedua untuk pesan

3.4 Memodifikasi aktivitas kedua untuk mendapatkan ekstra dan menampilkan pesan

package com.example.safiraaini.twoactivities;
 
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.TextView;
 
 public class SecondActivity extends AppCompatActivity {
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_second);
         Intent intent = getIntent();
 
         String message =
                 intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 
         TextView textView = (TextView) findViewById(R.id.text_Message);
         textView.setText(message);
     }
 }

Tugas 4. Mengembalikan data ke aktivitas utama

4.1 Menambahkan EditText dan Tombol ke layout aktivitas kedua

 

4.2 Membuat intent respons dalam aktivitas kedua

 

4.3 Menambahkan TextViews ke layout aktivitas utama untuk menampilkan balasan

Activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
 <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"
 
     tools:context="com.example.safiraaini.twoactivities.MainActivity">
 
     <TextView
         android:id="@+id/text_header_reply"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_alignParentTop="true"
 
         android:text="@string/text_header_reply"
 
         android:textStyle="bold"
         android:visibility="invisible"
         />
 
     <TextView
         android:id="@+id/text_message_reply"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignEnd="@+id/text_header"
         android:layout_alignRight="@+id/text_header"
         android:layout_below="@+id/text_header_reply"
         android:layout_marginStart="20dp"
         android:layout_marginLeft="45dp"
 
         android:visibility="invisible"
         />
     <Button
         android:id="@+id/button_main"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_alignParentEnd="true"
         android:layout_alignParentRight="true"
 
         android:layout_marginBottom="13dp"
         android:layout_marginEnd="18dp"
         android:layout_marginRight="18dp"
         android:onClick="launchSecondActivity"
         android:text="@string/button_main" />
 
     <EditText
         android:id="@+id/editText_main"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_toLeftOf="@+id/button_main"
         android:layout_toStartOf="@+id/button_main"
         android:layout_marginBottom="13dp"
         android:layout_marginLeft="18dp"
         android:layout_alignParentBottom="true"
         android:ems="10"
         android:inputType="textPersonName"
         android:hint="@string/editText_main" />
 </RelativeLayout>

 

Activity_second.xml

<?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.safiraaini.twoactivities.SecondActivity”
>

<TextView
android:id=”@+id/text_header”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”@dimen/left_Margin”
android:layout_marginStart=”@dimen/start_Margin”
android:layout_marginTop=”@dimen/top_Margin”
android:text=”@string/text_header”

android:textStyle=”bold”
tools:layout_editor_absoluteX=”16dp”
tools:layout_editor_absoluteY=”6dp”
/>

<TextView
android:id=”@+id/text_Message”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignEnd=”@+id/text_header”
android:layout_alignRight=”@+id/text_header”
android:layout_below=”@+id/text_header”
android:layout_marginEnd=”45dp”
android:layout_marginRight=”45dp”

/>

<Button
android:id=”@+id/button_second”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentEnd=”true”
android:layout_alignParentRight=”true”

android:layout_marginBottom=”13dp”
android:layout_marginEnd=”18dp”
android:layout_marginRight=”18dp”
android:onClick=”returnReply”
android:text=”@string/button_second”
/>

<EditText
android:id=”@+id/editText_second”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_toLeftOf=”@+id/button_second”
android:layout_toStartOf=”@+id/button_second”
android:layout_marginBottom=”13dp”
android:layout_marginLeft=”18dp”
android:layout_alignParentBottom=”true”
android:ems=”10″
android:inputType=”textPersonName”
android:hint=”@string/editText_second”
/>

</RelativeLayout>

 

MainActivity.java

package com.example.safiraaini.twoactivities;
 
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 
 
 public class MainActivity extends AppCompatActivity {
     private static final String LOG_TAG = MainActivity.class.getSimpleName();
 
     public static final String EXTRA_MESSAGE =
             "com.example.safiraaini.twoactivities.extra.MESSAGE";
 
     public static final int TEXT_REQUEST = 1;
 
     private EditText mMessageEditText;
     private TextView mReplyHeadTextView;
     private TextView mReplyTextView;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         mMessageEditText = (EditText) findViewById(R.id.editText_main);
         mReplyHeadTextView = (TextView) findViewById(R.id.text_header_reply);
         mReplyTextView = (TextView) findViewById(R.id.text_message_reply);
 
         Log.d(LOG_TAG, "-------");
         Log.d(LOG_TAG, "onCreate");
 
         if (savedInstanceState != null) {
             boolean isVisible = savedInstanceState.getBoolean("reply_visible");
             // Show both the header and the message views. If isVisible is
             // false or missing from the bundle, use the default layout.
             if (isVisible) {
                 mReplyHeadTextView.setVisibility(View.VISIBLE);
 
                 mReplyTextView.setText(savedInstanceState.getString("reply_text"));
                 mReplyTextView.setVisibility(View.VISIBLE);
             }
         }
     }
     @Override
     public void onStart(){
         super.onStart();
         Log.d(LOG_TAG, "onStart");
     }
 
     @Override
     public void onRestart() {
         super.onRestart();
         Log.d(LOG_TAG, "onRestart");
     }
 
     @Override
     public void onResume() {
         super.onResume();
         Log.d(LOG_TAG, "onResume");
     }
 
     @Override
     public void onPause() {
         super.onPause();
         Log.d(LOG_TAG, "onPause");
     }
 
     @Override
     public void onStop() {
         super.onStop();
         Log.d(LOG_TAG, "onStop");
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(LOG_TAG, "onDestroy");
     }
 
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
         // If the heading is visible, we have a message that needs to be saved.
         // Otherwise we're still using default layout.
         if (mReplyHeadTextView.getVisibility() == View.VISIBLE) {
             outState.putBoolean("reply_visible", true);
             outState.putString("reply_text", mReplyTextView.getText().toString());
         }
     }
 
     public void launchSecondActivity(View view) {
         Log.d(LOG_TAG, "Button clicked!");
         Intent intent = new Intent(this, SecondActivity.class);
 
         String message = mMessageEditText.getText().toString();
         intent.putExtra(EXTRA_MESSAGE, message);
         startActivityForResult(intent, TEXT_REQUEST);
     }
 
     public void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
         if (requestCode == TEXT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 String reply =
                         data.getStringExtra(SecondActivity.EXTRA_REPLY);
 
                 mReplyHeadTextView.setVisibility(View.VISIBLE);
                 mReplyTextView.setText(reply);
                 mReplyTextView.setVisibility(View.VISIBLE);
             }
         }
     }
 
 }


SecondActivity.java

package com.example.safiraaini.twoactivities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

public static final String EXTRA_REPLY =
“com.example.safiraaini.twoactivities.extra.REPLY”;

private EditText mReply;

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

mReply = (EditText) findViewById(R.id.editText_second);
Intent intent = getIntent();

String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = (TextView) findViewById(R.id.text_Message);
textView.setText(message);
}

private static final String LOG_TAG =
SecondActivity.class.getSimpleName();

public void returnReply(View view) {
String reply = mReply.getText().toString();
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
Log.d(LOG_TAG, “End SecondActivity”);
finish();
}

@Override
protected void onStart() {
super.onStart();
Log.d(LOG_TAG, “onStart”);
}

@Override
public void onRestart() {
super.onRestart();
Log.d(LOG_TAG, “onRestart”);
}

@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, “onResume”);
}

@Override
public void onPause() {
super.onPause();
Log.d(LOG_TAG, “onPause”);
}

@Override
public void onStop() {
super.onStop();
Log.d(LOG_TAG, “onStop”);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, “onDestroy”);
}
}

Hasil :

 

2.2: Siklus Hidup Aktivitas dan Status Instance

Tugas 1. Menambahkan Callback Siklus Hidup ke TwoActivities

1.2 Mengimplementasikan callback ke MainActivity

package com.example.safiraaini.twoactivities;
 
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 
 
 public class MainActivity extends AppCompatActivity {
     private static final String LOG_TAG = MainActivity.class.getSimpleName();
 
     public static final String EXTRA_MESSAGE =
             "com.example.safiraaini.twoactivities.extra.MESSAGE";
 
     public static final int TEXT_REQUEST = 1;
 
     private EditText mMessageEditText;
     private TextView mReplyHeadTextView;
     private TextView mReplyTextView;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         mMessageEditText = (EditText) findViewById(R.id.editText_main);
         mReplyHeadTextView = (TextView) findViewById(R.id.text_header_reply);
         mReplyTextView = (TextView) findViewById(R.id.text_message_reply);
 
         Log.d(LOG_TAG, "-------");
         Log.d(LOG_TAG, "onCreate");
     }
     @Override
     public void onStart(){
         super.onStart();
         Log.d(LOG_TAG, "onStart");
     }
 
     @Override
     public void onRestart() {
         super.onRestart();
         Log.d(LOG_TAG, "onRestart");
     }
 
     @Override
     public void onResume() {
         super.onResume();
         Log.d(LOG_TAG, "onResume");
     }
 
     @Override
     public void onPause() {
         super.onPause();
         Log.d(LOG_TAG, "onPause");
     }
 
     @Override
     public void onStop() {
         super.onStop();
         Log.d(LOG_TAG, "onStop");
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(LOG_TAG, "onDestroy");
     }
 
     public void launchSecondActivity(View view) {
         Log.d(LOG_TAG, "Button clicked!");
         Intent intent = new Intent(this, SecondActivity.class);
 
         String message = mMessageEditText.getText().toString();
         intent.putExtra(EXTRA_MESSAGE, message);
         startActivityForResult(intent, TEXT_REQUEST);
     }
 
     public void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
         if (requestCode == TEXT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 String reply =
                         data.getStringExtra(SecondActivity.EXTRA_REPLY);
 
                 mReplyHeadTextView.setVisibility(View.VISIBLE);
                 mReplyTextView.setText(reply);
                 mReplyTextView.setVisibility(View.VISIBLE);
             }
         }
     }
 
 }


 

1.3 Mengimplementasikan callback siklus hidup dalam SecondActivity

package com.example.safiraaini.twoactivities;
 
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 
 public class SecondActivity extends AppCompatActivity {
 
     public static final String EXTRA_REPLY =
             "com.example.safiraaini.twoactivities.extra.REPLY";
 
     private EditText mReply;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_second);
 
         mReply = (EditText) findViewById(R.id.editText_second);
         Intent intent = getIntent();
 
         String message =
                 intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 
         TextView textView = (TextView) findViewById(R.id.text_Message);
         textView.setText(message);
     }
 
     private static final String LOG_TAG =
             SecondActivity.class.getSimpleName();
 
     public void returnReply(View view) {
         String reply = mReply.getText().toString();
         Intent replyIntent = new Intent();
         replyIntent.putExtra(EXTRA_REPLY, reply);
         setResult(RESULT_OK, replyIntent);
         Log.d(LOG_TAG, "End SecondActivity");
         finish();
     }
 
     @Override
     protected void onStart() {
         super.onStart();
         Log.d(LOG_TAG, "onStart");
     }
 
     @Override
     public void onRestart() {
         super.onRestart();
         Log.d(LOG_TAG, "onRestart");
     }
 
     @Override
     public void onResume() {
         super.onResume();
         Log.d(LOG_TAG, "onResume");
     }
 
     @Override
     public void onPause() {
         super.onPause();
         Log.d(LOG_TAG, "onPause");
     }
 
     @Override
     public void onStop() {
         super.onStop();
         Log.d(LOG_TAG, "onStop");
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(LOG_TAG, "onDestroy");
     }
 }

 

Tugas 2. Menyimpan dan memulihkan status instance aktivitas.

2.1 Simpan status instance aktivitas dengan onSaveInstanceState()

 

2.2. Memulihkan status instance aktivitas dalam onCreate()

 

2.3: Memulai Aktivitas dengan Intent Implisit

Tugas 1. Membuat proyek dan layout baru

1.1 Membuat proyek

 

Tugas 2. Mengimplementasikan “buka situs web”

2.1 Mendefinisikan metode openWebsite

 

Tugas 3. Mengimplementasikan “buka lokasi”

3.1 Definisikan metode openLocation

 

Tugas 4. Mengimplementasikan bagikan teks ini

4.1 Mengimplementasikan metode shareText

 

Tugas 5. Menerima Intent Implisit

5.1 Membuat Proyek & Layout

 

5.2 Memodifikasi Manifes Android untuk menambahkan filter intent

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 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"
     android:orientation="vertical"
     tools:context="com.example.safiraaini.implicitintents.MainActivity">
 
     <EditText
         android:id="@+id/website_edittext"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ems="10"
         android:inputType="textPersonName"
         android:text="@string/edittext_uri"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="16dp" />
 
     <Button
         android:id="@+id/button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/open_website_button"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="62dp"
         android:layout_marginBottom="24dp"
         android:onClick="openWebsite"/>
 
     <EditText
         android:id="@+id/location_edittext"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ems="10"
         android:inputType="textPersonName"
         android:text="@string/edittext_loc"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="123dp" />
 
     <Button
         android:id="@+id/open_location_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_loc"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="169dp"
         android:onClick="openLocation"/>
 
     <EditText
         android:id="@+id/share_edittext"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ems="10"
         android:inputType="textPersonName"
         android:text="@string/edittext_share"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="232dp" />
 
     <Button
         android:id="@+id/share_text_button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_share"
         tools:layout_editor_absoluteX="16dp"
         tools:layout_editor_absoluteY="278dp"
         android:onClick="shareText"/>
 
 </LinearLayout>

MainActivity.java

package com.example.safiraaini.implicitintents;
 
 import android.content.Intent;
 import android.net.Uri;
 import android.support.v4.app.ShareCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.EditText;
 
 public class MainActivity extends AppCompatActivity {
     private EditText mWebsiteEditText;
     private EditText mLocationEditText;
     private EditText mShareTextEditText;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         mWebsiteEditText = (EditText) findViewById(R.id.website_edittext);
         mLocationEditText = (EditText) findViewById(R.id.location_edittext);
         mShareTextEditText = (EditText) findViewById(R.id.share_edittext);
 
     }
 
     public void openWebsite(View view) {
         String url = mWebsiteEditText.getText().toString();
 
         // Parse the URI and create the intent.
         Uri webpage = Uri.parse(url);
         Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
 
         // Find an activity to hand the intent and start that activity.
         if (intent.resolveActivity(getPackageManager()) != null) {
             startActivity(intent);
         } else {
             Log.d("ImplicitIntents", "Can't handle this intent!");
         }
     }
 
     public void openLocation(View view) {
         String loc = mLocationEditText.getText().toString();
 
         // Parse the location and create the intent.
         Uri addressUri = Uri.parse("geo:0,0?q=" + loc);
         Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
 
         // Find an activity to handle the intent, and start that activity.
         if (intent.resolveActivity(getPackageManager()) != null) {
             startActivity(intent);
         } else {
             Log.d("ImplicitIntents", "Can't handle this intent!");
         }
     }
 
     public void shareText(View view) {
         String txt = mShareTextEditText.getText().toString();
         String mimeType = "text/plain";
 
         ShareCompat.IntentBuilder
                 .from(this)
                 .setType(mimeType)
                 .setChooserTitle("Share this text with: ")
                 .setText(txt)
                 .startChooser();
     }
 }

 

Hasil :

 

 

© 2024 Xaveera.

Theme by Anders NorenUp ↑