Android Custom Dialog
By Fanxtea | 1 Januari 2015
- Susun Atur Android (Android Layout) Dua fail XML, satu skrin utama dan satu custom dialog
- Kod Fail : MainActivity.java
- Demo Run aplikasi.
Fail : res/layout/activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paparkan Custom Dialog"/>
</LinearLayout>
Fail : res/layout/newpage.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FFFF"
android:layout_toRightOf="@+id/image"/>/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/image"
/>
</RelativeLayout>
packagecom.fanxtea.helloworld;
importandroid.app.Activity;
importandroid.app.Dialog;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.TextView;
publicclass MainActivity extends Activity {
finalContext context =this;
privateButton button;
publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.buttonShowCustomDialog);
// add button listener
button.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View arg0){
// custom dialog
finalDialog dialog =newDialog(context);
dialog.setContentView(R.layout.newpage);
dialog.setTitle("Tajuk...");
// set the custom dialog components - text, image and button
TextView text =(TextView)dialog.findViewById(R.id.text);
text.setText("Contoh Android custom dialog!");
ImageView image =(ImageView)dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton =(Button)dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View v){
dialog.dismiss();
}
});
dialog.show();
}
});
}
}