ExpandableListView problems - android

I'm trying to create an expandable list where I can make the height of the group_row smaller, to use less screen space. When I change the layout_height of the LinearLayout, it changes the height to what I want it, but all of the labels of each TextView aren't displayed; they disappear. However, when it is wrap_content, the text appears. Any ideas how to fix?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp">
<TextView android:id="#+id/childname
android:paddingLeft="0dp"
android:textSize="16dp"
android:textStyle="italic"
android:layout_width="150dp"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>

Textview id should be android:id="#+id/childname"

Related

TextView gets cut after Rotation

I would like to rotate a TextView 90 degrees. This works, however, the TextView gets "cut" in the y-axis (namely to the screen width, see screenshot).
How could I fix this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:rotation="90"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="256"
android:textSize="50mm"
android:gravity="center"
android:id="#+id/TextView" />
</LinearLayout>
EDIT:
I am sure that the TextView gets cut, not my Layout.
Try changing the android:layout_height in the Textview to "wrap_content".
Besides doing as CodeZero said, add this to the LinearLayout:
android:clipChildren="false"
android:clipToPadding="false"

Allign text inside TextView to it's border completely

Even when gravity is set to top the text has some padding between it and view's border. I need to text to completely touch the border.
Bellow you can see the gap I'm trying to get rid of.
And here is the layout used:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/someView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is some text"
android:textSize="30dp"
android:gravity="top" />
</LinearLayout>
Try to create 9.patch line and set it to your TextView: android:drawableTop="#drawable/your_line".
Then set android:drawablePadding="-10dp"
This trick must work perfectly.
Try this on TextView in your xml file :
android:includeFontPadding="false"
--
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/someView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is some text"
android:textSize="30dp"
android:includeFontPadding="false"
android:gravity="top" />
</LinearLayout>
In xml try,
android:paddingTop="-5dp"
Negative paddings always seem to work for me.
Set the height manually instead of wrapping the content.
The linear layout is the item with the padding. You need to modify that element as well to remove the space.

How to vertically align text within TextView

I am trying to get some text vertically aligned within a TextView, but for some reason it just sticks to the top edge of such TextView.
Trust me, I've read several questions here on Stack Overflow regarding the same, or similar, questions and tried applying the provided solutions, but nothing seems to make a change.
For instance, what I thought would solve the issue right away was to add the attribute android:gravity="center_vertical|left", but that didn't help either.
This user's question describes almost exactly my issue, except that his TextView is inside a RelativeLayout which in turn is inside a ScrollView, which I don't (and don't need or want to) use. And the images he provided are also perfectly applicable to what I'm trying to describe and achieve.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<ImageView
android:id="#+id/account_server_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/account_server_icon"
android:paddingLeft="16dp"
android:gravity="left" />
<TextView
android:orientation="vertical"
android:id="#+id/tv_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:gravity="left|center_vertical" />
</LinearLayout>
The problem is that your TextView's height is set to wrap_content. This means that the size of the text's container will be the same as the height of the text, thus there really is nowhere within the view to center the content (it is effectively already centered vertically).
If the LinearLayout that you posted only contains the ImageView and the TextView, then you can simple change the height to match_parent and it should work fine.
If the LinearLayout contains other Views, you might want to consider using a RelativeLayout and setting the TextView to align with both the top and bottom of the image.
as your achievement suggest, you only need to change your gravity to layout_gravity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2" >
<ImageView
android:id="#+id/account_server_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/account_server_icon"
android:paddingLeft="16dp"
android:gravity="left" />
<TextView
android:orientation="vertical"
android:id="#+id/tv_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:layout_gravity="left|center_vertical" />
</LinearLayout>
You need to set gravity then you set text alignment to gravity
<TextView
android:orientation="vertical"
android:id="#+id/tv_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:gravity="left|center_vertical"
android:textAlignment="gravity"
/>

display textview above a button without hardcoding the height in xml

I am displaying a textview(I intend this to fill the entire screen excluding the button below it) and a button(small one at the bottom) in an activity. I want textview to be placed aove the button. I don't want to hardcode any height/width.
Hence
For button I have kept height ad width as word_wrap
For textview I have kept width as fill parent.
What I want to know is that, is there anyway by whcih I can specify textview height to be screenheight-button height. I want to this in xml file but not dnamically inside my code.
To achieve this you have to create something like this :
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button1"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
and you have a textview which will stay above your button and will fit the entire screen.
Use RelativeLayout, set textview's dimensions as fill_parent, then place button below it,
set button's android:layout_below="#+id/textview" and button's dimensions as wrap_content.
Check the result in visual designer or in device, it should work
You can use layout_weight attribute
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="TextView" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Problems with custom spinner alignment and width

I've created a custom spinner using my own ProjectSpinnerAdapter. I've also got another class which just inflates the layout but hasn't been implemented yet so is still using the standard spinner.
I can't seem to get my custom spinner to align the same way as the standard spinner.
I'm also having problems when the length of the item is wider than the table cell as you can see below. Is there not a way to truncate the options if they are over a certain length ? - This doesn't seem like its the correct way to fix this but I can't directly set the width of the widget as this is controlled via the table layout.*
**Fixed, See update*
Screenshots
The standard spinner aligns perfectly with the Button it sits next to (they are both contained within the same TableRow)
layout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1" >
<TableRow>
<Button
android:id="#+id/selectButton"
android:text="#string/show" />
<Spinner android:id="#+id/projectSpinner" />
</TableRow>
spinner_list_item.xml (Uses maxLength)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:maxLength="15"
android:gravity="center"/>
</LinearLayout>
Update
The issue was the layout of the spinner item, in my custom layout spinner_list_item.xml I'd specified a layout all I needed was the textview
spinner_list_item.xml (Uses maxLength)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:maxLength="15" />
If I may ask, why did you choose the TableLayout? I think the same could be achieved with a RelativeLayout and layout_toRightOf layout_below, etc. Or a LinearLayout with a weightSum and gravity each child having a layout_weight.
I'm not going to give you a solution that changes your entire layout though ;), but I tested this and this seems to work for me.
<TableRow>
<Button
android:id="#+id/selectButton"
android:maxLength="10"
android:text="sdadadasdfffffffffffffffffffffffff"/>
<Spinner
android:id="#+id/projectSpinner"
android:ellipsize="end"
android:lines="1"
android:scrollHorizontally="true" />
</TableRow>
Hopefully this works and it's a better way than setting a fixed string length
edit
I'm not sure why you're populating the spinner the way you are, but you can try:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLength="15"/>
</RelativeLayout>
android:gravity is wrong too, needs to be android:layout_gravity (but that's LinearLayout only
Last edit :D
try using the default layouts
view = vi.inflate(android.R.layout.spinner_list_item, null);
then
((TextView) view.findViewById(R.id.text1)).setText(title);

Categories

Resources