Android dialog box takes too much space - android

Windows 7, Android Studio 1.1.0, compiled SDK 21, Samsung Galaxy Core (SDK 14).
I'm designing a form with few fields to fill as a dialog box, but I have plenty of blank space that I can't get rid of. Arranged it using TableLayout. I skipped some irrelevant fields in xml code, but all layout-related ones are there.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="#dimen/margin_small"
android:background="#color/HellTeal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView/>
<EditText/>
</TableRow>
<TableRow>
<TextView/>
<Spinner
android:layout_width="0dp"
android:layout_weight="1"/>
<Spinner
android:layout_width="0dp"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<TextView/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button
android:layout_width="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
Everything looked fine on preview (sorry for this color, it's set to show taken space).
Unfortunately it looks different in reality. This takes way more space than it should, when I use it as dialog layout. Codes creating dialogs where maximally simplified, so it's not some weird declarations. There are screens with this layout in AlertDialog and DialogFragment respectively. Notice, how additional space is colored in second case, weird.
I noticed that the amount of additional space somehow depends on contents. It's even posible for it to be equal to 0. See this comparison (I didn't change anything in either layout):
I noticed that my layout appears properly if I don't set anything to views (onClickListeners, adapter etc.). So I started making as simplistic class as possible and adding proper line then. I found out that thing forcing this space in my case were spinners. Both use exactly the same array for items. I noticed the longer selected item is, the more aditional space is taken. Seems like maybe some mechanism preventing crossing screen borders, because right spinner affects dialog more. Left has to be like 7-8 characters to change dialog's size
What I tried:
explicitly setting layout and rows to wrap_content
using linear layouts as TableLayout and TableRow
setting layout_height = "fill_parent" for TableLayout - I read on wrap_content messes things up, but not in this case
Adding various views and observing, how amount of blank space changes, I see no formula
simplifying code

Seems like you have everything that you need in your table layout even the ok and cancel buttons. I suggest you to go with AlertDialog. But don't set buttons and title in your builder. Set your view also after created the alert. Try following code
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//you won't need yes no buttons because it is already containing in your layout
View view = getLayoutInflater().inflate(R.layout.aaa_test, null);
AlertDialog aler=builder.create();
alert.setView(view,0,0,0,0);
alert.show();

Related

How to get spacing between two edit text boxes as the below attached image

The texts can be entered into them . Only these two text widgets are possible in the given activity. I would want to know the structure , I would have to employ to get result as such.
I am still in learning phase.
There's a lot of ways how to achieve that. I suggest you to start with reading this thoroughly to learn how to build layouts on Android.
In general, you can add spacing among views by adding some margin and/or padding.
If you want to replicate the particular design quickly, do this:
Have vertical LinearLayout as your root layout (with gray background).
Add two CardViews (one for each box). That will add the
background and spacing.
Add other views to those CardViews.
To give you something to work on
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> //Elements inside this will be added vertically on the screen
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:hint="First edittext"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:hint="Second edittext"/>
</LinearLayout>
This is the basic structure on the image you showed. Expirement with it. Add your desired borders by using shapes and etc.

Android - Equal column width layout for any size text

I'm quite new to android. I wanted to show cards in a tabular form. I want 2 columns which should be exactly equal in length (even if the size of the content in one of the column increases).
For example, it should be like Play Store or Google Keep. My card will have 2 lines of data. First for Title and second for Description.
I tried many layouts; TableLayout, GridLayout etc. but neither worked actually. Lastly I read this SO post but it doesn't work if text size on my card increases.
Any idea on how this can be done? It will be a bonus if the Description text ellipsizes at end.
If I understand you well enough:
Try using a LinearLayout and setting the layout_weight of the items inside the layout. So you'll have something like:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Card
android:id="#+id/card1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Card
android:id="#+id/card2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Then if you wanted to perhaps have multiple things in the columns just make those card items additional LinearLayouts of vertical orientation.

Android Layouts - Combining fills with wraps and gravities

I have an oddly organized activity with a number of LinLayouts inside of LinLayouts inside of LinLayouts (inside of LinLayouts) all so that each little section is organized correctly. Everything looks good except for one part.
I have a vertical LinearLayout with two things inside of it. One is another LinearLayout with weight set to 6, and below it is a TextView weight set to 1. What I want to do is have the height of the TextView to scale depending on the amount of lines in it. It could either be a one-liner sentence or maybe a four-line paragraph--I don't know what it'll end up. Then the LinLayout above it needs to fill in the rest of the height.
What layout_height settings do I need to combine to get this to work?
Finally figured it out. It's sort of weird, but this is what I had to do:
<LinearLayout
android:id="#+id/everything_but_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/paragraph" />
I had to have both set to "wrap_content" but give the one I wanted to fill the remaining space a weight WITHOUT giving the other one a weight. Very weird workaround but it works perfectly.

Border Lines for cells in GridLayout, TableLayout, or GridView?

I am trying to create a table/grid for some items within my app and I would like to have a border around each cell to divide the items up and have a coherent association of the setting with the item. This app will be used in an industrial setting where there may be people unfamiliar with Android that need to use this, thus trying to make it as easy as possible.
The table/grid will contain TextView, EditText, Spinner, and Button, and will also be scrollable (via ScrollView parent).
I read about the GridView and found that it (seems) to only be able to get items programmatically, please correct me if I am wrong. I felt that this was unnecessary since I know what items I want and where. Also, I have not tried adding items to a layout programmatically yet so I figured I would try the other options first. Also, the GridView documentation does not say one way or the other if border lines are automatically shown, or if you can have them shown at all.
I started with a TableLayout and was able to get everything except the border lines to work. I tried android:divider to get the lines but that didn't work. One thought I had was to create a bunch of TextViews with black backgrounds and ~2dp widths/heights to make my own border lines. This feels like a huge waste though. Then I also read the TableLayout documentation and found this: "TableLayout containers do not display border lines for their rows, columns, or cells."
I then tried the GridLayout and had the same results as the TableLayout. I tried padding and margins, neither worked. Also, the GridLayout documentation states: "The grid is composed of a set of infinitely thin lines that separate the viewing area into cells."
My questions are:
Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?
If no, then will the GridView give me the lines I want?
Will I be able to add all the perviously mentioned items I want to the GridView?
I was actually able to achieve the desired look by setting the android:background="#000000" within the GridLayout view and then in the child items I set the android:background="#8CDD81" (just some green color) and combined with android:layout_margin="2dp" I was able to get the "grid" lines that I wanted. Thanks to CommonsWare though for getting me thinking in a new direction that turned into a solution.
EDIT:
This does not work quite as anticipated. You need the android:layout_alignLeft/Right which are only available via RelativeLayout in order to get just the right width on the child items. Haven't tested this yet using this idea, child items within RelativeLayout within GridLayout.
Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?
No.
If no, then will the GridView give me the lines I want?
No.
Will I be able to add all the perviously mentioned items I want to the GridView?
Yes, though how well something like a Spinner will work, I can't say.
The simplest way, off the top of my head, to give you the lines you seek is to have each cell of the TableLayout or GridLayout be some container containing the widget(s) for that cell, where you give the container a background that is your line. A ShapeDrawable could be defined in XML for that background, which will be nicely resizeable based upon the actual requirements of the cell.
For future visiters this is how I did it with TableLayout:
table.xml
<TableLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#969696">
<!-- table heading -->
<TableRow>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:background="#d2d2d2"
android:layout_margin="1dp"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:background="#d2d2d2"
android:layout_margin="1dp"
/>
</TableRow>
<!-- table data -->
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ahtisham"
android:layout_margin="1dp"
android:background="#f1f1f1"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kashmir"
android:layout_margin="1dp"
android:background="#f1f1f1"
/>
</TableRow>
</TableLayout>

Android EditText won't take up remaining space

In my Android app, I have a tabbed Activity. In one of the tabs I have two TextViews and two EditTexts.
The first EditText is only one line, and that's fine. However, I want the other EditText, android:id="#+id/paste_code", to take up the remaining space, but no matter what I do to it, it will only show one line. I don't want to manually set the number of lines, since the number that would fit on the screen differs based on your device.
Here's the relevant code. It's nested inside all the necessary components for a tabbed Activity.
<ScrollView
android:id="#+id/basicTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Paste title"
android:layout_weight="0" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/paste_title_hint"
android:id="#+id/paste_title"
android:lines="1"
android:gravity="top|left"
android:layout_weight="0" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Paste text"
android:layout_weight="0" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/paste_hint"
android:id="#+id/paste_code"
android:gravity="top|left"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
Since the accepted answer doesn't address the situation fully, here's a proper fix for people coming to this while searching:
Firstly, Romain Guy from the Android dev team addresses this well in this blog post:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Essentially, your ScrollView needs to contain the android:fillViewport="true" attribute.
If things aren't working once you've done that, here are a couple of things to check:
The layout inside the ScrollView (such as a LinearLayout) needs to have layout_height="wrap_content"
The view(s) you want to expand should have layout_height="wrap_content"
The view(s) you want to expand should have layout_weight="1.0" or similar
Don't forget to set minLines="3" or similar in the view(s) you want to expand if you don't want it/them to shrink too much.
The problem seems to come from your use of ScrollView. I've tested your code using a ScrollView as the parent container, and got the same problem. However if I replaced the ScrollView with a LinearLayout, then the second EditText properly expanded to fill the whole screen. The problem must be that ScrollViews are designed to wrap to their smallest possible size, regardless of what settings you put in android:layout_height. I experimented with another few layouts, e.g. a RelativeLayout using layout_above and layout_below, but those only affected its maximum size, not its size when empty. Unfortunately, that means I'm not sure how to solve your problem... Is there a way you can redesign your layout to use something other than the ScrollView as the parent container?

Categories

Resources