Berikut
adalah cara bagaimana agar dalam 1 layout dapat mengganti background tanpa
harus menambah layout dan activity.
Langkah pertama adalah buat satu project android eclipse kemudian buka layout
XML yang telah ada. namun sebelum itu copy kan sebuah gambar pada Drawable
(ukuran gambar tidak terlalu besar).
- Contoh XML pada Layout
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:text="Choose!"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:text="Picture"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/button1"
android:text="Color"
/>
</RelativeLayout>
|
Untuk membedakan mengganti background, dengan menggunakan warna dan gambar. sebelum masuk ke activity , Buatlah file XML baru pada Values untuk menambahkan id warna yang di inginkan .
- Contoh XML pada Values
|
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<color name="grey"
>#888888</color>
<color name="white">#FFFFFF</color>
<color name="green">#ff408c3a</color>
<color name="blue">#333399</color>
<color name="red">#ffff3448</color>
<color name="Skyblue">#87CEEB</color>
<color name="Black">#000000</color>
</resources>
|
- Contoh XML pada Activity
|
package example.funbackground;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.style.BackgroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements
OnClickListener {
Button btn1;
Button btn2;
RelativeLayout r;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
r
=(RelativeLayout)findViewById(R.id.layout1);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main,
menu);
return true;
}
@Override
public void onClick(View
v) {
// TODO
Auto-generated method stub
if (v==btn1){
r.setBackgroundDrawable(getResources().getDrawable(R.drawable.sherlockholmes));
}
else if (v==btn2) {
r.setBackgroundColor(getResources().getColor(R.color.Skyblue));
}
}
}
|
- Screen Shoot
Tampilan Awal
Tampilan setelah di klik Button Color
- Selamat Mencoba !

