Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:01:44+00:00 2026-05-26T00:01:44+00:00

I am trying to create a list item layout with 4 items. It is

  • 0

I am trying to create a list item layout with 4 items. It is very similar to the example here with some key differences:

  • the RelativeLayout layout_height may be larger than listPreferredItemHeight
  • the image is a CheckBox
  • there are three TextViews
  • The top and middle TextViews could have visibility set to GONE

What I would like is: If _label is gone, then _message and _definition should be evenly weighted in the layout. If _label and _message are gone, _definition should be centered. I have tried several variations of height, width, gravity, layout_gravity and I even tried to solve this with a LinearLayout (both as the whole layout and just to contain the vertical text). The only way I can get this to work is if I set layout_height to listPreferredItemHeight, but then only two of the text views fit.

<?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:minHeight="?android:attr/listPreferredItemHeight">
    <CheckBox android:id="@+id/alarm_list_item_able" 
        android:text=""     
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="True"
        android:layout_alignParentLeft="True"
        android:layout_alignParentBottom="True"
        android:layout_centerVertical="True"
        android:focusable="false"
        android:layout_marginRight="6dp"
        android:onClick="onClick"
        />
    <TextView android:id="@+id/alarm_list_item_label" 
        android:text="Label place holder" 
        android:singleLine="True"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_alignParentTop="True"
        android:gravity="center_vertical"
        />
    <TextView android:id="@+id/alarm_list_item_message" 
        android:text="Message place holder"
        android:singleLine="True"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:layout_below="@id/alarm_list_item_label"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        />          
    <TextView android:id="@+id/alarm_list_item_location" 
        android:singleLine="True"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"     
        android:layout_below="@id/alarm_list_item_message"
        android:layout_toRightOf="@id/alarm_list_item_able"
        android:layout_marginBottom="5dp"
        android:layout_alignWithParentIfMissing="true"
        android:layout_alignParentRight="true"
        android:paddingBottom="5dp"
        android:text="(0.000, 0.000) 0m"
        android:gravity="center_vertical"
        />
</RelativeLayout>

Adapter getView Code using the xml from blessenm’s answer

@Override
public View getView(int position, View convertView, ViewGroup parent){
    Log.d(Global.TAG,"Position "+position);
    View newView;
    if (convertView != null ) 
        newView = convertView; 
    else { 
        LayoutInflater layout = LayoutInflater.from(MyActivity.this); 
        newView = layout.inflate(R.layout.alarm_list_item, null); 
    }
    String tmp1 = "Place Holder Label"
    TextView label = (TextView)newView.findViewById(R.id.alarm_list_item_label);
    LayoutParams l = label.getLayoutParams();
    if (tmp1.matches("^\\s*$")){
        label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
    } else {
        label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
    }
    label.setText(tmp1)

    String tmp2 = "Place Holder Message"
    TextView message = (TextView)newView.findViewById(R.id.alarm_list_item_message);
    l = message.getLayoutParams();
    if (tmp2.matches("^\\s*$")){
        message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
    } else {
        message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
    }
    message.setText(tmp2);

    TextView location = (TextView)newView.findViewById(R.id.alarm_list_item_location);
    location.setText("location");

    return newView;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:01:45+00:00Added an answer on May 26, 2026 at 12:01 am

    Layout_gravity will only work inside a linear layout. When you want all your textviews to take even height, put them in a linearlayout with vertical orientation and give weight as 1 for all the textviews. So now all of them will take equal heights.

    When you need to hide the textviews, just set their layout_weight to 0, now the remaining textview will take the full available space. And if you give the textview gravity=”center”, the text will be centered both vertically and horizontally.

    Edit: The xml and getView method

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <CheckBox android:id="@+id/alarm_list_item_able" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:focusable="false" 
            android:layout_marginRight="6dp"
            android:onClick="onClick" 
            android:layout_centerVertical="true"/>
        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/alarm_list_item_able" 
            android:orientation="vertical"
            android:layout_centerVertical="true">
            <TextView android:id="@+id/alarm_list_item_label" 
                android:text="Label place holder" 
                android:singleLine="True" 
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>
            <TextView android:id="@+id/alarm_list_item_message" 
                android:text="Message place holder"
                android:singleLine="True" 
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content" 
                android:layout_toRightOf="@id/alarm_list_item_able"
                android:layout_below="@id/alarm_list_item_label"/>
            <TextView android:id="@+id/alarm_list_item_location" 
                android:singleLine="True"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="(0.000, 0.000) 0m" 
                android:layout_toRightOf="@id/alarm_list_item_able"
                android:layout_below="@id/alarm_list_item_message"/> 
        </LinearLayout> 
    </RelativeLayout>
    

    The getview method

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.list, null);
            holder = new ViewHolder();
            holder.label = (TextView)convertView.findViewById(
                    R.id.alarm_list_item_label);
            holder.msg   = (TextView)convertView.findViewById(
                    R.id.alarm_list_item_message);
            holder.loc   = (TextView)convertView.findViewById(
                    R.id.alarm_list_item_location);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
    
        if(holder != null){
            if(data.get(position).mLabel.equals("")){
                LinearLayout.LayoutParams labelLp = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,0);
                if(!data.get(position).mMsg.equals("")) {                   
                    holder.label.setLayoutParams(labelLp);                  
                    holder.msg.setText(data.get(position).mMsg);                    
                    holder.loc.setText(data.get(position).mLoc);
                } else {
                    holder.label.setLayoutParams(labelLp);                  
                    holder.msg.setLayoutParams(labelLp);                    
                    holder.loc.setText(data.get(position).mLoc);
                }
            } else {
                holder.label.setText(data.get(position).mLabel);
                holder.msg.setText(data.get(position).mMsg);
                holder.loc.setText(data.get(position).mLoc);
            }
        }
        return convertView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an item list, diffrent for each i and j
Trying to create a list to return some JSON data to a view. Following
I'm trying to create a list of tasks that I've read from some text
I'm trying to create a list that some of the elements are defined and
Working in C# win forms, I'm trying to create a list of items where
What's going on here? I'm trying to create a list of functions: def f(a,b):
I'm trying to create a two column layout. the left column contains a list
I'm trying to create a custom ListView layout similar to android.R.layout.simple_list_item_2. However, when I
I'm trying to create an html list where the user can edit the items
I've been trying to create a RelativeLayout with a header, body (list) and footer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.