Cara Membuat Grup pada Android
Pada tutorial Programming Android kali ini kita akan membuat desain tampilan menu dashboard. Tampilan menu dashboard pada Android merupakan elemen penting dalam aplikasi Android yang menyediakan navigasi yang mudah digunakan oleh pengguna aplikasi. Tutorial akan lebih menfokuskan pada tata letak dashboard dan memberikan navigasi untuk beralih pada layar yang terhubung ketika user memilih icon yang sesuai pada dashboard. Untuk lebih jelasnya kita akan langsung praktek dengan membuat aplikasinya.Buat project android seperti berikut:Application Name : AndroidDashboardProject Name : AndroidDashboardPackage Name : firman.salatigadev.dashboardMinimum Required SDK : Andoid 2.2Target SDK : Android 4.2Yang lain biarkan default.*Untuk kebutuhan desain gambar menu maupun background bisa Anda download pada link di akhir artikel.Buka file styles.xml
di direktori res/values
dan ubah code menjadi seperti berikut:
res/values/styles.xml
<resources>
<style name="ActionBarCompat">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">50dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">@drawable/actionbar_background</item>
</style>
<style name="DashboardButton">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:textSize">16dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ff29549f</item>
<item name="android:background">@null</item>
</style>
<style name="FooterBar">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">#dedede</item>
</style>
</resources>
Desain header action bar
Buat file baru actionbar_layout.xml
di direktori res/layout
isi code berikut:
res/layout/actionbar_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/ActionBarCompat" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:clickable="false"
android:paddingLeft="15dip"
android:scaleType="center"
android:src="@drawable/logo" />
</LinearLayout>
Buat class baru DashboardLayout.java
di src/package
isi dengan code berikut:
src/package/DashboardLayout.java
package firman.salatigadev.dashboard;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class DashboardLayout extends ViewGroup {
private static final int UNEVEN_GRID_PENALTY_MULTIPLIER = 10;
private int mMaxChildWidth = 0;
private int mMaxChildHeight = 0;
public DashboardLayout(Context context) {
super(context, null);
}
public DashboardLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public DashboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mMaxChildWidth = 0;
mMaxChildHeight = 0;
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());
mMaxChildHeight = Math.max(mMaxChildHeight, child.getMeasuredHeight());
}
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
mMaxChildWidth, MeasureSpec.EXACTLY);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
mMaxChildHeight, MeasureSpec.EXACTLY);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(
resolveSize(mMaxChildWidth, widthMeasureSpec),
resolveSize(mMaxChildHeight, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = r - l;
int height = b - t;
final int count = getChildCount();
int visibleCount = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
++visibleCount;
}
if (visibleCount == 0) {
return;
}
int bestSpaceDifference = Integer.MAX_VALUE;
int spaceDifference;
int hSpace = 0;
int vSpace = 0;
int cols = 1;
int rows;
while (true) {
rows = (visibleCount - 1) / cols + 1;
hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
spaceDifference = Math.abs(vSpace - hSpace);
if (rows * cols != visibleCount) {
spaceDifference *= UNEVEN_GRID_PENALTY_MULTIPLIER;
}
if (spaceDifference < bestSpaceDifference) {
bestSpaceDifference = spaceDifference;
if (rows == 1) {
break;
}
} else {
--cols;
rows = (visibleCount - 1) / cols + 1;
hSpace = ((width - mMaxChildWidth * cols) / (cols + 1));
vSpace = ((height - mMaxChildHeight * rows) / (rows + 1));
break;
}
++cols;
}
hSpace = Math.max(0, hSpace);
vSpace = Math.max(0, vSpace);
width = (width - hSpace * (cols + 1)) / cols;
height = (height - vSpace * (rows + 1)) / rows;
int left, top;
int col, row;
int visibleIndex = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
row = visibleIndex / cols;
col = visibleIndex % cols;
left = hSpace * (col + 1) + width * col;
top = vSpace * (row + 1) + height * row;
child.layout(left, top,
(hSpace == 0 && col == cols - 1) ? r : (left + width),
(vSpace == 0 && row == rows - 1) ? b : (top + height));
++visibleIndex;
}
}
}
Desain menu utama dashboard
Sekarang kita membutuhkan file layout yang akan menampilkan menu dashboard. Buat file layout fragment_layout.xml
di direktori res/layout
dan isi code seperti dibawah ini:
res/layout/fragment_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<firman.salatigadev.dashboard.DashboardLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#f8f9fe" >
<Button
android:id="@+id/btn_berita"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_berita"
android:text="Berita" />
<Button
android:id="@+id/btn_teman"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_teman"
android:text="Teman" />
<Button
android:id="@+id/btn_pesan"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_pesan"
android:text="Pesan" />
<Button
android:id="@+id/btn_tempat"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_tempat"
android:text="Tempat" />
<Button
android:id="@+id/btn_acara"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_acara"
android:text="Acara" />
<Button
android:id="@+id/btn_foto"
style="@style/DashboardButton"
android:drawableTop="@drawable/btn_foto"
android:text="Foto" />
</firman.salatigadev.dashboard.DashboardLayout>
Desain footer
Buat file baru footer_layout.xml
di res/layout
isi code berikut:
res/layout/footer_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/FooterBar" >
<TextView android:text="firman.salatigadev.com"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#606060"
android:gravity="center"
android:paddingTop="10dip"/>
</LinearLayout>
Menggabungkan semua layout
Setelah kita selesai mendesain masing – masing header action bar untuk bagian header, dashboard sebagai menu utama, dan footer. Sekarang kita akan menggabungkan semua file layout tersebut dalam satu file layout xml.
Buat file xml baru dashboard_layout.xml
di direktori res/layout
isi code seperti berikut:
res/layout/dashboard_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/actionbar_layout"/>
<include layout="@layout/fragment_layout"/>
<include layout="@layout/footer_layout"/>
</LinearLayout>
Output dari code-code diatas sebenarnya sudah bisa menampilkan menu dashboard, akan tetapi belum bisa masuk atau beralih dari screen menu yang kita pilih, maka kita akan menambahkan beberapa fungsi activity untuk setiap menu dashboard. Karena terdapat 6 menu icon, maka kita akan membutuhkan 6 activity untuk setiap icon menu. Anda bisa membaca kembali artikel
Berpindah Screen Dengan Activity Pada Aplikasi Android untuk lebih menjelaskan bagaimana cara aplikasi beralih atau berpindah antar activity.
Buka activity utama
DashboardActivity.java
pada direktori
src/package
tambahkan code menjadi seperti berikut:
src/package/DashboardActivity.java
package firman.salatigadev.dashboard;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import firman.salatigadev.dashboard.R;
public class DashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_layout);
Button btn_berita = (Button) findViewById(R.id.btn_berita);
Button btn_teman = (Button) findViewById(R.id.btn_teman);
Button btn_pesan = (Button) findViewById(R.id.btn_pesan);
Button btn_tempat = (Button) findViewById(R.id.btn_tempat);
Button btn_acara = (Button) findViewById(R.id.btn_acara);
Button btn_foto = (Button) findViewById(R.id.btn_foto);
btn_berita.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), BeritaActivity.class);
startActivity(i);
}
});
btn_teman.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), TemanActivity.class);
startActivity(i);
}
});
btn_pesan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), PesanActivity.class);
startActivity(i);
}
});
btn_tempat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), TempatActivity.class);
startActivity(i);
}
});
btn_acara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), AcaraActivity.class);
startActivity(i);
}
});
btn_foto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), FotoActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
}
Buat class baru BeritaActivity.java
di src/package
isi code berikut:
src/package/BeritaActivity.java
package firman.salatigadev.dashboard;
import android.app.Activity;
import android.os.Bundle;
import firman.salatigadev.dashboard.R;
public class BeritaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.berita_layout);
}
}
Buat juga class TemanActivity.java
, PesanActivity.java
, TempatActivity.java
, AcaraActivity.java
, dan FotoActivity.java
isi code kurang lebih seperti pada class BeritaActivity.java
diatas.
Jangan lupa buat file layout untuk masing – masing class activity diatas.
berita_layout.xml
, teman_layout.xml
, pesan_layout.xml
, tempat_layout.xml
, acara_layout.xml
, dan foto_layout.xml
.
isi code layout xml seperti pada contoh berita_layout.xml
berikut:
res/layout/berita_layout.xml
<?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="#f8f9fe"
android:orientation="vertical" >
<include layout="@layout/actionbar_layout" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="50dip"
android:text="Halaman Berita"
android:textColor="#ff29549f"
android:textSize="25dip"
android:textStyle="bold"
android:drawableBottom="@drawable/news_feed_default" />
</LinearLayout>
</LinearLayout>
Terakhir tambahkan semua activity yang dibuat pada file AndroidManifest.xml
.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="firman.salatigadev.dashboard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="firman.salatigadev.dashboard.DashboardActivity"
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="firman.salatigadev.dashboard.BeritaActivity" ></activity>
<activity android:name="firman.salatigadev.dashboard.TemanActivity" ></activity>
<activity android:name="firman.salatigadev.dashboard.PesanActivity" ></activity>
<activity android:name="firman.salatigadev.dashboard.TempatActivity" ></activity>
<activity android:name="firman.salatigadev.dashboard.AcaraActivity" ></activity>
<activity android:name="firman.salatigadev.dashboard.FotoActivity" ></activity>
</application>
</manifest>
Running aplikasi Android Dashboard yang baru saja kita buat.
0 komentar:
Posting Komentar