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 8411325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:23:59+00:00 2026-06-10T00:23:59+00:00

I have a listview which contains 2 Textview. one textview is shown inside a

  • 0

I have a listview which contains 2 Textview. one textview is shown inside a bubble background.

<?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="wrap_content" >
    <!--    android:background="#FFDBE2ED"-->
 <LinearLayout 
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/wrapper2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
       >
    <!--  android:layout_gravity="center" -->
        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_marginTop="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginLeft="5dip"
            android:background="@drawable/bubble_yellow"
            android:paddingLeft="10dip"
            android:text="Hello bubbles!"
            android:textColor="@android:color/primary_text_light" />
         <TextView
            android:id="@+id/sender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_margin="0dip"
            android:background="#00dcdcdc"
            android:paddingLeft="10dip"
            android:text="Hello bubbles!"

           android:textColor="#b9dcdcdc"  
            android:layout_below="@+id/comment"
            />


    </RelativeLayout>

    </LinearLayout>


</LinearLayout>

sometimes I display textview on the right, and sometimes on left

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }

        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

        OneMessage coment = getItem(position);

        countryName = (TextView) row.findViewById(R.id.comment);

        countryName.setText(coment.msgText);
        countryName.setGravity(!coment.sentbyuser   ? Gravity.LEFT : Gravity.RIGHT)  ;

        sender = (TextView) row.findViewById(R.id.sender);
        sender.setGravity(Gravity.LEFT)  ;

        countryName.setBackgroundResource(!coment.sentbyuser ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        wrapper.setGravity(!coment.sentbyuser   ? Gravity.LEFT : Gravity.RIGHT);

        return row;
    }

when I display the 2 textview on the left side, everything is fine. however, when I display the 2 textview on the right side, I expect the 2 Textview to be right aligned. But so far I could’t achieve that.

  • 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-06-10T00:24:00+00:00Added an answer on June 10, 2026 at 12:24 am

    TextView.setGravity() is defining how child views are poistioned see docs. You want to actually align the textView and not its children. You are going to want to use the method setLayoutParams() see docs. I am not sure what you are trying to do with the wrapper LayoutView. So the following code might not be everything you’ll need. But it is the recommended way to align your views with a relative layout (pragmatically).

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }
    
        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
    
        OneMessage coment = getItem(position);
    
        countryName = (TextView) row.findViewById(R.id.comment);
    
        countryName.setText(coment.msgText);
    
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        if(!coment.sentbyuser)
        {
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }
        else
        {
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
    
        countryName.setLayoutParams(params);
    
        return row;
    }
    

    If there is a second view that needs to be aligned exactly with the countryName you can use the following:

    View yourOtherView = findViewById(R.id.yourOtherView);
    
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    
    params.addRule(RelativeLayout.ALIGN_BELOW, R.id.comment);
    params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.comment);
    
    yourOtherView.setLayoutParams(params);
    

    That tells it to align it below your comment view and then to right align it. You might need to play around with margins, padding and other values to get it exactly right.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom ListView which contains one TextView (Of numbers) and one EditText
I have one custom ListView which contains one ImageView and one TextView, and I
I have ListView, which contains a TextView and a CheckBox. Now I am able
I have one table in sqlite database which contains five different columns as shown
I have a ListView which contains an ImageView and a TextView. I'm subclassing ArrayAdapter
I have a ListView which contains custom rows. This custom row has following UI
I have a ListView which might contains a lot of items, so it is
I have a Custom ListView which has an ImageView and a TextView. and i
Hallo all, I have a ListView which contains a Button in each line. The
Hallo all, I have a ListView, which contains an EditText in each of it's

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.