What value should I change?
I"ve an .xml file that contains all the text representations for the display table rows.
I've an adapter, which is fine to display all in the main_activity.xml.
Paddings: paddingTop,paddingLeft, paddingRight, paddingBottom
or
Margins: layout_marginTop,layout_marginLeft, layout_marginRight, layout_marginBottom
depending on your exact needs.
You can use attribute layout_marginLeft, layout_marginRight, layout_marginTop and layout_marginBottom to specify indents.
Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="This is a sample text" />
</LinearLayout>
OUTPUT:
Assuming this is a recyclerView or ListView and you are using a List Item layout for each instance.
In your list item layout:
Set a textView for each item in each row.
Then for each textview use either a fixed layout_width or use Layout_width="0" in conjunction with Layout_weight to space your textviews.
(fixed Layou_width is easier, but using Layout_Weight will auto adjust for screen orientation)
then your spacing will be consistent from row to row because it is based on the layout definitions and not the inserted text.
Related
I have a list of items in a horizontal recycler view, and a fairly simple item that should only have a TextView set to wrap_content and an ImageView that should be aligned to the right of the layout.
Theoretically I should be able to just set a layout_weight of 1 on the TextView, but that causes a random space to randomly show up sometimes in the view depending on where it is in the recycler view.
Is there anyway to align the close icon to the right, while still keeping the layout/textview width as wrap_content? The amount of text can vary quite a bit, so I can't actually set a fixed width. I'm fairly certain that I can't just set a layout_gravity of end on the ImageView since the parent layout's set to wrap_content.
See layout attached.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners"
android:minWidth="#dimen/min_width"
android:maxWidth="#dimen/max_width"
android:orientation="horizontal">
<WPTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|end"/>
</LinearLayout>
With out knowing what the items are supposed to look like and how it needs to behave it is hard to judge what the problem you are having is.
If the image is just supposed to be a small image to the right of the text you could just add a right drawable to the text view:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableRight
If you can replace the LinearLayout by RelativeLayout then you are good to with
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corners"
android:minWidth="#dimen/min_width"
android:maxWidth="#dimen/max_width">
<WPTextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv" />
</RelativeLayout>
Spacing between TextView and ImageView can be adjusted with margin on them. The properties used in the above code are self-explanatory.
I have two views, a day view and a event view as part of a calendar. I want my events to fill the day view in width (which is working) but when I add another at the same position/time I want them to split the width... I am adding my event dynamically using java. Is there an attribute I can use so that views don't overlap?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/day_view">
</RelativeLayout>
.
<?xml version="1.0" encoding="utf-8"?>
<ie.test.calendar.CalendarEventView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="#+id/eventView"
android:background="#drawable/event_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/eventTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:textColor="#android:color/white"/>
</ie.test.calendar.CalendarEventView>
I think you need to use Linier layout as parent view with horizontal orientation. And also use concept of weight for width. It will devide your parent view according to child quantity
Take a look on RelativeLayout.LayoutParams
You could set rules at runtime, using ID's.
layoutparams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.id1)
Although, i think, that using LinearLayout would be much simpler.
What is the best layout to use tp produce a data entry form like this one in android:
Should I use a vertical linear layout,, with a horizental layout for each of the items? or a relative layout. I can't use a table layout because each text entry might have it's own width.
Thanks
As main layout, you can use a linear layout vertical, and for each row, a linear layout horizontal, setting width to fill_parent.
For the 4 first rows (data form container), when setting width, use "0 dip" for width and set a proportion to layout_weight as you need, keep the same value for the 3 next row in order to keep the alignement. Dont forget to set gravity right fo first column
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="50dip"
android:paddingRight="50dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="label"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:hint="value"
android:layout_marginLeft="15dip"/>
</LinearLayout>
</LinearLayout>
Do so, for last row with 0.5 proportion for each column.
Think, that helped you...
relative layout will be useful and use padding attribute to obtain the UI as image
As per my view you can pick linear layout it will be easy to handle
one Linear layout with vertical orientation
and 5 linear layouts with horizontal orientation
I have a problem with my list item layouts that i cannot understand. I set the height of a list item to 70dp but if i only have content with a total height of 40dp it will automatcally just ignore the value i set with android:layout_height and instead use what seems to be wrap_content as the height.
If i use the exakt same layout outside of a list it works as intended.
below is the layout of a list item from one of my lists.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="70dip"
android:layout_width="fill_parent">
<TextView android:id="#+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/NormalText"/>
<TextView android:id="#+id/description"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/VerySmallLightText"/></LinearLayout>
Editted my xml to reflect my code better. i have some of there attributes in style, that is why i accidently made a typo when tying it in manually here.
Have you tried layout_height of the linear layout instead of just the height?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="70dp"*
android:layout_width="fill_parent">
your linearlayout attribute the following
android:height = '70dip' is it can passing the IDE checking?
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/LinearLayout01"
android:layout_height="wrap_content">
<Spinner android:text="#+id/AutoCompleteTextView01"
android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="130dp"></Spinner>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/Chapter" android:width="30dp"></EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView01" android:text=":"></TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="#+id/Verse"
android:width="40dp"></EditText>
</LinearLayout>
I inflate this layout as an AlertDialog's view. But when I pick a large element, the text fields get pushed out to the right. Is there any way to set the maximum size of the spinner so after choosing an element, it shortens the choice with an ellipsis ("...") or something?
No need to use a custom adapter. Use the android.R.layout.simple_spinner_item and then change it from ellipse marquee to end ellipse. Then set your new change layout file as layout id or use this layout file
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:ellipsize="end" />
The size of a Spinner set have android:layout_width="wrap_content" is determined by its content. If you want things ellipsized, that's up to you in your SpinnerAdapter, when you create your row Views.
Or, hard-wire an android:layout_width that is fixed in size.
Or, instead of using LinearLayout, use RelativeLayout and structure your rules such that the Spinner takes up the remaining space on the row, by anchoring it to the left side of the screen and to the left side of the first EditText. Though I'm not 100% certain what then happens if the content is too big -- it probably just gets truncated.
Set spinner android:background="#null" to solve the problem.
This will also hide the dropdown icon visibility