In my listview one row was in bigger in size.
Initially all are in same width and height
After scrolling some time one row become bigger (see in below image)
now in Entertainment
Scrolling again that Gaming will become bigger
How to resolve?
It looks like your items are varying in height a little bit.
One of the standard things to do for a ListView is to use the Android Attribute listPreferredItemHeight which will provide a preferred item height for the device.
So. Your layout for the list items would look something like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
/>
This will give you a single TextView for each item, with the preferred system height.
Related
I have a very irritating problem with my views. As you see on a galaxy S7 my views is perfectly in center, but on a galaxy S4 mini they are placed down at the bottom!
I have tried to change alot in XML without any succses. I think it may be of the way I designet the whole thing with recyclerview, tabviews etc.
The following dont work either:
layout_InParent, Gravity:Center, layout_centerVertical
Imagebutton item for the recyclerview. item_colorbutton.xml:
android:id="#+id/colorbutton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="15dp"
android:background="#drawable/bbtn">
BACKGROUND tabfragement. tab_fragment_1.xml:
<?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="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="10dp"
android:id="#+id/rvColors"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is the viewpager for the different tabs: BACKGROUND, FONT, TEXT STYLE
<org.m.muddzboy.QuoteCreator.ViewPager.NonSwipeableViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="51pt"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:background="#517293" />
Your parent view seems to be limited height... and your recyclerview wraps children, plus it has top margin of 10dp. So you have 10dp, then 15dp + 15dp top and bottom margin of child view (you set general margin of 15dp, so all sides), plus 48dp height of child view. Seems like on low density devices that is not enough for your view which contains recyclerview. So it follows the rules and pushes your views down. It cannot break set margin. It has to set them as you requested.
In situations like this it's better to have dimensions for different device configurations. And if it's too complex, setting relative dimensions programmatically based on measured height of containing view.
In Android Wear I'm using an WearableListView with WearableListView.Adapter to show items in a list. I have a custom layout for each item that has the parent WearableListItemLayout but it's problematic because the items are limited to approximately/exactly 80dp. I understand this is because of the "snapping" effect and follows the guidelines throughout the Android Wear system. My plan here is not to go outside of these guidelines more than a few dp's and I cannot make more sacrifices to the parts of the item (I have already chosen a too small font, too small icons etc..).
Item
<com.strafe.android.view.WearableListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="100dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:paddingBottom="4dp"
android:paddingTop="4dp">
<!-- Textviews and other stuff -->
</RelativeLayout>
</com.strafe.android.view.WearableListItemLayout>
The code above cuts the item's TextViews and other parts of the item making it only 80dp high no matter if i have minHeight="100dp".
Is there any way to remove this limitation?
I think the device you are using has a screen of 240dp; the WearableListView is designed to work on Wearable devices in a way that each item takes exactly 1/3 of the screen height so in a 240dp display, each item will be 80dp and you cannot change that.
I am using the Code from JawsWare - Overlay View to create a View that lays above everything.
This works fine, but I failed to adapt the given layout to my needs.
What I want: Two Phases.
First Phase: A button in the upper left corner that is always there above all other apps.
When this button is pressed, we enter the Second Phase:
The button is replaced by two images which are to be horizontally aligned at a certain distance from the top, again floating above everything else.
I have to admit that I dont really understand Android Layouting, but I've tried fiddling with a RelativeLayout and an ImageView but it seems buggy.
Additionally, I encountered a very strange behaviour: Even if the Image is only about 200x200 px in the upper left corner, nearly the whole screen is "blocked" by the View (it receives all TouchEvents, even though there shouldt be anything). It seems that if I position the Layout using Margins to float in the middle of the screen, everything to the left and top of the visible button is also receiving touch events and not letting them pass through to the underlying app (even though there is no visible content).
Any ideas to why this happens and how to circumvent that?
Secondly: How can I achieve the two layouts from earlier?
Edit 1: My Layout. (please keep in mind that I just copied this and then did what I thought was right)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:onClick="overlayTextClicked"
android:padding="0dp"
android:id="#+id/overlay_layout_id"
>
<ImageView
android:id="#+id/imageview_info"
android:layout_width="200px"
android:layout_height="200px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/picture_010512233437384578"
android:contentDescription=""/>
<TextView
android:id="#+id/textview_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="info"
android:textColor="#FFF"
android:orientation="vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Afterwards I am trying to set the RelativeLayouts Gravity to TOP | CENTER_VERTICAL with a Top-Margin of 200px.
I believe what's going on here is your TextView is filling out the entire screen.
wrap_content bounds the size of a View with its content. match_parent fills a view to as big as it can get (i.e., whatever size the container is bound to).
So in this case, your RelativeLayout is does not have a max size it's bound to. Your TextView is going to try to get as big as it can get, so it's going to fill the screen. Likewise, the RelativeLayout is going to blow up to that size to wrap around the TextView.
Also, RelativeLayout doesn't really respond to Gravity well. That is used in LinearLayout and FrameLayout containers a lot, but RelativeLayout relational rules like "CENTER_IN_PARENT" are going to override whatever Gravity you set (if you set Gravity.RIGHT and "CENTER_IN_PARENT", then one has to win out I guess).
I've observed a behavior with layout_weight that I can't explain. The following is a trivial example:
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="This is a very very very very very very very very very very very very very very very long string."
android:layout_weight="1"
/>
<View
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:background="#ffffffff"
/>
</LinearLayout>
In a QVGA display, the TextView wraps the text. The white square is displayed to the right of the text.
However, if I remove android:layout_weight="1" from the TextView, the TextView now takes up the entire display width. The white square is no longer displayed.
Why would layout_weight in the TextView affect whether or not the white square is displayed? Shouldn't the View with the white background always be assigned 32dpx32dp first? (It makes no difference if the view were any other types - ImageView or TextView).
The problem I was working on is that I want the white square to always be displayed to the right of the TextView (whether or not the text is wrapped), but I don't want any empty space between the TextView and the white square. (If I add android:layout_weight="1" to the TextView, then there is a gap if the text is not wrapped.)
Any help would be appreciated!
To answer my question #1: One thing I learned by looking at the source for LinearLayout: Not only does layout_weight assign unused space to a child, it also shrinks a child with layout_weight if the child extends beyond the bounds of the LinearLayout. That explains why a TextView with wrapped text is shrunk in my layout.
As for the answer to my question #2, I think you meant android:toRigthOf instead of android:layout_alignRight. Using a RelativeLayout instead of a LinearLayout doesn't change the layout behavior. The tricky part is placing a view immediately to the right of a TextView, without gaps, whether or not the text is wrapped. Setting a maxWidth would limit the TextView's width, but that solution doesn't scale across portrait/landscape and different display dimensions.
Solution - Looks like Dyarish's solution is the best available. My layout problem exists regardless of the layout you use. The key is to set a maxWidth for the TextView so that it doesn't take up the all of the horizontal space in the layout. Because hardcoding a android:maxWidth value in the TextView doesn't scale across different displays, setting the maxWidth at runtime, as Dyarish suggested, is a good solution.
Hopefully this is what you are looking for.
First off, here is a great resource I found for Creating UI's.
layout_weight - Specifies how much of the extra space in the layout to be allocated to the View.
If you want to ensure that the white square is always to the right of the textview, you can use a Relative View, and add the parameter to the view. android:layout_alignRight="+id#yourTextViewID". This should always make the box appear right beside the textView area. You should probably also add something like android:maxWidth="250px" This will ensure that you don't push the white box completely out of the screen.
Here is a code sample:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:maxWidth="250px"
android:id="#+id/TextForWhiteBox"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:text="This is a very very very very very very very very very very very very very very very long string."
/>
<View android:background="#ffffffff" android:layout_width="32dp" android:layout_height="32dp" android:id="#+id/view1" android:layout_toRightOf="#+id/TextForWhiteBox"></View>
</RelativeLayout>
You could also add to the View:
android:layout_alignTop="#+id/TextForWhiteBox" android:layout_alignBottom="#+id/TextForWhiteBox"
to make the white box the same size as the TextView.
Firstly I've tested the code from my other answer and it does exactly what you've described you've wanted. (unless I'm misunderstanding what you are asking for). You definitely do not want to use the android:layout_alignRight which is not what is in the code sample. That would simply keep the box on the right hand of the screen and not be affected by the textview at all. This sample uses android:layout_toRightOf="#+id/TextForWhiteBox" which is possible due to it being a relative layout. Since the Relative Layout allows you to place objects in relation to others. That line will always place the box just to the right of the textview with no gaps.
As for the screen orientation changes:
When the orientation changes it creates a new instance of the view.
Here is a simple solution.
//Add to oncreate in your Activity
private TextView textStatus;
textStatus = (TextView) findViewById(R.id.TextForWhiteBox);
// This get's the width of your display.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
// Now you know the screen orientation, and it's width. So just set the maxwidth of the text view to match the display width - the pixels of your white box.
textStatus.setMaxWidth(width - 32); // 32 is here because you already know the size of the white box. More logic is needed to dynamically get this value, because you would need to wait for the activity to be fully created.
}
Here is the main.xml I used:
<?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"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:id="#+id/TextForWhiteBox"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:text="This is a very very very very very very very very very very very very very very very very very very very long string."
/>
<View android:background="#ffffffff" android:layout_width="32px" android:layout_height="32px" android:id="#+id/view1" android:layout_toRightOf="#+id/TextForWhiteBox"></View>
</RelativeLayout>
You might need some additional logic to keep screen values.
This code has been tested, you should be able to literally copy and paste this to work as you asked.
Also depending on your logic you could use something like this to return the screen orientation.
int orient = getResources().getConfiguration().orientation;
Hope this helps!
If this helped you, please click the accepted button. =) Cheers!
What would cause only a portion of a list view item to highlight when tapped?
I'm working on an Android app where the user navigates through a series of lists, and some of the list items don't highlight properly. The list items have an image on the left followed by text. If the text in the item mostly fills the row, the entire row highlights when tapped. If the text is short, however, then only part of the row highlights when tapped. I'd really like the entire row to highlight regardless of the length of the text. My list item layout looks like 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">
<ImageView android:id="#+id/zoneIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="5dp">
</ImageView>
<TextView
android:id="#+id/itemtitle"
android:layout_width="fill_parent"
android:textColor="#color/listItemColor"
android:textSize="14sp"
android:layout_gravity="top|left"
android:layout_height="fill_parent"
android:padding="13dp"
android:textStyle="bold">
</TextView>
</LinearLayout>
I thought that setting the layout_width of the text view to fill_parent would cause the text view to fill the remainder of the item's width, and that the whole row should highlight. Is there something else I need to set to control the width of the overall item? What am I doing wrong?
Edit: I also have another problem which may be related: When I scroll the list (which normally has a white background), the area occupied by the list items turns black during the scrolling, and then appears normal again when scrolling stops. The black area is the same size as the area that would be highlighted (except that it's all the items rather than just one), which is to say that there are white spaces on the right side of some of the items while scrolling. I'd like to prevent the list from turning black while scrolling, but perhaps that's another question. I mention it because it makes me think that some list items aren't filling their parent, but I'm still not sure why.
The black background is controlled by android:cacheColorHint (XML) or ListView.setCacheColorHint (Java) -- it used for drawing a solid color underneath the list while scrolling for performance reasons.
The list item should highlight in full regardless of the size of the TextView since the LinearLayout fills the parent. However you should certainly change the height of the LinearLayout towrap_content` -- it doesn't make much sense for it to fill a parent since the height of a list (and therefore the maximum size of a list item) is unbounded.
The height of the TextView and ImageView should probably be set to wrap_content as well. See if those changes resolve your issue.