I can't understand the use of android:gravity and setGravity() of GridView.
android:gravity
Specifies the gravity within each cell.
center_horizontal - Place object in the horizontal center of its container, not changing its size.
setGravity(int gravity)
Set the gravity for this grid. Gravity describes how the child views are horizontally aligned. Defaults to Gravity.LEFT
gravity int: the gravity to apply to this grid's children
I am trying to center the last row as mentioned in this question, but I can't get it.
So, I tried to understand the use of android:gravity and setGravity().
I tried this code.
<GridView
android:id="#+id/grid_view"
android:stretchMode="none"
android:numColumns="auto_fit"
android:columnWidth="140dp"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="match_parent" />
its child view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/chapter_item_layout"
android:minHeight="120dp"
android:background="#color/red">
<ImageView
android:id="#+id/thumbnail_image"
android:layout_width="68dp"
android:layout_height="68dp"
android:scaleType="fitXY"
android:src="#drawable/icon" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#color/black"
android:maxLines="2"
android:ellipsize="end"
android:id="#+id/title" />
</LinearLayout>
and it coming like this
if changed the stretchMode="columnWidth" and it is coming like this
The first image denoting that the child rows are not aligning right.
The second image denoting that the contents inside the child are not aligning right
Then what is the use of setting gravity?
I tried gridView.setGravity(Gravity.CENTER).
I need to display the dynamic items in the GridView.
How can I display the 3rd item in the center, if I have 3 items?
I tried setting layout_width="wrap_content" & putting my GridView inside
LinearLayout and setting layout_gravity:center
RelativeLayout and setting layout_centerInParent=true
Both cases not worked.
I cannot use TableLayout because I need to load more data on scroll to bottom, so I need to set OnScrollListener
Thanks in advance
put your GridView inside a RelativeLayout and then set GridView's layout_centerInParent property to true.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/grid_view"
android:stretchMode="columnWidth"
android:numColumns="2"
android:columnWidth="140dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
Because :
RelativeLayout gives you much better control over positions of children.
UpDate :
Try to change this two property
android:stretchMode="columnWidth"
android:numColumns="2"
Remove this property if you add in the GridView android:gravity="right".
Related
How i can insert an textview with height=100dp in parent layout with height=60 and display cropped textview?
I want to resize parent layout without resizing textview inside.
Set a negative value to the layout_marginTop of your TextView, and after resizing the parent you should also set that layout_marginTop to zero. The layout is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_marginTop="-40dp"
android:layout_height="100dp"
android:text="text" />
</LinearLayout>
I have a GridView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="horizontal" >
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/DarkSlateGray"
android:layout_weight="1"
android:columnWidth="200dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none" >
</GridView></LinearLayout>
I want to add an ImageView on top of this.
Tried <include>:
<include layout="#layout/connection"/>
connection.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_connection"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:src="#drawable/globe_connected_icon_48"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></RelativeLayout>
But result looks like this:
Is there a way to show it on top of the GridView like this:
Change the Linear Layout to a Relative Layout, then have both the grid view and the image view align to the top left of the parent. Make sure the the image view is below the grid view so that it appears on top.
Change your LinearLayout orientation to vertical and place the ImageView above the GridView code. For LinearLayout you can provide attributes to define where in the layout a view will be, such attributes: gravity and layout_gravity. RelativeLayout provides similar attributes: ex.layout_alignParentTop. Also, use android:background="#null" in your ImageView if you want the white boarder around your image to be transparent.
I have an ImageView and TextView. I want to place the TextView below the image view but with the middle of the TextView aligned with the middle of the ImageView (along the horizontal axis). If the text in the TextView changes to something much larger or smaller, the middle of the text always needs to remain aligned with the middle of the ImageView. Is this possible in xml?
Yes, you are simply looking to contain both elements in a vertical LinearLayout which has android:gravity set to center_horizontal.
Something like this:
<?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:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
... />
</LinearLayout>
Because the TextView's width is wrap_content, setting its gravity shouldn't be necessary. I would do it just for safety (and additionally, I may set the width to match_parent as well).
You can make use of RealtiveLayout as follows..
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
... >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
<TextView
android:layout_below="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
If you want your textview to be of single line only then add following properties to TextView
android:ellipsezed="end"
android:singleLine="true"
Use a given layout and add it to any of your existing layout you get result what your looking for ..
Hope this explanation works for you..
set TextView android:layout_width="fill_parent" and set android:gravity="center" in TextView.
I think it help you.
Thanks
I have a listview with custom rows. In the row there are these items:
<?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="wrap_content">
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:focusable="false"
android:scaleType="fitXY">
</ImageView>"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
<TextView android:id="#+id/textView1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:maxLines="1"
android:ellipsize="end">"
</TextView>
<TextView android:id="#+id/textView2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:maxLines="1"
android:ellipsize="end">
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll2"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="10dp">"
<TextView android:id="#+id/textView3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingBottom="5dp"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:background="#87EB81">
</TextView>
</LinearLayout>
</LinearLayout>
If both textviews has some content, they are centered vertically in the row (almost).
But if the lower textview is empty, I call:
holder.textView2.setVisibility(View.INVISIBLE);
holder.textView.setGravity(Gravity.CENTER_VERTICAL);
Still, the upper textview gets into the top of the row. Why?
If I set it to View.GONE, I get this, but I dont want the height of the row to change:
Use android:layout_weight to set all the childs of the main LinearLayout to mesure the same.
Use a LinearLayout as parent for textView1 and textView2. Use Use android:layout_weight to have the same size for each one.
Define TextView one with android:gravity="center_vertical|left" Then, if you don't want to show the second TextView, set its visibility as View.GONE.
Maybe this will resolve your problem,
Update:
In a RelativeLayout, views are aligned with their parent, with the RelativeLayout itself, or with other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called layout_alignWithParentIfMissing.
This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title's bottom with its own bottom.
For more look at Layout Tricks: Creating Efficient Layouts
This can be possible in your RelativeLayout by setting up the layout_below and layout_above with the help of parent alignment by using layout_centerInParent and set the value true of layout_centerHorizontal
Hope, this will work.
Try to use:
<TextView
...
android:layout_height="?android:attr/listPreferredItemHeight"
...
/>
in your_list_item.xml. And use View.GONE
Try to fix some value to your RelativeLayout's height like-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
To Solve for your problem what u can do is have the LinearLayout fill Parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
And use View.GONE This will make the Row Height Same.
If you see a gap after Best Car in World then try using Relative Layout and align last Layout to bottom.
Basically I have a horizontal scroll view which, by monitoring the onTouch event I'm paging (ie:each visible part of the scroll view (page) "clicks" into the next when scrolling, rather than just having a standard ScrollView. see paged scrollviews in iOS).
Now I want to find a way to have inner children inherit the same width as the scrollview (which is set at "fill_parent").
Here is my XML to help:
<HorizontalScrollView
android:id="#+id/scrollview"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="#drawable/scrollviewbg">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/content1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/content2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#drawable/content3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
As you can see the scrollview is set to width fill_parent and there is a LinearLayout inside it. Now at the moment there are three images which have a set width and height similar to the width of the screen, but what I want to do is change that for 3 LinearLayouts.
Each LinearLayout needs to inherit the same width as the scroll view, so that each one takes up a whole "page" when my code is applied, as it were.
How would I do this? Is it something I will have to do using code? What code will I need?
Thank you.
I know its not direct answer, but its much easier to user ViewPager from Compatibility Package for paging functionality. You can find an example in samples folder (see src/com/example/android/supportv4/app/FragmentPagerSupport.java for source code).
Try this:
android:fillViewport="true"
<HorizontalScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="right"
android:background="#drawable/black_border"
android:id="#+id/displayText"
android:layout_width="match_parent"
android:scrollHorizontally="true"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="20dp"
android:text=""
android:textSize="20sp" />
</HorizontalScrollView>
inside horizontal scroll view, i have a text view, you can try for image view
Set all the layout_height parameters as fill_parent. You should see the image in full screen then.