I have a dialog with a custom theme:
final Dialog d = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
This makes my dialog background transparent.
Now the problem is if I let the normal theme(style.dialog) it is centered in my screen but now it has no layout and is in the upper left corner of my screen. Is there a way to programmatically center the dialog?
I didn't actually find a programatic solution to my problem, instead in my customdialog.xml I put the main layout into a RelativeLayout with fill_parent for height/width and then put the layout atribute centerInParent="true".
construct your dialog by extends Dialog,
override the style & content(by setContentView)
Related
I'm trying to create a Custom Dialog, setting my layout in order to customize it, but keeping the standard width.
In THIS picture we see a basic Dialog(AlertDialog), which has a defined width. On phones it has the full width minus some pixel, on tablet it has a defined width taht can be seen HERE.
The problem by using a custom layout is that the width shrinks to a wrap content of the layout like in THIS picture.
How can i do in order to have a custom dialog, which uses my layout but behavies on size as a standard dialog?
It appears that after you set the content view for the dialog, you have to call:
dialog.getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
And the dialog will have the same width as an AlertDialog.
It seems like you used LinearLayout for dialog. Try to set your root layout as RelativeLayout. It will solve your problem.
Check this example in details.
I created custom dialog and i have textview in it. Text is pretty close to edge of my dialog box, so i wonder if I can set some space between my text and left and right edge of dialog box and how ??
In your custom dialog's xml layout add the following to the root:
android:padding="10dp"
Or whichever value you see fit.
You need to create your TextView with some marginLeft and marginRight attributes.
These will put some space between your TextView and the left, right edges of the screen.
Also consider using paddingLeft, paddingRight attributes to ensure that there is some space between the border of your TextView and the actual text itself.
Look here for more information about how you can set these parameters in a TextView.
How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"
i'm having a hard time squeezing my alert dialog to just wrap my content.
the dialog's layout xml is structured like this:
+ linear layout (main)
++ text view
++ linear layout (placeholder)
i use the placeholder to attach a singe edittext-derived field to it later, during oncreate, by calling addView() on it, so my onCreate basically looks like this:
View v = LayoutInflater.from(context).inflate(input_layout, null);
LinearLayout placeholder = (LinearLayout)view.findViewById(input_placeholder)
SoftEditText text = new SoftEditText(context)
// set text attributes - input type, ime action, watchers and listeners
placeholder.addView(text);
setCancelable(true);
setButton(BUTTON_NEGATIVE, ....);
setTitle(title_label);
// view.forceLayout() - didn't help much
setView(view);
super.onCreate(instance);
// getWindow().getAttributes().height=LayoutParams.WRAP_CONTENT no go either
and the result is: my alert dialog is twice the height it's supposed to be. my linearlayout's are set to WRAP_CONTENT height, and if i paint their background, i see they are of correct size (that is nearly half the dialog's height). the rest of the dialog is black, so there is no component that forces or expects this size.
I set the text's maxLines to 1, but that again hasn't helped at all. this seems to be a trivial layout problem but i can't figure out what more should i call to get the dialog squeeze to simply wrap the content.
thanks bunches for any tip.
set maxLength and you can use ellipsize to indicate the text in continuation..
I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().
I think this is the same problem as the one mentioned here, but no one gave a proper answer.
Thanks for your help.
For more details, here is my code:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
And here is the code of the first element of the XML layout of newrecord.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
Here is the output screenshot:
(source: free.fr)
The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.
The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.
If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.
You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:
android:theme="#android:style/Theme.Dialog"
And you can adjust the activity xml layout as per your need.
android:theme="#android:style/Theme.Dialog"
If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.
myProgressDialog.show();
float widthPecent = 0.60f;
//order matters
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayWidth = displayMetrics.widthPixels;
//dont do any adjustments to the height. ************************** <<<<<<<<<<
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
int dialogWindowWidth = (int) (displayWidth * widthPecent);
layoutParams.width = dialogWindowWidth;
//dont do any changes to the height. ************************** <<<<<<<<<<
myProgressDialog.getWindow().setAttributes(layoutParams);
layoutParams.height = dialogWindowHeight; //comment or remove this line.
set the attribute on your AlertDialog as android:gravity="center" or programmatically as setGravity(Gravity.CENTER). This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.
Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.
Can you try using AlertDialog.Builder.setCustomTitle(View); instead of setView? We use it because alert dialog looks a bit better than dialog with empty title.
you should use the following line in the xml
firstly remove that padding line from your xml and after that
android:layout_gravity="center"
after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that
android:layout_width="fill_parent"
than change it with the
android:layout_width="wrap_content"
after that your dialog will be appear in the center.
Try:
newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
You need to disable the window title.
First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Then apply your style to the dialog builder:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);
newRecDialog = builder.create();