Programmatically set ImageButton layout_gravity

dumbfingers picture dumbfingers · Apr 9, 2013 · Viewed 13.7k times · Source

What I Tried To Do

I tried to set my ImageButton's layout_gravity via Java code, the way I want the ImageButton to be is presented like the way within the Orange frame in the image below:

image

The Blue frame is a vertical LinearLayout act as a "base" layout and this is the layout I tried to add child layouts to.

The Orange and Red are both horizontal LinearLayout.

The Orange layout is the way I want to put the ImageButton and the TextView, this one is set up via XML

The Red layout is the result I tried to mimic the Orange layout via Java code.

The Related Code

Here's the XML code that set up the Orange layout, this is the effect I want to achieve via Java code:

        <!-- Begin the Orange Layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/info_left_padding"
            android:layout_marginRight="@dimen/info_right_padding" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:minHeight="@dimen/detail_min_line_item_height"
                android:text="TextView" />

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

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical"
                    android:adjustViewBounds="true"
                    android:maxHeight="@dimen/abs__action_bar_default_height"
                    android:scaleType="fitCenter"
                    android:src="@drawable/navigation_cancel" />
            </FrameLayout>
        </LinearLayout>

Here's the Java code that set up the Red layout

        int textHeight = (int)getResources().getDimension(R.dimen.detail_min_line_item_height);
        int imgHeight = (int)getResources().getDimension(R.dimen.abs__action_bar_default_height);

        TextView mTextView = new TextView(this);
        ImageButton mDeleteButton = new ImageButton(this);

        // Set Delete Button Padding
//      mDeleteButton.setPadding(buttonPadding, buttonPadding, buttonPadding, buttonPadding);

        // Set Imagebutton Scale type as fitCentre
        mDeleteButton.setScaleType(ScaleType.FIT_CENTER);

        // Set AdjustViewBounds
        mDeleteButton.setAdjustViewBounds(true);

        // Set max height of the image
        mDeleteButton.setMaxHeight(imgHeight);

        // Set the text appearance to be "large"
        mTextView.setTextAppearance(this, android.R.style.TextAppearance_Large);
        mTextView.setText(text);

        // Set the minimum height of this textview
        mTextView.setMinHeight(textHeight);

        // Set the content of the textview to be centred
        mTextView.setGravity(Gravity.CENTER_VERTICAL);

        // Set the ImageButton's background image
        mDeleteButton.setImageResource(R.drawable.navigation_cancel);

        LinearLayout.LayoutParams hParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        LinearLayout hLayout = new LinearLayout(this);
        // Set Margins
        hParams.leftMargin = (int) getResources().getDimension(R.dimen.info_left_padding);
        hParams.rightMargin = (int) getResources().getDimension(R.dimen.info_right_padding);
        hParams.bottomMargin = (int) getResources().getDimension(R.dimen.text_layout_margin);

        hLayout.setLayoutParams(hParams);
        // Set orientation to horizontal
        hLayout.setOrientation(LinearLayout.HORIZONTAL);

        // The settings below is actually setting up some of the button's parameters        
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParams.gravity = Gravity.RIGHT;
        mDeleteButton.setLayoutParams(buttonParams);

        hLayout.addView(mTextView);
        hLayout.addView(mDeleteButton);

        layout_blue.addView(hLayout);

What I've Tried So Far

According to some SO post like this: Java method for android:layout_gravity I initially tried to first put my ImageButton into a FrameLayout then set the params of this FrameLayout, like this:

FrameLayout buttonFrame = new FrameLayout(this);
FrameLayout.LayoutParams buttonParams = new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.WRAP_CONTENT, 
                                                                            android.widget.FrameLayout.LayoutParams.WRAP_CONTENT, 
                                                                            Gravity.RIGHT | Gravity.CENTER_VERTICAL);
buttonFrame.setLayoutParams(buttonParams);
buttonFrame.addView(mDeleteButton);

But I had the same result as the image presented above. Later I also tried to change the LayoutParams width to MATCH_PARENTonly to find theImageButton` was stretched horizontally (Yes it's stretched)

Then I tried the method posted in these two SO posts:

How to set layout_gravity programmatically?

How to set a button's parameters programatically

Their method is to set up a LinearLayout.Params first, then apply this params to the button (The Code I posted in The Related Code section applies this method). In short it is:

    // The settings below is actually setting up some of the button's parameters        
    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buttonParams.gravity = Gravity.RIGHT;
    mDeleteButton.setLayoutParams(buttonParams);

However, the result was still the same as the image presented above.

Question

Since I need to programmatically add more child views to the Blue layout later, I wonder if there's a way to set up each child layout like the Orange one in the image?

At Last

I found a solution which is quite similar to @Permita 's answer.

Here's my solution:

    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
    textParams.weight = 1.0f;
    mTextView.setLayoutParams(textParams);

Answer

Permita picture Permita · Apr 10, 2013

Add the below code, it will assign all the available space to the texView, shifting the button to right side of the layout to make it appear like the orangeLayout.

mTextView.setLayoutParams(LayoutParams param = new LinearLayout.LayoutParams(
                         LayoutParams.MATCH_PARENT,
                         LayoutParams.MATCH_PARENT, 1.0f));