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.
Related
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.
I've created a custom dialog builder that contains 2 buttons.
Depending on the dialog's setup, I may choose to hide one of the buttons completely, using Window.GONE.
Ideally what I want to happen is:
1. If there is only one button, then fill the layout with it
2. If there are two buttons, then split up the space in the layout equally with these
Is it possible to do this without having to work out the width of the dialog, the number of buttons and then set the sizes manually?
I was hoping there may be a neater way to perform this
ok, here is how I would do it:
<LinearLayout layout_width:fill_parent layout_height:wrap_content>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
<Button
layout_width=fill_parent
layout_height=wrap_content
layout_weight=1/>
</LinearLayout>
The trick is to put both elements a width of fill_parent and a weight of 1. If they are both drawn, they will each take up half the screen. If you use View.Gone, one of them will disappear and the other should take up all the space.
yes of cource put your views means button in linear layout and give yor buttons equal layout_weight will solve your problem.
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)
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 want to add a new view to my scene that will contain content that will change programatically throughout the course of my application. When it does change, it needs to pop up on the screen for 3 seconds (or a click) then disappear.
This view will change in size according to its content WRAP_CONTENT, but ideally I'd like it centered horizontally and vertically on the screen.
I'm stuck on three parts:
1) what type of view should I use for this...I was thinking Relative, but all of my playing with it has yielded no good results for what I'm trying to do
2) with respect to #1 (trying relative view), I could not get it centered properly (tried using param.leftMargin and param.topMargin with varying values but could not get it to work on different devices with different resolutions
3) also with respect to #1, I couldn't make this float over everything else on my screen (need something like a z-index or the like).
any ideas, code examples would be wonderful.
TIA
Use a custom dialog, i.e. a LinearLayout with android:theme="#android:style/Theme.Dialog" and for the class, it would be something like
public class YourCustomDialog extends Dialog implements DialogInterface
where you can implement you custom logic of what to display. Such dialog is floating and modal on top of all other views then and you can also optionally set the background to blurry, etc.
This is a typical constructor of my custom dialog - the layout would be defined in an xml layout file, which in my case is my_custom_dialog.xml:
public MyCustomDialog(Context context) {
super(context, android.R.style.Theme);
Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.empty));
setContentView(R.layout.my_custom_dialog);
// actually not necessary as it's already the default value:
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
...
}