Note that there are some explanatory texts on larger screens.

plurals
  1. POAbsListView LayoutParams Set Layout Gravity
    primarykey
    data
    text
    <p>How can I set the layout gravity for AbsListView.LayoutParams?</p> <p>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.</p> <p>So, I tried to set the layout_gravity for an AbsListView, but it has no option for a gravity.</p> <p>How can I set the layout_gravity programatically in this case?</p> <p>EDIT: (Added source code)</p> <p><strong>The Listview Parent</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;LinearLayout android:id="@+id/footer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>The Listview Child</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>What should I call in the getView of my adapter to push the child to the right according to a variable in the adapter?</strong></p> <p><strong>EDIT 2:</strong></p> <p>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 <code>setGravity()</code> method.</p> <pre><code>&lt;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" &gt; &lt;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" &gt; </code></pre> <p>The second linear layout is useless but having it gets the job done!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload