How can I center(Horizontally) an icon in a toolbar menu












-1















How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.
How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.



This is the code for the, please let me know if I need to upload more source code.
enter image description here
main.xml



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:id="@+id/item1"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>

</menu>


menu_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/colappsingtoolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:contentScrim="?attr/colorPrimary"
app:title=" "
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainimg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />









share|improve this question

























  • Center in which direction – horizontally or vertically?

    – Mike M.
    Nov 20 '18 at 23:24











  • @MikeM. Horinzontally

    – Pedro
    Nov 20 '18 at 23:24











  • You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

    – Mike M.
    Nov 20 '18 at 23:27













  • Possible duplicate of How to Control Menu Position in Toolbar Android

    – dglozano
    Nov 20 '18 at 23:29






  • 1





    Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

    – Mike M.
    Nov 21 '18 at 0:11
















-1















How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.
How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.



This is the code for the, please let me know if I need to upload more source code.
enter image description here
main.xml



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:id="@+id/item1"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>

</menu>


menu_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/colappsingtoolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:contentScrim="?attr/colorPrimary"
app:title=" "
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainimg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />









share|improve this question

























  • Center in which direction – horizontally or vertically?

    – Mike M.
    Nov 20 '18 at 23:24











  • @MikeM. Horinzontally

    – Pedro
    Nov 20 '18 at 23:24











  • You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

    – Mike M.
    Nov 20 '18 at 23:27













  • Possible duplicate of How to Control Menu Position in Toolbar Android

    – dglozano
    Nov 20 '18 at 23:29






  • 1





    Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

    – Mike M.
    Nov 21 '18 at 0:11














-1












-1








-1








How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.
How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.



This is the code for the, please let me know if I need to upload more source code.
enter image description here
main.xml



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:id="@+id/item1"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>

</menu>


menu_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/colappsingtoolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:contentScrim="?attr/colorPrimary"
app:title=" "
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainimg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />









share|improve this question
















How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.
How can I center an icon in a toolbar menu, I tried adding padding or margin but is not working.



This is the code for the, please let me know if I need to upload more source code.
enter image description here
main.xml



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:id="@+id/item1"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_credit_card_black_24dp"
android:title=" "
app:showAsAction="ifRoom"/>

</menu>


menu_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">



<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/colappsingtoolbar"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:contentScrim="?attr/colorPrimary"
app:title=" "
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp" >
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainimg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />






android android-studio android-layout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:39







Pedro

















asked Nov 20 '18 at 23:21









Pedro Pedro

314214




314214













  • Center in which direction – horizontally or vertically?

    – Mike M.
    Nov 20 '18 at 23:24











  • @MikeM. Horinzontally

    – Pedro
    Nov 20 '18 at 23:24











  • You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

    – Mike M.
    Nov 20 '18 at 23:27













  • Possible duplicate of How to Control Menu Position in Toolbar Android

    – dglozano
    Nov 20 '18 at 23:29






  • 1





    Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

    – Mike M.
    Nov 21 '18 at 0:11



















  • Center in which direction – horizontally or vertically?

    – Mike M.
    Nov 20 '18 at 23:24











  • @MikeM. Horinzontally

    – Pedro
    Nov 20 '18 at 23:24











  • You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

    – Mike M.
    Nov 20 '18 at 23:27













  • Possible duplicate of How to Control Menu Position in Toolbar Android

    – dglozano
    Nov 20 '18 at 23:29






  • 1





    Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

    – Mike M.
    Nov 21 '18 at 0:11

















Center in which direction – horizontally or vertically?

– Mike M.
Nov 20 '18 at 23:24





Center in which direction – horizontally or vertically?

– Mike M.
Nov 20 '18 at 23:24













@MikeM. Horinzontally

– Pedro
Nov 20 '18 at 23:24





@MikeM. Horinzontally

– Pedro
Nov 20 '18 at 23:24













You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

– Mike M.
Nov 20 '18 at 23:27







You won't be able to do that with a menu item without some rather hacky approach. Just use your own <Toolbar> in your layout (if you're not already) and put an <ImageButton> in it with layout_gravity="center".

– Mike M.
Nov 20 '18 at 23:27















Possible duplicate of How to Control Menu Position in Toolbar Android

– dglozano
Nov 20 '18 at 23:29





Possible duplicate of How to Control Menu Position in Toolbar Android

– dglozano
Nov 20 '18 at 23:29




1




1





Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

– Mike M.
Nov 21 '18 at 0:11





Hmm, well, the CollapsingToolbarLayout still works for me, but when the Toolbar is inside one, the ImageButton is no longer centered. Very odd. I'm still trying to figure out why.

– Mike M.
Nov 21 '18 at 0:11












2 Answers
2






active

oldest

votes


















1














I dont know if exist an easy way, but you could create CustomToolbar. For example, create a xml layout with name: toolbar



<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/tool_bar_size_mio"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="10">
<Button
android:id="@+id/btn_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"/>
</LinearLayout>




And then, include to your activitys xml



<include
android:id="@+id/toolbar"
layout="@layout/toolbar_menu_principal" />





share|improve this answer



















  • 1





    This won't work. The remaining menu item on the right will cause the Button to be left of center.

    – Mike M.
    Nov 20 '18 at 23:39



















0














I don't know if u can do this but maybe try to apply text align center to parent item.
And I saw this in google it will maybe help:
android:layout_centerHorizontal="true"
android:layout_gravity="center"
Maybe it should help.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403105%2fhow-can-i-centerhorizontally-an-icon-in-a-toolbar-menu%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I dont know if exist an easy way, but you could create CustomToolbar. For example, create a xml layout with name: toolbar



    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_size_mio"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">
    <Button
    android:id="@+id/btn_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Button"/>
    </LinearLayout>




    And then, include to your activitys xml



    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_menu_principal" />





    share|improve this answer



















    • 1





      This won't work. The remaining menu item on the right will cause the Button to be left of center.

      – Mike M.
      Nov 20 '18 at 23:39
















    1














    I dont know if exist an easy way, but you could create CustomToolbar. For example, create a xml layout with name: toolbar



    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_size_mio"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">
    <Button
    android:id="@+id/btn_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Button"/>
    </LinearLayout>




    And then, include to your activitys xml



    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_menu_principal" />





    share|improve this answer



















    • 1





      This won't work. The remaining menu item on the right will cause the Button to be left of center.

      – Mike M.
      Nov 20 '18 at 23:39














    1












    1








    1







    I dont know if exist an easy way, but you could create CustomToolbar. For example, create a xml layout with name: toolbar



    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_size_mio"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">
    <Button
    android:id="@+id/btn_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Button"/>
    </LinearLayout>




    And then, include to your activitys xml



    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_menu_principal" />





    share|improve this answer













    I dont know if exist an easy way, but you could create CustomToolbar. For example, create a xml layout with name: toolbar



    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/tool_bar_size_mio"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">
    <Button
    android:id="@+id/btn_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Button"/>
    </LinearLayout>




    And then, include to your activitys xml



    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_menu_principal" />






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 23:37









    Pablo DbSysPablo DbSys

    31828




    31828








    • 1





      This won't work. The remaining menu item on the right will cause the Button to be left of center.

      – Mike M.
      Nov 20 '18 at 23:39














    • 1





      This won't work. The remaining menu item on the right will cause the Button to be left of center.

      – Mike M.
      Nov 20 '18 at 23:39








    1




    1





    This won't work. The remaining menu item on the right will cause the Button to be left of center.

    – Mike M.
    Nov 20 '18 at 23:39





    This won't work. The remaining menu item on the right will cause the Button to be left of center.

    – Mike M.
    Nov 20 '18 at 23:39













    0














    I don't know if u can do this but maybe try to apply text align center to parent item.
    And I saw this in google it will maybe help:
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    Maybe it should help.






    share|improve this answer




























      0














      I don't know if u can do this but maybe try to apply text align center to parent item.
      And I saw this in google it will maybe help:
      android:layout_centerHorizontal="true"
      android:layout_gravity="center"
      Maybe it should help.






      share|improve this answer


























        0












        0








        0







        I don't know if u can do this but maybe try to apply text align center to parent item.
        And I saw this in google it will maybe help:
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        Maybe it should help.






        share|improve this answer













        I don't know if u can do this but maybe try to apply text align center to parent item.
        And I saw this in google it will maybe help:
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        Maybe it should help.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 23:25









        karlkarl

        54




        54






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403105%2fhow-can-i-centerhorizontally-an-icon-in-a-toolbar-menu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?