You are on page 1of 6

Tugas Pertemuan Ke – 4

Bab 4 : Fragment
Nama Surya Jalu Kinayun
Nim 1918055
Kelas A
Pemberi Tugas (Nama Pengajar - NIM Pengajar)

1. Nama projek yang ditugaskan (tugas4)


A. Langkah – langkah pengerjaan
1. Buat Project dengan nama Tugas4

Gambar 4.1 Membuat Project


2. Buka file build.gradle. Tambahkan kode dibawah ini pada bagian dependencies file.
implementation 'com.google.android.material:material:1.0.0'

3. Jika sudah tambahkan gambar pada folder drawable

1
#praktikumobile2021
Gambar 4.2 Menambahkan gambar

4. Buat list_item.xml lalu masukan source code


<?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="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="16dp"
android:paddingBottom="8dp">

<ImageView
android:id="@+id/imageList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/facebook" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical">

<TextView
android:id="@+id/text_headline"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook" />

2
#praktikumobile2021
<TextView
android:id="@+id/text_subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Social Media" />
</LinearLayout>
</LinearLayout>
5. Buat Myitem java class lalu masukan source code
package com.example.tugas4;

class Myitem {

static int[] iconList = {


R.drawable.facebook, R.drawable.twitter,
R.drawable.instagram, R.drawable.snapchat,
R.drawable.whatsapp, R.drawable.line,
R.drawable.kakaotalk, R.drawable.telegram, R.drawable.messenger,
R.drawable.youtube, R.drawable.tiktok, R.drawable.vine,
R.drawable.vimeo,
R.drawable.figma, R.drawable.adobe_xd,
R.drawable.sketch, R.drawable.framer,
R.drawable.android, R.drawable.apple,
R.drawable.windows,
R.drawable.opera, R.drawable.firefox, R.drawable.safari,
R.drawable.edge,
R.drawable.paypal, R.drawable.mastercard,
R.drawable.visa,
R.drawable.bitcoin, R.drawable.ethereum
};

static String[] Headline = {"Facebook", "Twitter", "Instagram",


"SnapChat",
"WhatsApp", "LINE", "KakaoTalk", "Telegram",
"Messenger",
"YouTube", "TikTok", "Vine", "Vimeo",
"Figma", "Adobe XD", "Sketch", "Framer",
"Android", "iOS", "Windows",
"Opera", "Mozilla", "Safari", "Edge",
"Paypal", "Mastercard", "Visa",
"Bitcoin", "Ethereum"
};

static String[] Subhead = {"Social Media", "Social Media",


"Social Media", "Social Media",
"Chatting App", "Chatting App", "Chatting App",
"Chatting App", "Chatting App",
"Video Streaming App", "Video Streaming App", "Video
Streaming App", "Video Streaming App",
"UI Design App", "UI Design App", "UI Design App", "UI
Design App",
"Operating System", "Operating System", "Operating
System",
"Browser App", "Browser App", "Browser App", "Browser
App",
"Payment Method", "Payment Method", "Payment Method",
"Cryptocurrency", "Cryptocurrency"
};
}

3
#praktikumobile2021
6. Buat Itemmodel java class lalu masukan source code
package com.example.tugas4;

public class Itemmodel {

private String name;


private String type;
private int image;

public Itemmodel(String name, String type, int image) {


this.name = name;
this.type = type;
this.image=image;
}

public String getName() {


return name;
}

public String getType() {


return type;
}

public int getImage() {


return image;
}

7. Buka Mainactivity java class lalu masukan source code


package com.example.tugas4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager recylerViewLayoutManager;
ArrayList<Itemmodel> data;

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

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

4
#praktikumobile2021
recylerViewLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recylerViewLayoutManager);
recyclerView.addItemDecoration(new
DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

data = new ArrayList<>();


for (int i = 0; i < Myitem.Headline.length; i++) {
data.add(new Itemmodel(
Myitem.Headline[i],
Myitem.Subhead[i],
Myitem.iconList[i]
));
}

recyclerViewAdapter = new AdapterRecyclerView(data);


recyclerView.setAdapter(recyclerViewAdapter);
}
}

8. Buka Activity_main.xml lalu masukan source code


<?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=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
tools:listitem="@layout/list_item"/>

</RelativeLayout>

Source Diatas ditujukan untuk ………


9. Running app untuk melihat hasilnya, bisa pada Hp atau Emulator

5
#praktikumobile2021
B. Link repository github
https://github.com/SuryaJ77/tugas4-1918055-surya

6
#praktikumobile2021

You might also like