10

I am trying to create a toolbar programmatically and add left and right bar buttons without using XML.

But the button is not getting aligned to right.

Toolbar TopTulBarVar = new Toolbar(this);
TopTulBarVar.setId(View.generateViewId());
TopTulBarVar.setBackgroundColor(Color.parseColor("#DDDDDD"));
TopTulBarVar.setTitle("Ttl Txt");
TopTulBarVar.setSubtitle("Dtl Txt");

Button NamBarBtnVar = new Button(this);
NamBarBtnVar.setText("Select");
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);

Also tried

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

I am getting as below image

enter image description here

But need like

enter image description here

2
  • can you add some screenshot for better understanding Commented May 27, 2017 at 17:05
  • Added Screenshots... Commented May 27, 2017 at 17:17

3 Answers 3

22

In Activity code

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar t = (Toolbar) findViewById(R.id.tool);
    setSupportActionBar(t);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    //left side button

    Button b = new Button(this);
    Toolbar.LayoutParams l1=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l1.gravity = Gravity.START;
    b.setLayoutParams(l1);
    b.setText("left");
    t.addView(b);

    //center Textview
    TextView text=new TextView(this);
    text.setText("Text:1");
    Toolbar.LayoutParams l2 = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l2.gravity = Gravity.CENTER;
    text.setLayoutParams(l2);
    t.addView(text);

    //Right side button

    Button b1=new Button(this);
    b1.setText("Right");
    Toolbar.LayoutParams l3=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l3.gravity=Gravity.END;
    b1.setLayoutParams(l3);
    t.addView(b1);

}

Toolbar XML code

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:id="@+id/tool"
app:contentInsetLeft="0dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:layout_height="wrap_content">

</android.support.v7.widget.Toolbar>

OUTPUT enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Can u modify so we build Toolbar also in Java code instead of XML. I dont want anything in XML as I want to reuse this code again and again.
Also consider voting my question too
Looks like no way of adding toolbar dynamically I tried but didn't workout (i will edit my answer in future if I found a way)
This can also be used with MaterialButton class buttons.
1

For anyone struggling with this nowadays: please make sure to use androidx.appcompat.widget.Toolbar.LayoutParams while adding view programmatically as for me just typing Toolbar.LayoutParams were accidentally resolved to android.widget.Toolbar.LayoutParams by Android Studio. I spent some time trying to figure out why gravity is not changed: no exception occures if set wrond layoutParams before adding a view to the toolbar. I have androidx.appcompat.widget.Toolbar in my layout.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-4

for me I added one switch button in Toolbar and it does work :

            <Toolbar
                    android:id="@+id/toolbar"
                    style="@style/Toolbar"
                    android:visibility="visible"
                    android:background="@color/white">

                <Switch
                        android:id="@+id/btn_accessible"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:visibility="visible"
                        android:paddingLeft="25sp"
                        android:paddingRight="25sp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:text=""
                        android:textColor="@color/white"
                        android:textSize="12sp"
                        android:thumb="@drawable/custom_switch_thumb"
                        android:track="@drawable/custom_switch_track"

                />


            </Toolbar>

and in activity i just set onClick listener

1 Comment

this is exactly the thing that the OP specifically said he did NOT want to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.