title: Android Adding Buttons to Toolbar Programmatically
body: <p>I am trying to create a toolbar programmatically and add left and right bar buttons without using XML.</p>
  <p>But the button is not getting aligned to right.</p>
  <pre><code>Toolbar TopTulBarVar = new Toolbar(this);
  TopTulBarVar.setId(View.generateViewId());
  TopTulBarVar.setBackgroundColor(Color.parseColor(&quot;#DDDDDD&quot;));
  TopTulBarVar.setTitle(&quot;Ttl Txt&quot;);
  TopTulBarVar.setSubtitle(&quot;Dtl Txt&quot;);
  
  Button NamBarBtnVar = new Button(this);
  NamBarBtnVar.setText(&quot;Select&quot;);
  NamBarBtnVar.setBackgroundColor(Color.GREEN);
  LinearLayout.LayoutParams NamBarBtnRulVar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  NamBarBtnRulVar.gravity = Gravity.RIGHT;
  TopTulBarVar.addView(NamBarBtnVar, NamBarBtnRulVar);
  </code></pre>
  <p>Also tried</p>
  <pre><code>RelativeLayout.LayoutParams RloRulVar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      RloRulVar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  </code></pre>
  <p>I am getting as below image</p>
  <p><a href="https://i.stack.imgur.com/IlQGZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IlQGZ.jpg" alt="enter image description here" /></a></p>
  <p>But need like</p>
  <p><a href="https://i.stack.imgur.com/UBrjX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UBrjX.jpg" alt="enter image description here" /></a></p>