How to set gravity to layout programmatically?
Asked Answered
F

3

1

I am creating a chat application in which i need to align the chat messages accordingly.I need to align those from the user to right side and others to the left side.I have a listview in which the messages are displayed.For now,i am using bundled json for getting data.Here is my code

This is for appending a string to the message typed by the user which is checked and splitted inorder to recognize messages from the user.

final EditText et = (EditText)findViewById(R.id.EditText1);
final Button imb=(Button)findViewById(R.id.btn_send);
            imb.setOnClickListener(new OnClickListener()
            {
             @Override
             public void onClick(View arg0) 
             { 
                  String str = et.getText().toString()+":aeiou";
                  web.add(str);
                  scrollMyListViewToBottom();
                  et.setText(" ");
                  adapter.notifyDataSetChanged();


             }

After that, i am splitting the string and stored in an array.If the message is from user.ie,if it has the appended string,then the gravity of the layout need to be set to RIGHT,else LEFT. I used this code in my adapter class for this

    mychat=web.get(position);
if(mychat.contains(":"))
{
    String[] splitted = mychat.split(":");
    String chats;

    if(splitted[1].equalsIgnoreCase("aeiou"))
    {

       chats=splitted[0];
       Toast.makeText(context, "You entered...."+chats, Toast.LENGTH_SHORT).show();
       LinearLayout my_layout = (LinearLayout) rowView.findViewById(R.id.main_layout);

       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
       params.gravity = Gravity.RIGHT;
       my_layout.setLayoutParams(params);


      //my_layout.setGravity(Gravity.RIGHT);

      txtTitle.setText(chats);
    }   
}
else
    {
    txtTitle.setText(web.get(position));
    }




txtTitle.setBackgroundResource(R.drawable.test);

What is wrong in this ?Please help...

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2a2a34"
    android:padding="3dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <RelativeLayout
        android:id="@+id/chat_icon_layout"
            android:layout_width="match_parent"
            android:layout_weight="4"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/img"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/white" />

            <ImageView
                android:id="@+id/img1"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/abs__ab_bottom_solid_light_holo" />

            </RelativeLayout>

        <LinearLayout
            android:id="@+id/chat_text_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:layout_weight="1" >

                    <TextView
                     android:id="@+id/txt"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     android:text="Message"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="#ffffff" />

            </LinearLayout>


    </LinearLayout>

</LinearLayout>
Flossie answered 22/4, 2014 at 5:34 Comment(0)
A
3

since you want to align chat messages to left and right therefore you need to align your text messages i.e your text view. therefore you need to do

if(...)
  txtTitle.setText(chats);
txtTitle.setGravity(Gravity.RIGHT);
    }   
}
else
    {
    txtTitle.setText(web.get(position));
txtTitle.setGravity(Gravity.LEFT);
    }
Anderton answered 22/4, 2014 at 5:43 Comment(4)
what hmmmmmmmmmmmmmmmmAnderton
check how many times i pinged u idiot :/ i thought like u went to heaven in earthquake :/ :/Flossie
i went to lahore...after eid.. and came back on saturday...there were problem in internet so..thats why i didnt reply u.Anderton
acha na ab..bs karo..:PAnderton
C
1

In your adapter, you should be setting the other case in else condition as well. Example is below.

I saw you are using wrap_content for your width. So your LinearLayout will wrap your content and can't align to left or right of your layout. You should use fill parent for your LinearLayout in order it to align it's contents if there is sufficient space

if(splitted[1].equalsIgnoreCase("aeiou"))
        {

           chats=splitted[0];
           Toast.makeText(context, "You entered...."+chats, Toast.LENGTH_SHORT).show();
           LinearLayout my_layout = (LinearLayout) rowView.findViewById(R.id.main_layout);

           LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
           params.gravity = Gravity.RIGHT;
           my_layout.setLayoutParams(params);

          //my_layout.setGravity(Gravity.RIGHT);

          txtTitle.setText(chats);
        }   
    }
    else{
         LinearLayout my_layout = (LinearLayout)rowView.findViewById(R.id.main_layout);    
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
              LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         params.gravity = Gravity.LEFT;
         my_layout.setLayoutParams(params);
         txtTitle.setText(web.get(position));
    }
Cheeseparing answered 22/4, 2014 at 5:39 Comment(3)
I am setting the Layout gravity of the whole layout which contains the textview and also an image.I want that image also to be aligned left.Is that wrong ?Flossie
if your intent was that, no it is not :)Cheeseparing
use fill_parent for your linearlayout. I updated my answer please check it and ask if you need more help.Cheeseparing
E
0

The problem is you are setting the layout gravity right.. try setting the textview gravity left or right..

just add the property in the xml android:gravity="left" or android:gravity="right"

Emersonemery answered 22/4, 2014 at 5:37 Comment(3)
I want that to be done programmatically because i need to set Left and Right gravity according to whether the message is from user or others.Flossie
ok ,try this labelTV.setGravity(Gravity.RIGHT| Gravity.BOTTOM);Emersonemery
use this in if else statement if input incoming gravity left if outgoing gravity right.. here labeltv is your textviewEmersonemery

© 2022 - 2024 — McMap. All rights reserved.