Android: ImageView next to TextView not showing - android

I have a TextView and ImageView in a ListView row, positioned next to each other. However, the ImageView doesn't show up at all, and doesn't register clicks either. This is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:text="text"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerVertical="true"
android:padding="10dp" />
<ImageView
android:id="#+id/imageView"
android:clickable="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/textView"
android:src="#drawable/ic_action"/>
</RelativeLayout>
The problem seems to lie in the layout_toRightOf line, if I remove it, the ImageView is shown, but in the wrong place. But I don't understand why it's causing a problem. What am I missing?

The issue is that the TextView is pushing the ImageView off the screen.
You can fix this using a LinearLayout and android:layout_weight
eg:
<?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="match_parent">
<TextView
android:id="#+id/textView"
android:text="text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_centerVertical="true"
android:padding="10dp" />
<ImageView
android:id="#+id/imageView"
android:clickable="true"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/textView"
android:src="#drawable/ic_action"/>
</LinearLayout>
More info on the layout_weight attribute:
This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.
For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

You need to use LinearLayout with weight.. if you set fixed width and the size of the phone is small, it will either stretch out of the screen.
//do linearlayout with orientation horizontal
<LinearLayout
...
orientation = "horizontal"
...
>
<TextView
....
android:layout_width="0dp"
android:layout_weight="1"
...
/>
<Button
....
android:layout_width="wrap_content"
...
/>
</LinearLayout>
Play with android:layout_weight, you will understand

Related

Android LinearLayout horizontally or vertically oriented items fill remaining space

I wonder if it is possible to have two items in a LinearLayout one wraps its content and the other fills the remaining horizontal space. I do this frequently in WPF (.NET) by specifying
HorizontalAlignment="Stretch".
For example:
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:background="#0000FF"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="24dp"
android:background="#FF0000"/>
When I do this the second ImageView fill the whole horizontal space as I expected. I tried to set both wrap_content and use gravity such as android:gravity="start" and android:gravity="fill_horizontal" for the second one it did not worked.
NOTE: I can achieve something similar by specifying weight attribute. But this is providing a division according to the percent value. This is not I want.
You can do this using the weight attribute itself. Try the following method
<ImageView
android:layout_width="wrap_content"
android:layout_height="24dp"
android:background="#0000FF"/>
<ImageView
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="#FF0000"/>
include this in your horizontal linear layout
You need to use a Relative layout for this. Also you either need to specify a width for first image view or it should contain an image otherwise, how can the imageview wrap the content.
You can use like below :
<?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" >
<ImageView
android:layout_alignParentLeft="true"
android:id="#+id/imgFirst"
android:layout_width="50dp"
android:layout_height="24dp"
android:background="#0000FF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_alignParentRight="#+id/imgFirst"
android:layout_toRightOf="#+id/imgFirst"
android:background="#FF0000" />
</RelativeLayout>

Text being truncated in TextView with width set to wrap_content

I'm trying to create a simple component in my layout, where there are two TextViews horizontally next to each other. The one on the right should start where the one on the left finishes. My code for this is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="25sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
I programmatically set the text on each TextView after the view has rendered. However, sometimes the text does not display correctly in the first TextView- I can see that the width has been set correctly, as the second TextView is not next to it, but the text is truncated rather than using the space. If I lock/unlock the device to refresh the screen then the text displays correctly (without the widths of the TextViews changing).
I've tried changing this to use a RelativeLayout, but I see the same issue.
Any ideas?
Although i dont understant what exactly you mean, would suggest you to use weightSum property in the parent view and android:layout_weight in child views. The same allows to put many child views inside a parent view with respect to ratio (like navigation tabs).
for eg :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#android:color/white"
android:textIsSelectable="false"
android:textSize="25sp"
android:layout_weight="0.4" /> //60% width
<TextView
android:id="#+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#android:color/white"
android:textSize="20sp"
android:layout_weight="0.6" /> //40% width
</LinearLayout>
also, dont forget to put the width if child views as 0dp. as that will result in ignoring the calculations regarding the width of view. or you can set the width of child view as "match_parent" as well. any other property to width will not work. (and if you want half matchparent for both child views set layout_width to 0.5 both views.. ithink thats obvious to note)
Hopw it helps.

Android - Button text center gravity not working

I have this XML layout in my App (example of one button):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollViewMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff0e8" >
///some views
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />
///some views
</RelativeLayout>
</ScrollView>
But when I start my app at Android 4.3, the text of buttons isn´t in center of button. Look at the screenshot (in red rectangle):
Where can be problem?
EDIT:
Whole layout
When you specify:
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
It does make the bottom/right/top edge of your button match the bottom edge of the given anchor view ID & accommodates bottom/right/top margin, but while doing that, the android_gravity does not take the resultant height/width into consideration.
So the gravity of the text is center according to wrap_content for layout_height and layout_width.
You can verify that by setting values for layout_height and layout_width (Eg. 200dp and 100dp to try with) and you will get the text with gravity center but for that height and width.
To confirm the same, what you can do is use a container LinearLayout for your Button like:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckStastnych"
android:layout_alignRight="#+id/chckStastnych"
android:layout_alignTop="#+id/chckStastnych"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118" >
<Button
android:id="#+id/btnVysledkyStastnych"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffe118"
android:gravity="center"
android:onClick="vysledkyStastnych"
android:text="Archiv výsledků"
android:textStyle="bold" />
</LinearLayout>
Set the gravity of LinearLayout as center and then center the Button within or as shown above, use layout_gravity for the button to center it in parent LinearLayout.
This will work as a solution when you do that for all 4 buttons, however there might be better options if you restructure your xml and avoid this kind of nesting.
Try This:
<Button
android:id="#+id/btnVysledkySportka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/chckSprotka"
android:layout_alignRight="#+id/chckSprotka"
android:layout_alignTop="#+id/chckSprotka"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/textView3"
android:background="#ffe118"
android:gravity="center_vertical"
android:onClick="vysledkySportka"
android:text="Archiv výsledků"
android:textStyle="bold" />

ListView row layout height not being set

I want my rows to be 50dp high, but for some reason it will only wrap_content.
Here is my xml:
<?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="50dp"
android:background="#drawable/master_selector" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="3dp"
android:contentDescription="Category" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:text="#+id/label"
android:textColor="#ebebeb"
android:textSize="25sp" >
</TextView>
</LinearLayout>
I cannot figure out WHY the 50dp is having no effect? It's on a regular ListView
Edit: Screenshot:
As you see, the rows are close together. The text is only 25sp (on regular text size).
Edit: Changing TextView height to 50dp and gravity to center_vertical fixes it.
The layout values of an inflated view can be easily forgotten in some implementations (Usually because they're added with a specified LayoutParam object as is the case in ListView).
You can fix this by adding one of the childrens heights to be 50dp which will correctly display.

How to layout view right aligned and bottom of an LinearLayout

I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.
how can I do that? I tried that, but both 'textview' and image view at left aligned.
<TextView
android:id="#+id/uptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"/>
<TextView
android:id="#+id/downtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="right|bottom"/>
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|bottom"/>
</LinearLayout>
Don't use a LinearLayout. Use a RelativeLayout, with
your first TextView set with android:layout_alignParentLeft="true"
your second TextView set with android:layout_alignParentBottom="true" and android:layout_alignParentRight="true"
something similar for your ImageView, which presently looks like it is going to overlap the second TextView
I realize this post is a bit old but just in case someone comes across this in their search for clarity;
The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">
<TextView
android:layout_width="40dp"
android:layout_height="20dp"
android:text="test"/></LinearLayout>
It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.
Hope this helps.
Using RelativeLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hi"
android:textStyle="bold|italic"
android:gravity="right"/>
<TextView
android:id="#+id/text2"
android:layout_below="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="All"
android:gravity="right"/>
</RelativeLayout>

Categories

Resources