Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align ImageView to the right side of the LinearLayout Programmatically android

Tags:

android

i want to align an ImageView to the right side of the LinearLayout Programmatically.How can i do this.

like image 805
androiduser Avatar asked Jan 06 '12 20:01

androiduser


2 Answers

You don't. That is not what a linear layout does. Use a RelativeLayout instead.

XML

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

Programmatically

public void makeView(Context context) {
    RelativeLayout rl = new RelativeLayout(context);
    TextView tv = new TextView(context);
    tv.setText("Hello, World");

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    rl.addView(tv, lp);
}
like image 133
ahodder Avatar answered Oct 31 '22 21:10

ahodder


You can try this:

ImageView view = (ImageView)findViewById(R.id.imageView);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.LEFT;

But you should use RelativeLayout instead of LinearLayout.

like image 26
Botond Kopacz Avatar answered Oct 31 '22 23:10

Botond Kopacz