Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gravity in programmatically in a relativelayout

Tags:

android

How to set gravity in programmingcally in a relativelayout. I have a XML layout with name chat_viewer_message.xml as below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="4dip"
    android:paddingBottom="4dip"
    android:gravity="left"
    android:background="@drawable/chat_bg_in">

    <ImageView
        android:id="@+id/avatar"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_marginLeft="4dip"
        android:src="@drawable/avatar_1_1"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/avatar"
        android:paddingLeft="4dip"
        />

</RelativeLayout>

And code view in programming is as below:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int type = getItemViewType(position);
        final View view;
        if (convertView == null) {
            final int resource = R.layout.chat_viewer_message;
            view = activity.getLayoutInflater()
                    .inflate(resource, parent, false);
            if (type == TYPE_MESSAGE)
                ((TextView) view.findViewById(R.id.text)).setTextAppearance(
                        activity, appearanceStyle);
        } else
            view = convertView;



        final MessageItem messageItem = (MessageItem) getItem(position);
        String name;
        final String account = messageItem.getChat().getAccount();
        final String user = messageItem.getChat().getUser();
        final String resource = messageItem.getResource();
        final boolean incoming = messageItem.isIncoming();
        final String server_host = view.getResources().getString(
                R.string.server_host);
        if (isMUC) {
            name = resource;
        } 


        if (incoming) {
            view.setBackgroundResource(R.drawable.chat_bg_in);
        } else {
            // I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS
            view.setBackgroundResource(R.drawable.chat_bg_out);
        }

        return view;
}

See gravity at the relativelayout with id = background, the default gravity is LEFT, So I want to if !incoming the value gravity at relativelayout be RIGHT instead LEFT.

Thanks.

like image 292
Loren Ramly Avatar asked Sep 02 '13 08:09

Loren Ramly


People also ask

How do I use RelativeLayout code in Android?

Android RelativeLayout enables you to specify how child views are positioned relative to each other. The position of each view can be specified as relative to sibling elements or relative to the parent.

Which attribute or property is specific for RelativeLayout?

Positioning Views RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.


2 Answers

Cast view to container layout, in this case RelativeLayout, and use setGravity(int) on it:

if (incoming) {
    view.setBackgroundResource(R.drawable.chat_bg_in);
} else {
    // I WANT TO GRAVITY TO RIGHT. HOW TO CODE? THANKS
    view.setBackgroundResource(R.drawable.chat_bg_out);

    ((RelativeLayout) view).setGravity(Gravity.RIGHT);
}

A word of caution (from the docs):

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

like image 93
Vikram Avatar answered Oct 02 '22 10:10

Vikram


There are two main ways to programmatically set the gravity in a RelativeLayout. You can either set the gravity for all child views (setGravity(int)) or set the gravity individually (params.addRule(int)). Note: These methods are not available for LinearLayouts.

To set the gravity for all children:

RelativeLayout relativeLayout = findViewById(R.id.myRelativeLaout);
relativeLayout.setGravity(Gravity.CENTER);

To set the gravity for a single view within a RelativeLayout:

MyView myView = findViewById(R.id.myView);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
myView.setLayoutParams(params);

Sources:

  • RelativeLayout.setGravity(int)
  • RelativeLayout.LayoutParams.addRule(int)
  • RelativeLayout.LayoutParams.removeRule(int)
like image 40
Anonsage Avatar answered Oct 02 '22 11:10

Anonsage