AbsListView LayoutParams Set Layout Gravity - android

How can I set the layout gravity for AbsListView.LayoutParams?
I am using this in a custom array adapter where I need to set the layout gravity based on a variable. My list child contains a linear layout root but in the adapter, I cannot set the LinearLayout params as i get the error that LinearLayout.LayoutParams cannot be cast to AbsListView.Layout params.
So, I tried to set the layout_gravity for an AbsListView, but it has no option for a gravity.
How can I set the layout_gravity programatically in this case?
EDIT: (Added source code)
The Listview Parent
<?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="#drawable/messaging_background"
android:orientation="vertical"
android:padding="2dp" >
<ListView
android:id="#+id/messageThread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/list_separator"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:cacheColorHint="#00000000"
android:divider="#null"
android:dividerHeight="2dp"
android:fastScrollEnabled="false"
android:listSelector="#00000000"
android:transcriptMode="disabled" />
<View
android:id="#+id/list_separator"
android:layout_width="match_parent"
android:layout_height="1.0dip"
android:layout_above="#+id/footer"
android:background="#android:color/white" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/filter"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="#null"
android:contentDescription="#string/message_filter_button"
android:scaleType="fitCenter"
android:src="#drawable/filter" />
<EditText
android:id="#+id/send_message_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:imeOptions="actionSend"
android:inputType="textMultiLine"
android:maxLines="6"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarDefaultDelayBeforeFade="200"
android:scrollbarFadeDuration="300"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<ImageButton
android:id="#+id/send"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="#null"
android:contentDescription="#string/send_message_button"
android:scaleType="fitCenter"
android:src="#drawable/send_btn" />
</LinearLayout>
</RelativeLayout>
The Listview Child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/incoming"
android:orientation="horizontal" >
<TextView
android:id="#+id/messaging_bubble_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-light"
android:maxWidth="200dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="true" />
<TextView
android:id="#+id/messaging_bubble_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:paddingRight="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textIsSelectable="false" />
<ImageView
android:id="#+id/messaging_bubble_megaphone"
android:layout_width="20dp"
android:layout_height="match_parent"
android:contentDescription="#string/message_child_megaphone"
android:src="#drawable/megaphone"
android:visibility="gone" />
</LinearLayout>
What should I call in the getView of my adapter to push the child to the right according to a variable in the adapter?
EDIT 2:
A possible solution:
Wrap the whole child under another element under the root and then you can set gravity instead of layout gravity which can be easily done using the setGravity() method.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/messaging_bubble_wrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/incoming"
android:orientation="vertical"
tools:ignore="UselessParent" >
The second linear layout is useless but having it gets the job done!

To change the gravity you could wrap the existent row layout in another layout(FrameLayout for example) and then simply set the gravity on the LinearLayout from that extra layout(which now can be moved inside the parent).
To avoid adding an extra level in the layout you could keep the current row layout and replace the root LinearLayout with a RelativeLayout and change the position of the views inside it according to your desired gravity.

I solved this problem by creating another "useless view (in eclipse's words)" under my root element.
I set the width of the parent to match_parent and then changed the gravity to move the "useless view" left or right. The setGravity(Gravity.LEFT) method is allowed on the AbsListView.

I think you need to get rid of the ListView. Replace the ListView with a FrameLayout and use the layoutinflater to add the "listview" child into the FrameLayout. When using the layoutinflater set the FrameLayout.LayoutParams with the gravity property assigned to the root of the child (LinearLayout). Good luck.

You can set the gravity of the child's content with setGravity():
View child;
boolean left;
child.setGravity(left ? Gravity.LEFT : Gravity.RIGHT);
And the child should be changed to android:layout_width="match_parent".

Related

My scroll view doesn't scroll

I have a xml view like this, and as you see I have scroll view in it, but scroll view doesn't scroll and just match screen size, and I'm not able to see data under the screen.
This is my code:
CODE HAS BEEN UPDATED
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/aboveRelative"
android:background="#color/darkOrange">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/category_text"
android:textSize="23sp"
android:textColor="#color/white"
android:text="Category"
android:paddingLeft="10sp" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/topVoices"
android:textSize="23sp"
android:text="#string/topVoices"
android:paddingLeft="10sp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/trendlist"
android:elevation="2dp"
android:background="#color/white"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/moreTrend"
android:layout_gravity="right"
android:textSize="23sp"
android:text="More"
android:paddingLeft="10sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newVoices"
android:textSize="23sp"
android:text="#string/newVoices"
android:paddingLeft="10sp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/soundlist"
android:background="#color/white"
android:layout_below="#+id/newVoices"
android:elevation="5dp"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/soundlist"
android:id="#+id/moreNew"
android:layout_gravity="right"
android:textSize="23sp"
android:text="#string/more"/>
<LinearLayout
android:id="#+id/yoursongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#color/white"
android:padding="10dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
I'm sure that there are some data under the screen but scroll view doesn't let me scroll it and see them.
Scrollview layout can only have ONE child layout. You still have TWO linear layout children in the XML you posted.
This is explained in the first paragraph of the Android developer documentation for ScrollView:
http://developer.android.com/reference/android/widget/ScrollView.html
Your layout is wrong. Here's what you gotta do:
Change your ScrollView layout_height attribute to match_parent.
The ScrollView will take the whole screen and make itself scrollable, that's why you need it as match_parent. The scrolling happens inside the View.
Put everything inside the ScrollView in a Linear or RelativeLayout. (see below)
The ScrollView extends FrameLayout, so it can only have a single child view.
IMPORTANT: this layout must have the attribute layout_height set to wrap_content, otherwise the ScrollView won't have anything larger than itself to scroll.
I'd also remove the root layout from the XML and leave the ScrollView as root.
Simply because it's pointless and it requires extra work from the LayoutInflater.
EDIT: your layout is also way more complex that it needs to be.
It lead me to believe you did not have a single child inside the ScrollView (which you do).
Still, item #1 should fix your problem.
Finally, I found answer. The problem was from fillviewport in scroll view. if you remove fillviewport and set fix size for listview, you can scroll.

Android-How to center my custom View in LinearLayout?

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<com.example.root.howold.MyRing
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I also try to set gravity and layout_gravity, but they don't work actually.
How can I make MyRing(my custom View) center in Layout?
Try this:-
<?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" >
<com.example.root.howold.MyRing
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Read this:
Gravity and layout_gravity on Android
To make your view aligned center, you can:
LinearLayout case1:
"android:gravity" is for it's children views.
If this doesn't works, check it's parent view's width and height.
And, set parent's width and height to bigger size.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST" />
</LinearLayout>
LinearLayout case2:
"android:layout_gravity" is for it self, if parent view has empty spaces.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST" />
</LinearLayout>
RelativeLayout
You can use "android:layout_centerInParent" for RelativeLayout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="TEST" />
</RelativeLayout>
If your using root layout as linear layout. You can use below text that will set your custom view to center.
Depends on your requirement you can user
layout_gravity="center"
(or)
layout_gravity="center_vertical"
(or)
layout_gravity="center_horizontal"
Below is the xml which has custom view.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Here you may some other views -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<com.purpletalk.root.sampledemo.MyRing
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="hellooooo" />
</LinearLayout>
</LinearLayout>
If you are using Linear Layout,
Option 1: Add android:gravity to the parent view, to center all its child
views.
Example:
<LinearLayout
android:gravity="center">
<TextView/>
<TextView/> // Both these textviews will be aligned center
</LinearLayout>
Option 2: If you want to center only a child view, then add the attribute
android:layout_gravity="center" to the childview tag.
<LinearLayout>
<TextView
android:layout_gravity="center" // Only this textview will be aligned center
/>
<TextView/>
</LinearLayout>

layout_alignParentRight not working for relative layout inside linear layout

I have two relative layout inside linear layout. I want the first relative layout left align and the second relative layout right align. I set the following line in the second relative layout but it is not working.
android:layout_alignParentRight="true"
and my full xml file goes here.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100">
<RelativeLayout
android:id="#+id/left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview">
<ImageView
android:id="#+id/leftPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="left panel"
android:layout_marginTop="5dp"
android:layout_below="#id/leftPanelImage"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/rightPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="right panel"
android:layout_marginTop="5dp"
android:layout_below="#id/rightPanelImage"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
My current code gives an output like this. Second layout start after first layout. I want it to start from the right from parent.
So my question is how can I set the second relative layout right align.
Thank you.
android:layout_alignParentRight is for children within relative layout. Since your relative layouts are children for linear layout, try using
android:layout_gravity="left"
and
android:layout_gravity="right"
Your layout seems to be very overkill, using LinearLayout weights and RelativeLayout for such a simple layout is not necesseary. Also both these methods requiere double measurement which is slower. I would suggest to always use as few elements as possible – for your layout try something like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
android:weightSum="100">
<TextView
android:layout_width="0dp"
android:layout_weight="45"
android:layout_height="wrap_content"
android:drawableTop="#drawable/sports"
android:drawablePadding="5dp"
android:text="left panel"/>
<View
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="match_parent"/>
<TextView
android:layout_width="0dp"
android:layout_weight="45"
android:layout_height="wrap_content"
android:drawableTop="#drawable/listview"
android:drawablePadding="5dp"
android:text="right panel"/>
</LinearLayout>
android:layout_alignParentRight and android:layout_alignParentLeft are used to align a child right or left inside a relative layout. Of course, it won't work with LinearLayout as parent. You can change your parent layout to RelativeLayout.
android:layout_alignParentRight attribute is meant to work in children of Relative Layouts. You are using in in a child of a Linear Layout.
You can set it to the childs of the RelativeLayout OR use on the parent view android:gravity="right".
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="45"
android:background="#drawable/listview">
<ImageView
android:id="#+id/rightPanelImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:src="#drawable/sports"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right panel"
android:layout_marginTop="5dp"
android:layout_below="#id/rightPanelImage"/>
</RelativeLayout>

Android: In relativelayout Listview disappears when I try to align it above a linear layout

I'm trying to make a page with a listview that has some textviews and buttons beneath. I don't want it anchored at the bottom unless it would be pushed off otherwise. I was thinking of trying a relative layout with the listview aligned above a linearlayout but for some reason it doesn't show up at all
Code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/feet">
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#id/feet"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/total"
android:textSize="32sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/funds"
android:textSize="32sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/buy"
android:id="#+id/buyB"
android:textSize="32sp"
android:onClick="confirmTrans"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/cancel"
android:id="#+id/canc"
android:textSize="32sp"
android:onClick="cancelTrans"
/>
</LinearLayout>
</RelativeLayout>
That's because by default a RelativeLayout positions its elements at the top-right. You will need to add a property to your LinearLayout such as
android:layout_alignParentBottom="true"
Right now, your LinearLayout will be positioned at the top-right and your ListView above that which there is no more room in the window for the ListView to go above the LinearLayout.
If you have such problem in ListView make sure that you use proper inflation method. Parent view group must be specified for correct inflation.
mLayoutInflater.inflate(R.layout.listitem, parent, false);
Instead of
mLayoutInflater.inflate(R.layout.listitem, null);

layout_centerHorizontal is not centering

I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview.
My layout is like this...
<?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="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="#drawable/background">
<RelativeLayout
android:id="#+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/button_background"
android:contentDescription="#string/hotelBtnDesc"
android:gravity="center"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/down" />
</RelativeLayout>
</LinearLayout>
The above code produces the view shown here:
You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.
Thank you in advance
Its problem with your
android:layout_margin="8dp"
remove it from both scrollview and imageView
and pass it to RelativeLayout direct.
or add
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
in your RelativeLayout.
and pass
android:layout_centerHorizontal="true"
to your scrollview
they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.
u need to set image view that is under the Relative layout set this image view width fill parent.
Try removing android:layout_weight="1" from your RelativeLayout and see if it works.
In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.
So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.
It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".

Categories

Resources