9

as i'm new to android i set back button in my toolbar, i want to reduce its padding/margin from left how can i do that?? thanks in adwance.

this is my design code

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/tripidtoolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="name"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/dayNotoolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textColor="#ffffff"
android:layout_marginRight="10dp"
android:text="day"
android:textAllCaps="true"
android:textSize="18sp"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>

this is how i manually set back button

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vouchers_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.arrowbackwhite);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
setSupportActionBar(toolbar);

this is my screen (back button having left margin)

Ajay Mistry
  • 951
  • 1
  • 14
  • 30
  • 1
    Possible duplicate of [Android: remove left margin from actionbar's custom layout](http://stackoverflow.com/questions/27354812/android-remove-left-margin-from-actionbars-custom-layout) – Evgeniy Mishustin Apr 21 '16 at 10:47
  • @EvgeniyMishustin: i try that question but still not solved my problem.. – Ajay Mistry Sep 01 '16 at 07:29

4 Answers4

21

add these line into your toolbar xml

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
Abdul Rizwan
  • 3,904
  • 32
  • 31
2

Set the style to toolbar:

<!-- Tool Bar-->
    <style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
        <item name="android:paddingLeft">0dp</item>
    </style>

And in on Create method of activity do this,

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

Put this in the toolbar xml

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
fsebek
  • 389
  • 2
  • 9
  • 4
    ..Thanks for Replying....it looks work in Xml Editor .but when i run app in real device there is no change in it. it looks as it is.. do you know why it happans?? – Ajay Mistry May 03 '16 at 07:25
-3

You could try this:

in onCreate:

actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);

Then add this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Add this in the manifest activity you want the backbutton in:

android:parentActivityName=".homeActivity"

This will create the back button for you.

Jikiwiki
  • 115
  • 1
  • 8