Joseph
Joseph

Reputation: 420

How to align Button in a custom ToolBar to Right

I have made a custom ToolBar but recently decided to include a button on it as a home button. I have also defined it in java and it works when a button is clicked but the ToolBar title text shows half-way, i have tried to alignParentRight but it wouldn't work. Please help me

Below are my codes and what i have tried.

TIps_4.java Activity

public class Tips_4 extends AppCompatActivity {
    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    Button Tips3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the start page passed to us or default to first page
        Intent mIntent = getIntent();
        int startPage = mIntent.getIntExtra("startPage", 0);
        setContentView(R.layout.activity_tips_4);

        toolbar= findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        Button toolbar_btn = (Button)findViewById(R.id.toolbarButton);
        toolbar_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toolbarIntent = new Intent(Tips_4.this, MainActivity.class);
                toolbarIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Tips_4.this.startActivity(toolbarIntent);
                setSupportActionBar(toolbar);
            }
        });



        ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new SimpleFragmentPageAdapter(getSupportFragmentManager(),this));
        // set the current page to the start page
        viewPager.setCurrentItem(startPage);

        //Attach the page change listener inside the activity
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            // This method will be invoked when a new page becomes selected
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(Tips_4.this, "Selected Maths Page:" + position, Toast.LENGTH_SHORT).show();
            }
            // This method will be invoked when the current page is scrolled
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetpixels) {

            }
            // Called when the scroll state changes:
            //SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        tabLayout= (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);


    }



}

toolbar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <Button
        android:id="@+id/toolbarButton"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:foreground="@drawable/ic_home_24dp"
        android:layout_marginLeft="300dp"
        android:background="#000"/>

</androidx.appcompat.widget.Toolbar>

This is what i did that refused to work

 <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:orientation="horizontal"
        android:layout_alignParentRight="true">


        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="54dp"
            android:layout_height="50dp"
            android:background="#056359"
            android:layout_gravity="end"
            android:padding="11dp"
            android:foreground="@drawable/ic_home_24dp" />

    </RelativeLayout>

This is the screenshot

enter image description here

Upvotes: 1

Views: 2110

Answers (2)

Rajnish Sharma
Rajnish Sharma

Reputation: 388

Here is the complete code for a toolbar with button at the end

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mathematics Pages"
            android:textSize="23sp"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:gravity="right"/>

        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:foreground="@drawable/ic_home_24dp"
            android:layout_alignParentEnd="true"
            android:background="#000"
            android:layout_alignParentRight="true" />

    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

Upvotes: 1

CodeRED Innovations
CodeRED Innovations

Reputation: 298

You can use a relative layout and set its width and height to match_parent now add the button in you're relative layout and set alignParentEnd to true

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:subtitleTextColor="#FF9800"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/toolbarButton"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:foreground="@drawable/ic_home"
            android:layout_alignParentEnd="true"
            android:background="#000"/>

    </RelativeLayout>
</androidx.appcompat.widget.Toolbar>

If you just want a icon in you're button you can use a ImageButton that works same as a normal button. And If you're icon is of fixed size say 24 X 24 then you can make the button as wrap_content in both width and height.

Upvotes: 0

Related Questions