Layout alert dialog with dynamic content - result too tall - android

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..

Related

mText1.getLayoutParams().height = NEW VALUE does not take effect in popup window

In Android application,
I create a LinearLayout which contains a TableLayout which contains some rows and one of the rows contains a TextView called mText1
I set up a pop up window with this LinearLayout with mText1 setting as following :
mText1.getLayoutParams().height = OLD VALUE
Then when in the pop up window in one other row of the table a click on an ImageView happens by the user, I'd like to change the height of mText1 as following :
mText1.getLayoutParams().height = NEW VALUE
The problem is this will not take effect but when I type the following code, it takes effect:
mText1.setGravity(Gravity.CENTER)
Or if I type the following code instead of the previous one, it takes effect too:
mText1.setText("some value")
Why?
You need to call requestLayout() after changing the LayoutParams.
requestLayout()
Call this when something has changed which has invalidated the layout
of this view. This will schedule a layout pass of the view tree.
setGravity and setText are handling this for you.

Android Dialog with standard width like AlertDialog

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.

Android textview options in custom dialog

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 to keep EditText from breaking to the next line when width does not match_parent?

I am trying to fit two EditTexts horizontally. What I am trying to accomplish is make the text keep scrolling right without the EditText extending in anyway when there is no more space in that specific EditText. This happens when the EditText is set to match_parent (width), but when I set an actually width or wrap_content, it breaks to the next line. Not the behavior I am hoping for. Any ideas?
If I understood your question correctly, you're saying you want the edit text to be a single line each? If so, have you tried using the xml attribute
android:singleLine
Or you can accomplish the same thing with:
setSingleLine() //called on the edit text
I just tested it out with 2 edit texts in a horizontal linear layout set to 100dp width each and I think it accomplishes the behavior you want.
You can try this:
Set both to fill_parent and layout_weight=1. This will align them side by side.

pop-up type view (z-index?) - android

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);
...
}

Categories

Resources