title: Programmatically set ImageButton layout_gravity
body: <h1>What I Tried To Do</h1>
  
  <p>I tried to set my <code>ImageButton</code>'s <code>layout_gravity</code> via Java code, the way I want the <code>ImageButton</code> to be is presented like the way within the <strong>Orange</strong> frame in the image below:</p>
  
  <p><img src="https://i.stack.imgur.com/sXtEj.png" alt="image"></p>
  
  <p>The <strong>Blue</strong> frame is a vertical <code>LinearLayout</code> act as a "base" layout and this is the layout I tried to add child layouts to.</p>
  
  <p>The <strong>Orange</strong> and <strong>Red</strong> are both horizontal <code>LinearLayout</code>.</p>
  
  <p>The <strong>Orange</strong> layout is the way I want to put the <code>ImageButton</code> and the <code>TextView</code>, this one is set up via XML</p>
  
  <p>The <strong>Red</strong> layout is the result I tried to mimic the <strong>Orange</strong> layout via Java code.</p>
  
  <h1>The Related Code</h1>
  
  <p>Here's the XML code that set up the <strong>Orange</strong> layout, <strong>this is the effect I want to achieve via Java code:</strong></p>
  
  <pre><code>        &lt;!-- Begin the Orange Layout --&gt;
          &lt;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" &gt;
  
              &lt;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" /&gt;
  
              &lt;FrameLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
  
                  &lt;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" /&gt;
              &lt;/FrameLayout&gt;
          &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here's the Java code that set up the <strong>Red</strong> layout</p>
  
  <pre><code>        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);
  </code></pre>
  
  <h1>What I've Tried So Far</h1>
  
  <p>According to some SO post like this: <a href="https://stackoverflow.com/questions/2945315/java-method-for-androidlayout-gravity">Java method for android:layout_gravity</a> I initially tried to first put my <code>ImageButton</code> into a <code>FrameLayout</code> then set the params of this <code>FrameLayout</code>, like this:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>But I had the same result as the image presented above. Later I also tried to change the <code>LayoutParams</code> width to MATCH_PARENT<code>only to find the</code>ImageButton` was stretched horizontally (Yes it's stretched)</p>
  
  <p>Then I tried the method posted in these two SO posts: </p>
  
  <p><a href="https://stackoverflow.com/questions/8049620/how-to-set-layout-gravity-programmaticaly/8049860#8049860">How to set layout_gravity programmatically?</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/8127704/how-to-set-a-buttons-parameters-programatically">How to set a button&#39;s parameters programatically</a></p>
  
  <p>Their method is to set up a <code>LinearLayout.Params</code> first, then apply this params to the button (The Code I posted in <strong>The Related Code</strong> section applies this method). In short it is:</p>
  
  <pre><code>    // 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);
  </code></pre>
  
  <p>However, the result was still the same as the image presented above.</p>
  
  <h1>Question</h1>
  
  <p>Since I need to programmatically add more child views to the <strong>Blue</strong> layout later, I wonder if there's a way to set up each child layout like the <strong>Orange</strong> one in the image?</p>
  
  <p><strong>At Last</strong></p>
  
  <p>I found a solution which is quite similar to @Permita 's answer.</p>
  
  <p>Here's my solution:</p>
  
  <pre><code>    LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
      textParams.weight = 1.0f;
      mTextView.setLayoutParams(textParams);
  </code></pre>