Top elements are hidden in ScrollView with white space at bottom. How to fix the issue - android

I am adding text views to a linearLayout which is in a scrollView.
in 80 tags around 30 are lost on top. And there is some white space at the bottom of the scrollView. As you can see in the picture, top tag is cut(top of scrollView).
ScrollView Xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/backLayout"
android:layout_above="#+id/buttonSeparator"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/tagsLayout"></LinearLayout>
</ScrollView>
text item xml:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_marginLeft="10dp"
android:background="#drawable/tag_background"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#color/text_color"
android:textSize="14sp"
android:alpha="0.70"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
</RelativeLayout>
Adding text views to linear layout:
for(String tag: tagList){
View tagHeader = LayoutInflater.from(mcontext).inflate(R.layout.tag_item, null);
TextView tagText = (TextView) tagHeader.findViewById(R.id.tag);
tagText.setText(tag);
tagsLayout.addView(tagHeader);
}
I found few solutions that said to remove gravity from the layout. still same after removed the gravity parameter. fillViewport set to true did not work either

Related

Android: ImageView next to TextView not showing

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

Fill left over screen space using ViewPager

I'm trying to make a layout in which a ViewPager fills vertically the available space.
Basically what I have here are a bunch of views stacked vertically.
Checkbox
ViewPager
LinearLayout (which will contain the indicator)
Button
I want the checkbox on top, and the button on the bottom, the LinearLayout above the button, and whatever space is left over in the middle to be used by the ViewPager, however I can't seem to be able to make it work.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#color/white"
android:gravity="top|center_horizontal"
android:padding="#dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:background="#drawable/login_button"
android:id="#+id/checkbox_friend_notify"
android:visibility="invisible"
android:checked="false"
android:textColor="#color/black"
android:text="#string/friend_notify"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ViewPager
android:id="#+id/pager"
android:gravity="top"
android:layout_below="#+id/checkbox_friend_notify"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/pager_indicator"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_below="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/start_btn"
android:visibility="invisible"
android:text="#string/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:background="#drawable/login_button"
android:layout_below="#+id/pager_indicator"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Any pointers?
If you simply need a set of views stacked vertically, consider changing your RelativeLayout for a vertical LinearLayout. Then you can keep all your child views the same (getting rid of all the layout above/below attributes) except give the ViewPager android:layout_height="0dp" and android:layout_weight="1"
Explained:
layout_weight tells LinearLayout how to distribute leftover white space, and by giving the ViewPager a weight (and not giving any of the other children any weight values) you're telling LinearLayout that ViewPager wants all of the remaining white space to itself. This only works if you don't define a height (hence why layout_height=0dp).
You can read more about it on the LinearLayout guide.

Android: ScrollView does not re-size when adding child views

I have a ScrollView that I am trying to populate dynamically. Here is the XML that contains the ScrollView:
<?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"
android:background="#f6f5f5" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#40ff0000"
android:paddingBottom="70dp"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<LinearLayout
android:id="#+id/option_list_container"
android:background="#40ffff00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#292929"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:text="Stap 3/5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</LinearLayout>
</ScrollView>
And here is the XML for the element I'm inserting in the ScrollvVew:
<?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"
android:orientation="horizontal" >
<np.custom.FontIcon
android:id="#+id/option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/calendar_circleround"
android:textSize="40sp"
android:gravity="center"
android:text="#string/planit_selection_empty_circle"
android:textColor="#292929" />
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
This is the code I use to populate the scrollview:
ArrayList<String> g = new ArrayList<String>();
g.add("Fix a Bug");
g.add("Buy a new PC");
g.add("Make Coffee");
g.add("Take a Break");
g.add("Don't do that");
g.add("Throw your hands in the air like you just don't care!");
LinearLayout optionListLayout = (LinearLayout) rootView.findViewById(R.id.option_list_container);
LayoutInflater inflater = LayoutInflater.from(getActivity());
for(String p:g){
View v = inflater.inflate(R.layout.planit_option_item, null);
TextView optText = (TextView) v.findViewById(R.id.option_text);
optText.setText(p);
optionListLayout.addView(v);
}
This all works almost fine, except that the sizes of the ScrollView and the LinearLayout it contains do not come out as I expected. This a screenshot of that it looks like on a device, which shows text being cut out when it goes to a second line:
So how can I make sure the linear layout re-sizes to accommodate children views of different heights?
Your issue isn't the ScrollView not accommodating new children. The problem is that the text, when it spans more than one line, is being clipped due to layout parameters in R.layout.planit_option_item. Try changing the TextView of ID #+id/option_text so that its height is wrap_content.
Change your TextView for the ScrollView element to accommodate two lines:
...
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:maxLines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
This is not an issue with ScrollView, it's an issue with the specifications of the element you are inserting into the ScrollView. If you expect there may be more than 2 lines of text in some cases set maxLines = X where X is the maximum number of lines you expect.
Please let me know if this does not work and I would be happy to help further.

ExpandableListView overlaps Text in RelativeLayout

I have relative layout ("relative") with clickable layout ("clickable") on top, expandable list view ("lview") under "clickable", and "footer" text view with version of my app on bottom of "relative":
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/menu_background" >
<com.reconti.app.widgets.Logo
android:id="#+id/reconti_menu_tv_logo"
android:layout_width="fill_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:paddingLeft="#dimen/standart_side_margin"
android:textSize="30sp" />
<RelativeLayout
android:id="#+id/clickable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/menu_tv_logo"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:paddingLeft="#dimen/standart_side_margin" >
<com.reconti.app.widgets.RoundedImageView
android:id="#+id/menu_profile_avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="#drawable/com_facebook_profile_picture_blank_portrait" />
<TextView
android:id="#+id/menu_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/menu_profile_avatar"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</RelativeLayout>
<ExpandableListView
android:id="#+id/lview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:childDivider="#color/menu_divider_color"
android:divider="#color/menu_divider_color"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:groupIndicator="#null"
android:headerDividersEnabled="false"
android:listSelector="#drawable/expandable_row_background" />
<TextView
android:id="#+id/footer"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/white" />
</RelativeLayout>
Everything looks good as long as user hit some option in expandable list. Then "lview" overlaps "footer". I would like to achive effect, that "lview" remains on top of "footer" and "footer" remains on bottom of "relative" and only bottom of "lview" is visible to user (so top goes under "clickable"). I hope I explained it clearly:)
Usually I like to work with linearlayout when needs to keep views above views. I don't like how relative layout works sometimes. Well, you can try my idea, I know this works with list, but didnt try with expandable list (but guess will work).
LinearLayout vertical #relative
Logo #reconti_menu_tv_logo
RelativeLayout #clickable
ListView weight 1 #lview
TextView #footer
In this, only your ListView will scroll. I don't know if is this what you want. Your logo, clickable and footer will be fixed.
The weight 1 is to keep your listivew using all not used space, so your footer will be always on "footer". Don't forget about height 0dp when using weight.
Now, if you want all scrolling, you have to use addHeader(View) and addFooter(View) on your list before set the adapter. (this is the better way)

How to vertically center content within a ListView footer?

I have posted the exact layout below. I want to show an image and text vertically centered inside the footer. I have applied:
android:gravity="center_vertical|center"
To both the LinearLayout containing these elements and the TextView inside but nevertheless the whole line image and text appears way too far towards the top of the footer. I want it centered vertically but instead it is in the top 30 % of the footer at all times.
<?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"
android:orientation="vertical" >
<TextView
android:id="#+id/someMessageMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_margin="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center"
android:paddingBottom="2dip"
android:paddingTop="16dip" >
<ImageView
android:id="#+id/myImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dip"
android:paddingTop="3dip"
android:src="#drawable/picimg" >
</ImageView>
<TextView
android:id="#+id/myMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:gravity="center_vertical|center"
android:text="This message appears way to close to the top of the footer. It should be along with the image in the center:"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:id="#+id/myButton"
style="#style/mybuttonstyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="#string/lbl_send"
android:visibility="gone" />
</LinearLayout>
After seeing your layout it seems you haven't given android:paddingTop to your TextView. Thats why its appearing to the top.
Moreover you can also remove android:paddingTop from ImageView if that suits to your layout.
Hope that helps.
How big is the image? I think the problem is in your second LinearLayout. You're setting the height to be "wrap_content" so that means that it will only be as big as the biggest child. If the image and the text are about the same height, then it won't seem as if anything is getting centered. You can test this theory by forcing the height to be something big enough.

Categories

Resources