This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
FEATURE_NO_TITLE works when creating a dialog from scratch, as in:
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
But it doesn't work when creating an AlertDialog (or using the Builder), because it already disables the title and use a custom one internally.
I have looked at the SDK sources, and I think that it can't be worked around. So to remove the top spacing, the only solution is to create a custom dialog from scratch IMO, by using the Dialog class directly.
Also, one can do that with a style, eg in styles.xml:
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
And then:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
You can hide the title of a dialog using:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Previous version of this answer, which is overcomplicated:
You need to use an AlertDialog
. There's a good explanation on the Android Developer's site about custom dialogs.
In very short summary, you do this with code like copied below from the official website. That takes a custom layot file, inflates it, gives it some basic text and icon, then creates it. You'd show it then with alertDialog.show()
.
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
In response to comment:
I assume that TextView with the id nr
is in the View you are inflating with View view = inflater....
. If so, then you need to change just one bit: instead of dialog.findView...
make it view.findView...
. Then once you've done that, remember to use dialog.show(), or even builder.show() without bothering to do builder.create().
In your code add this line
requestWindowFeature(Window.FEATURE_NO_TITLE);
Or in XML use a theme
android:theme="@android:style/Theme.NoTitleBar"
XML would be a better implementation as with the code version the title bar gets created and then removed which is a waste of resource
Ok good try but it is not working. I get: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application if I want to shwo the dialog.
Change the alert dialog type to system dialog ( e.g., TYPE_SYSTEM_OVERLAY ) and see if this resolves your issue
Use like this:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
This will remove any title bar from dialog window.
Use below code before setcontentview
:-
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
Note: You must have above code, in same order and line.
requestWindowFeature
must be before the setContentView line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With