title: Android - Set Layout_Gravity programmatically for LinearLayout
body: <p>I've got the following problem: I implemented a <code>HorizontalScrollView</code> which contains in one case a <code>LinearLayout</code> and an <code>ImageView</code>. In this case the image is about 50% of the screen width. So I want to center it. Unfortunately the only way I found to center it is, to use <code>layout_gravity="center"</code> on the <code>LinearLayout</code>.</p>
  
  <p>Here's my xml:</p>
  
  <pre><code>&lt;HorizontalScrollView
              android:layout_gravity="center"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"&gt;
  
          &lt;LinearLayout
                  android:orientation="horizontal"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"&gt;
  
          &lt;ImageView                    
                  android:src="@drawable/myImg"
                  android:layout_width="wrap_content"
                  android:adjustViewBounds="true"
                  android:layout_height="150dp"/&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;/HorizontalScrollView&gt;
  </code></pre>
  
  <p>But I need to set the <code>layout_gravity</code> programmatically. Does anybody has an idea how I can achieve this or knows a different way? All the things I found via Google are not working for me like <a href="https://stackoverflow.com/q/8049620/1331671">this post</a>.</p>
  
  <p>Thanks!</p>