How to display a custom dialog in your Android application
How to display a custom dialog in your Android application
Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...
Here is my main layout, main.xml. It's just a textview, with a button:

- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout android:id="@+id/RelativeLayout01&qu
ot; - android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.c
om/apk/res/android"> - <TextView android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text.">
- </TextView>
- <Button android:layout_height="wrap_content"
- android:layout_below="@+id/TextView01"
- android:layout_width="wrap_content"
- android:id="@+id/Button01main"
- android:text="Hey! There is more..."></Button>
- </RelativeLayout>
Here is my dialog's layout, maindialog.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:layout_width="wrap_content" android:layout_height="wrap_content">
- <ImageView android:id="@+id/ImageView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_centerHorizontal="true" />
- <ScrollView android:id="@+id/ScrollView01"
- android:layout_width="wrap_content" android:layout_below="@+id/ImageView01"
- android:layout_height="200px">
- <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content" />
- </ScrollView>
- <Button android:id="@+id/Button01" android:layout_below="@id/ScrollView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_centerHorizontal="true" android:text="Cancel" />
- </RelativeLayout>
Now that the xml part is all set up, it's time to code.
- public class main extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //set up main content view
- setContentView(R.layout.main);
- //this button will show the dialog
- button1main.setOnClickListener(new OnClickListener() {
- @Override
- //set up dialog
- dialog.setContentView(R.layout.maindialog);
- dialog.setTitle("This is my custom dialog box");
- dialog.setCancelable(true);
- //there are a lot of settings, for dialog, check them all out!
- //set up text
- TextView text = (TextView) dialog.findViewById(R.id.TextView01);
- text.setText(R.string.lots_of_text);
- //set up image view
- ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
- img.setImageResource(R.drawable.nista_logo);
- //set up button
- button.setOnClickListener(new OnClickListener() {
- @Override
- finish();
- }
- });
- //now that the dialog is set up, it's time to show it
- dialog.show();
- }
- });
- }
- }

You can read more about the topic, here.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- Android bases applications development (only indian & very skilled developers apply) by sudhirjeet01
- Health Care App - want freelancer in Georgia by rshearer
- IOS+Android+HTML5+PHP Expert Needed Urgently by johnusa1
- NFC Keyboard Wedge by billinginfoes
- I need an iphone and android app + web services by senjy
- Mobile Remote control software for android only by leoagent786
- Add a "Print" button, with code, to our existing process (PDF explains) by sportingchance
- I need a phone app - Android and Iphone - a copy of an existing app by DooniseP
- Apple & Android app - Connecting our CRM and an external GPS device by kshyamnatraj
- Flash&Actionscript app for android by mobistar2013



