I have the following xml to display rows in a listView. I want the TextView with the id rowCreatedAt, to wrap around nicely but it never does and only shows 1 line. I tried most of the solutions in the other questions and added them in the code, but it still doesn't work. Can anyone tell me why...?
+I would like to avoid setting the exact size to the textview.
++ To put it differently, why does the rowCreatedAt textView doesn't wrap the text when width/height is set to fill_parent while other textViews wrap texts just fine?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
android:layout_marginBottom="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/rowUserImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/twitter_icon"
android:layout_margin="5dp"/>
<TextView
android:id="#+id/rowCreatedAt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10dp"
android:textColor="#ff696969"
android:ellipsize="none"
android:singleLine="false"
android:scrollHorizontally="false"
android:layout_weight="1"
android:maxLines="100"
android:text="aaaaaaaaaaaaaaabbbbbbbbbbbbbb" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/rowUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/rowText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="status text" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/rowCreatedAt"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10dp"
android:textColor="#ff696969"
android:ellipsize="none"
android:singleLine="false"
android:scrollHorizontally="false"
android:layout_weight="1"
android:maxLines="100"
android:text="aaaaaaaaaaaaaaabbbbbbbbbbbbbb" />
Change android:layout_width="fill_parent" to android:layout_width="200dp"
Try like this :
<TextView
android:id="#+id/Lesson_Description"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:minLines="4"
android:maxLines="6"
android:gravity="right|center"
android:background="#drawable/filed_shape_corner"
android:inputType="textMultiLine" />
Then do something like in code :
mLessonDescription = (TextView) rootView
.findViewById(R.id.Lesson_Description);
mLessonDescription.setMovementMethod(new ScrollingMovementMethod());
Try out be setting android:ems="10" it will make your edittext that much wide and wraps the other characters.
OR
You should use android:ellipsize="marquee". If you do not want to fix the height.
Add the following style changes to fix the width problem. along with the initial changes
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:singleLine">false</item>
<item name="android:maxLines">100</item>
<item name="android:width">0dp</item>
<item name="android:layout_weight">1</item>
Related
This is my layout. My question is that the id is "below_button" LinearLayout is invalid that running on the phone. The phone didn’t display this LinearLayout, but others layout is right. How come?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/title"
layout="#layout/toptitlebar" />
<LinearLayout
android:id="#+id/query_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/title"
android:background="#e2dfdf"
android:layout_alignParentLeft="true">
<EditText
android:id="#+id/ram_assistinqnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="0.45"
android:hint="通知书编号"
android:maxLines="1"
/>
<TextView
android:id="#+id/ram_operate_date"
style="#style/SpinnerCtl_Wrap"
android:gravity="center"
android:layout_margin="10dp"
android:layout_weight="0.45"
android:hint="操作时间"
android:textColor="#color/black"/>
<ImageView
android:id="#+id/ram_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="#drawable/ram_query"
android:layout_margin="10dp"/>
</LinearLayout>
<com.sdrcu.operatingdecisions.view.scrollabletbv.ScrollTableView4
android:id="#+id/scrollTableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/query_detail"
/>
<LinearLayout
android:id="#+id/below_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#e2dfdf"
android:layout_below="#+id/scrollTableView"
>
<ImageView
android:id="#+id/ram_arrow_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_weight="0.3"
android:src="#drawable/ram_arrow_previous"/>
<TextView
android:id="#+id/paging"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="center"
android:text="翻页"
android:textColor="#color/text_blue"
android:textSize="20sp"
/>
<ImageView
android:id="#+id/ram_arrow_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_weight="0.3"
android:src="#drawable/ram_arrow_next"/>
<ImageView
android:id="#+id/ram_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:src="#drawable/ram_agree"/>
<ImageView
android:id="#+id/ram_disagree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:src="#drawable/ram_disagree"/>
</LinearLayout>
</RelativeLayout>
#Seal
In my layout editor's Preview I changed the "Theme in Editor" to "Theme"
And I started to get errors like your screenshot.
See:
So, I selected "All" -> "AppTheme" from the "Theme in Editor" option... Do not forget to press "Ok"
And the errors went away:
So, why do not you also try to set it to "All" -> "AppTheme"
Further it appears to me that you might have created style(s) which do not inherit from "proper parent".
Using below example: "AppTheme" inherits all the required attributes from "Theme.AppCompat.Light.DarkActionBar". And thus, you need to only specify the attributes you need modified.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
...
<item name="colorAccent">#color/colorAccent</item>
...
</style>
replace
android:layout_below="#id/query_detail"
in
<com.sdrcu.operatingdecisions.view.scrollabletbv.ScrollTableView4
android:id="#+id/scrollTableView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/query_detail"
/>
with
android:layout_below="#+id/query_detail"
yes you miss the parameter height and weight
android:layout_width="match_parent"
android:layout_height="wrap_content"
add in your layout it works Use the Tag
I have the following layout for my custom log in dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/padding_large">
<ImageButton android:src="#drawable/ic_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dialog.login.settings"
android:padding="#dimen/margin_medium" />
<TextView android:text="#string/dialog.login.text.user_name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/dialog.login.user_name"
android:layout_width="match_parent"
android:gravity="center"
android:ems="20"
android:layout_height="wrap_content"
android:inputType="text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:text="#string/dialog.login.text.password"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText android:id="#+id/dialog.login.password"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox android:id="#+id/dialog.login.show_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dialog.login.check.show_password"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_margin="#dimen/margin_medium"
android:checked="false" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="#+id/dialog.login.cancel"
android:drawableStart="#drawable/ic_exit_black"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_medium"
android:text="#string/dialog.login.button.cancel" />
<Button android:id="#+id/dialog.login.connect"
android:text="#string/dialog.login.button.connect"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginStart="#dimen/margin_medium"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
when I create the dialog and setcontentview the width of the dialog becomes equal to that of the image button on the first row
if I remove that , it becomes the size of the EditText (dialog.login.user_name) whose ems is 20
when I make the dialog's constructor call the base dialog constructor with a theme of R.style.Theme.Material.Dialog it does get the correct (about 3/4 of the screen) but the problem is that it ignores my primary and accent colours (which is normal)
Is there a way to have the dialog have the Theme.Material.Dialog size but still keep my own primary/accent colours?
thanks in advance for any help you can provide
After a lot of searching it seems that the solution is to actually replace the dialogTheme attribute of my main theme, so the solution was:
<style name="app.theme"
parent="android:Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:dialogTheme">#style/app.dialog</item>
......
</style>
<style name="app.dialog"
parent="android:Theme.Material.Light.Dialog">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary.dark</item>
<item name="android:colorAccent">#color/accent</item>
</style>
I wanted to make resizable views when one of them is hidden. The question is already asked for iOS in the following link. I wanna make it for android. Any helps are appreciated. I wanted to add free space to three TextViev: position, stat_name, price after setting visibility hidden to count and discount
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="30dp"
android:weightSum="100" >
<TextView
style="#style/list_titles_style"
android:layout_weight="10"
android:text="#string/position" />
<TextView
style="#style/list_titles_style"
android:layout_weight="35"
android:paddingLeft="15dp"
android:text="#string/stat_name" />
<TextView
style="#style/list_titles_style"
android:layout_weight="20"
android:text="#string/price" />
<TextView
android:id="#+id/count_label"
style="#style/list_titles_style"
android:layout_weight="10"
android:visibility="invisible"
android:text="#string/count" />
<TextView
android:id="#+id/discount_label"
style="#style/list_titles_style"
android:visibility="invisible"
android:layout_weight="25"
android:text="#string/discount" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
android:background="#drawable/border_layout"
android:orientation="horizontal" >
<ListView
android:id="#+id/stats_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginBottom="5dp"
android:divider="#drawable/list_divider"
android:dividerHeight="2dp"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingTop="0dp" />
</LinearLayout>
And my style here it is:
<style name="list_titles_style">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#color/Blue</item>
<item name="android:gravity">center</item>
<item name="android:textSize">18sp</item>
</style>
I have gone through the provided link...To remove the left side view just set the android:visibility="gone" for the view in your xml layout file....if you want remove programmatically try view.setVisibility(View.GONE);....
You can achieve this using LinearLayout with match_parent parameter depending on what orientation you desire. So you might set weights for each child view and also put an stub view in order to stretch out the size.
Note that if you hide your views with INVISIBLE flag they will hold the space whereas with GONE it's like the view never been there.
Hope that helps.
If you're interested in RadioButtons in addition to (or instead of) CheckBoxes, see this question instead.
Despite the presence of
<item name="android:layout_gravity">center_horizontal</item>
in the style file, the two checkboxes are not centered, but appear "left-justified".
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/cb1"
style="#style/CB_style" />
<CheckBox
android:id="#+id/cb2"
style="#style/CB_style" />
</LinearLayout>
res/values/styles.xml
<style name="CB_style" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:checked">false</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
You have android:layout_weight = 1. So your checkboxes fill all width of screen.
Remove android:layout_weight from style and add margin between checkboxes.
Gravity in Checkbox don't affect the inner tick button, only text.
EDIT
Ok, try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
<CheckBox
android:id="#+id/cb1"
style="#style/CB_style" />
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
<CheckBox
android:id="#+id/cb2"
style="#style/CB_style" />
<TextView
android:layout_width="0dp"
android:layout_height="5dp"
android:layout_weight="1"/>
</LinearLayout>
And remove layout_weight from style.
Another way is create custom checkbox. But I think it's too complicated for this problem.
Here's something that may help:
android:gravity vs android:layout_gravity
android:gravity is usually internal, usually asking the view itself what to do with the pixels it has
android:layout_gravity is usually external, usually asking the parent ViewGroup (LinearLayout/RelativeLayout/FrameLayout) how to position the view with extra pixels that the view is not using
If you are using a view with wrap_content, you probably want to use layout_gravity. If you are using a view with match_parent, you probably want to try gravity.
Sometimes wrapping another ViewGroup around a troublesome View can help with positioning. Views and ViewGroups go through an intricate "screen space" negotiating phase, and different Views (Buttons/TextView/ImageView/etc) and ViewGroups (LinearLayout/RelativeLayout/TableLayout/etc) have different rules and negotiating powers
This is why sometimes pairing a troublesome View with another parent like a FrameLayout or LinearLayout can make it behave all of the sudden
This is a little messy but it works, and yes it works as well for 3 or more checkbox inputs:
<LinearLayout
android:padding="0dip"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="name"
android:layout_height="40dip"
android:textAlignment="center" android:layout_gravity="center"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
<CheckBox
android:id="#+id/chk1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleX="1.5" android:scaleY="1.5"
>
</CheckBox>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
<CheckBox
android:id="#+id/chk2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleX="1.5" android:scaleY="1.5"
>
</CheckBox>
<TextView
android:layout_width="0dip"
android:layout_height="1dip" android:layout_weight="1" android:text=" " />
</LinearLayout>
Try it in style
<item name="android:layout_width">0dp</item>
I hope it will work.. thanks..
<style name="TextlessCheckBox" parent="android:Widget.Material.CompoundButton.CheckBox">
<item name="android:button">#null</item>
<item name="android:foreground">?android:listChoiceIndicatorMultiple</item>
<item name="android:foregroundGravity">center</item>
</style>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#ff0000"
android:gravity="center" >
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#00FF00"
android:gravity="center" >
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ok, last try for today ;) you should post a picture next time, HOW it should look... well, now you ll have your LinearLayout divided into two similar wide part (red & green), and in every block your checkbox is in the center...did i got it right this time?!
Just add this to your LinearLayout :
android:gravity="center_horizontal"
This line specifies that anything inside this layout will be in the center.
The Full layout code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox ONE"
/>
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox TWO" />
</LinearLayout>
Here is the output i got :
I have a Ui which shows customer information. The problem I have is some customer name(android:id="#+id/customerName") is long and does not fit in the textview. what option i ahev to beable to display them other then the option to give full layout width to the name or removing the code textview from next to it(as seen in the xml file android:id="#+id/customerName").
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow>
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/customerCode" />
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/customerName" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/customerCode"
style="#style/CustomerTextView"
android:gravity="right" />
<TextView
android:id="#+id/customerName"
style="#style/CustomerTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="right"
android:maxEms="5" />
</TableRow>
<TableRow android:gravity="right" >
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/customerAddress" />
</TableRow>
<TableRow android:gravity="right" >
<TextView
android:id="#+id/customerAddress"
style="#style/CustomerTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="right"
android:maxEms="5"
/>
</TableRow>
<TableRow>
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/POBox" />
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/OrganizationName" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/poBox"
style="#style/CustomerTextView"
android:gravity="right" />
<TextView
android:id="#+id/organizationName"
style="#style/CustomerTextView"
android:gravity="right" />
</TableRow>
<TableRow>
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/fax" />
<TextView
style="#style/CustomerLabelTextView"
android:gravity="right"
android:text="#string/phone" />
</TableRow>
====================================STYLE++++++++++++++++++++++++++++++++++++++++
<style name="CustomerTextView">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:layout_margin">5dp</item>
<item name="android:background">#drawable/textview_border</item>
I fixed it by using
android:layout_height="wrap_content"
android:inputType="textMultiLine"
hope this works for you too.
One option is to make it ellipsize, so if a name is 'Really Long Name", it might show up as "Really Lon...". To do this:
android:ellipsize="end"
android:singleLine="true"
On the other hand, you can make the textview change size to fit the text using:
android:layout_width="wrap_content"
But I'm not sure if it'll work with everything else you're trying to do (eg fill_parent and width inside a tablerow)
remove the android:maxEms="5" line in xml file and than try
You can make the TextView expand to a second line by setting singleLine attribute to false.
<TextView
android:singleLine="false"
/>
You can try and make the textview scrollable so that when the data to display is too much,scrolling will reveal the hidden text.
In the xml file,change LinearLayout to ScrollView layout.