I have GridView in one of my android application layouts. I am trying to set Item in center of Gridview but its always stay on the left side. I have searched many similar questions in StackOverflow but not got my problem solved. My XML is like below. Let me know if anyone can help me to make my GridView items in the center.
<FrameLayout
android:id="#+id/framecaregory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/second"
android:layout_alignParentTop="true"
android:layout_marginTop="?actionBarSize">
<GridView
android:id="#+id/gridviewcategory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lite_color"
android:numColumns="5"
android:visibility="gone">
</GridView>
</FrameLayout>
Thanks
The thing you want does not depends on grid Layout, but to its child layout which is adapted to it. First create another layout grid_item_row.xml and put:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/framecaregory"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:text="Text in the center"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
After that adapt this layout to grid layout
Related
I have created a grid layout Like this:
But I want my grid layout some thing like this:
I want my text to be appear on right side. Like Contacts List. Please tell me what changes I must have to do in my layout file. Waiting for a good response.
Since you seem to want a list and not a grid, you should use a LinearLayoutManager, not a GridLayoutManager.
See the official documentation for examples.
Try changing the orientation of your linerlayout to horizontal.
this the xml code just copy and past
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_list_view_tutorial_with_example"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:padding="5dp"
android:src="#drawable/pic1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="YOUR TEXT"
android:id="#+id/album_title">
</TextView>
</LinearLayout>
I have a GridView that I populate through code, the problem is that the last row it's not fully visible. I don't know why this is happening, the layout of the Activity is pretty simple:
<?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:orientation="vertical">
<GridView
android:id="#+id/sectionGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:horizontalSpacing="0dp"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
And the Layout for the row is the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:pew="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white" >
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:id="#+id/sectionImage"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/sectionImage"
android:layout_alignLeft="#+id/sectionImage"
android:layout_alignStart="#+id/sectionImage"
android:background="#drawable/bg_section_row"
android:layout_alignBottom="#+id/sectionTitle"
android:layout_alignRight="#+id/sectionImage"
android:layout_alignEnd="#+id/sectionImage"
android:id="#+id/sectionBgView"></RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#color/white"
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="1"
android:padding="10dp"
android:layout_alignBottom="#+id/sectionImage"
android:layout_alignLeft="#+id/sectionImage"
android:layout_alignStart="#+id/sectionImage"
android:layout_alignRight="#+id/sectionImage"
android:layout_alignEnd="#+id/sectionImage"
android:id="#+id/sectionTitle"/>
</RelativeLayout>
</LinearLayout>
I'm not sure if it might be related but currently i'm using the following theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
What's wrong with the layout? am I missing any property for the GridView?
In GridView android:verticalSpacing or android:horizontalSpacing only works if there is another cell to give spacing between.
Even for the first cell the spacing doesn't get applied in the top but as soon as second cell is drawn the verticalSpacing property works and a gap can be observed between the cell.
In this case, the bottom cell doesn't have any other cell to show below it as a result the last cell looks like being cut from bottom.
Try giving a padding for LinearLayout in row cell.
eg: android:paddingBottom="5dp"
after wasting hours I found out this issue on the library i'm using in order to create a Sliding Menu:
https://github.com/jfeinstein10/SlidingMenu/issues/680
I've added the following code to my BaseActivity (every activity inherits from this):
if(Build.VERSION.SDK_INT >= 21)
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
And it did the trick. The problem seem to happen only on devices with API > 21
I have a layout which holds the grid layout as follows
<?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">
<GridView
android:id="#+id/gridview_gallery_mine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10sp"
android:background="#color/background"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="4"
android:padding="4dip"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/empty_list" />
</RelativeLayout>
</RelativeLayout>
The problem is that, the last rows of the grid is being cut to half. I mean the last row is only half visible. I tried giving the gridview and also its parent (RelativeLayout) padding, margin but nothing works. BTW I have this layout inflated inside a fragment with the tabs.
P.S I am learning android design. Thanks.
I had used the combinations of
Android basic actionbar,
tabhost from support library.
The answer to above question was since i had no other options for implementation, Applying padding bottom on the parent node of the content made it work.
I tried another combination with the Toolbar in appcompat v7.21 (or 21), It worked without the extra padding, So i dint get time to investigate who causing the problem, actionbar or tabhost.
~ Hope it helps
I am creating an application that contains a header with a button on the right that should slide a list view to left when I click on it(similar to the chat list in Facebook)
-->
When I was including this header without putting the list view in it, it was working properly. But now I am inserting this list view inside the .xml file of the header (because the header is always shown for the user and he can click on this list view button in any layout that is shown for him).
The problem that I want to include this header to all the other layouts without changing the way they looks and show this list each time he click on the button.
I searched and found a lot of answers but every solution was about making the same as I did in the previous screen shots in each page which is not an optimistic solution for multiple pages.
*I am working on API level 10 and want to find the solution without using Actionbar.
Tell me please if more information would be helpful, any help will be really appreciated.
Thanks in advance.
UPDATE my xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GeneralRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout android:id="#+id/rel_layout"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#android:color/white" android:layout_alignParentRight="true">
<LinearLayout android:id="#+id/fake_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"></LinearLayout>
<ListView android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/background_dark"
></ListView>
</RelativeLayout>
<RelativeLayout android:id="#+id/rel_layout_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<LinearLayout android:id="#+id/lin_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="vertical"
android:layout_centerHorizontal="true" android:layout_alignParentTop="true">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide"
android:layout_gravity="right|center"></Button>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Try to create a RelativeLayout for the ListView with a android:visibility="gone" and all the other components in the same User Interface inside another RelativeLayout.
Then change its visibility upon the state of the menu if it's opened or closed.
Guess this should be a way to go:
<?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" android:clickable="false">
<LinearLayout
android:id="#+id/thisistopbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="3dp"/>
<include here listview etc../>
</LinearLayout>
you can also make the top bar general and include it in every screen. But the basic idea is like above xml, and put the button on the top bar.
I am having an issue that make me crazy
I have a listview with WebView inside. So I created a custom listview.
The WebView forbid me to click so I created a webviewclicklistener.
My problem was that when I display some images, my webviews are "shaking" as if it wants to load the image a thousand times.
In fact I discovered that the height size of some elements change like every seconds, that give a feeling of shake.
The only way I found to fix it is to give a layout:height value for my listview.
My new problem is that when I put for exemple 600dip, I have a scrollbar, but I can't look at the end of my listviews.
If I put like 1000dip, I don't have scroll bar and I can't see the end of my list neither.
Here is my layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#drawable/woodbackground"
android:layout_weight="1">
<TextView android:background="#android:color/transparent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="Question" android:textSize="30px"></TextView>
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical">
<WebView android:id="#+id/wvplquestion" android:layout_height="wrap_content"
android:layout_width="fill_parent"></WebView>
</ScrollView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_weight="1">
<TextView android:background="#android:color/transparent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:hint="Select Answer" android:textSize="30px"></TextView>
<ListView android:id="#+id/lvquestion" android:layout_width="fill_parent"
android:layout_height="600dip" android:prompt="#string/selectp" />
</LinearLayout>
And my custom listview layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="3dip">
<WebView android:id="#+id/weblistview" android:layout_width="fill_parent"
android:layout_height="wrap_content"></WebView>
Thanks for your help
maybe you can try to change your listview layout height. Instead of wrap content, put a fixed height. I don't know if it will be working, but you can try it. I'd a problem similar to yours and I solved it like this.
I hope it will work. Good luck ;)
Seems it mostly happens when several webviews contained into one scrollview. Seems at least first webviews should have fixed height.