[Bosicc] Custom Alert Dialog based on Activity

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[Bosicc] Custom Alert Dialog based on Activity

Postby Bosicc » Wed Sep 09, 2009 4:41 pm

This example from API demos, from SDK 1.5

CustomDialogActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.android.apis.app;
  3.  
  4. // Need the following import to get access to the app resources, since this
  5.  
  6. // class is in a sub-package.
  7.  
  8. import com.example.android.apis.R;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13.  
  14. import android.os.Bundle;
  15.  
  16.  
  17.  
  18. public class CustomDialogActivity extends Activity {
  19.  
  20.  
  21.  
  22.     @Override
  23.  
  24.         protected void onCreate(Bundle savedInstanceState) {
  25.  
  26.         // Be sure to call the super class.
  27.  
  28.         super.onCreate(savedInstanceState);
  29.  
  30.        
  31.  
  32.         // See assets/res/any/layout/dialog_activity.xml for this
  33.  
  34.         // view layout definition, which is being set here as
  35.  
  36.         // the content of our screen.
  37.  
  38.         setContentView(R.layout.custom_dialog_activity);
  39.  
  40.     }
  41.  
  42. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

res/layout/custom_dialog_activity.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.         android:id="@+id/text"
  9.  
  10.    android:layout_width="fill_parent"
  11.  
  12.    android:layout_height="fill_parent"
  13.  
  14.    android:gravity="center_vertical|center_horizontal"
  15.  
  16.    android:text="Default text. You can print there your custom text, or add more components to layout "/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


res/value/styles.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <!-- Base application theme is the default theme. -->
  6.  
  7.     <style name="Theme" parent="android:Theme">
  8.  
  9.     </style>
  10.  
  11.  
  12.  
  13.     <!-- A theme for a custom dialog appearance.  Here we use an ugly
  14.  
  15.         custom frame. -->
  16.  
  17.     <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
  18.  
  19.         <item name="android:windowBackground">@drawable/filled_box</item>
  20.  
  21.     </style>
  22.  
  23. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

res/drawable/filled_box.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  4.  
  5.     <solid android:color="#f0600000"/>
  6.  
  7.     <stroke android:width="3dp" color="#ffff8080"/>
  8.  
  9.     <corners android:radius="3dp" />
  10.  
  11.     <padding android:left="10dp" android:top="10dp"
  12.  
  13.        android:right="10dp" android:bottom="10dp" />
  14.  
  15. </shape>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.        
  2.  
  3. <activity android:name=".app.CustomDialogActivity"
  4.  
  5.                android:label="@string/activity_custom_dialog"
  6.  
  7.                android:theme="@style/Theme.CustomDialog">
  8.  
  9.             <intent-filter>
  10.  
  11.                 <action android:name="android.intent.action.MAIN" />
  12.  
  13.                 <category android:name="android.intent.category.SAMPLE_CODE" />
  14.  
  15.             </intent-filter>
  16.  
  17.         </activity>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


P.S. I'm sorry for my style, but it's my first tutorial. I will modify it latter.

I mised one string @string/activity_custom_dialog, you can enter there you title, or add string to the values.
I find tutorial where use this customdialog here

P.P.S. May by I'm not lucky but this code from my tutorial doesn't work in my emulator :( I have black background.
Attachments
CustomDialog.gif
CustomDialog based on style
(32.65 KiB) Downloaded 1526 times
User avatar
Bosicc
Freshman
Freshman
 
Posts: 7
Joined: Wed Sep 09, 2009 4:08 pm

Top

Postby Boni2k » Tue Feb 16, 2010 7:22 am

To get rid of the black background use "Theme.Dialog". However the alertbox will have a small title-box in the background that you can not hide with "requestWindowFeature(Window.FEATURE_NO_TITLE);".
Boni2k
Experienced Developer
Experienced Developer
 
Posts: 88
Joined: Sun Dec 20, 2009 9:46 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests