-1

Hi I am implementing a toolbar, and for this I have the following code:

Activity

public class SimulationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simulation);
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromAsset(SIMULATION_PDF).load();

        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                launchMapActivity(this);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".simulation.SimulationActivity">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/lato_black"
                android:text="@string/app_name"
                android:textColor="@color/red"
                android:textSize="26sp" />


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

    ...
</android.support.constraint.ConstraintLayout>

Problem

If I add the TextView inside the Toolbar then the arrow's toolbar not show. Anyway, when I do click on the arrow place (it is hidden), work perfectly.

Target

Show the text view and the arrow in the toolbar.

Any idea?

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84

2 Answers2

1

Remove TextView child of Toolbar & for setting title of toolbar use getSupportActionBar().setTitle().

if (getSupportActionBar() != null)
    getSupportActionBar().setTitle("My title");
  • For changing Toolbar textColor, call setTitleTextColor
toolbar.setTitleTextColor(getResources().getColor(R.color.colorName));
  • For chaging font-face add a style in styles.xml & set android:theme="@style/yourStyle" Add following in Styles.xml
<style name="yourStyle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:fontFamily">@font/yourFont</item>
</style>
Tirth Patel
  • 5,443
  • 3
  • 27
  • 39
0

Finally, the problem was in the AppBarLayout in the line:

android:theme="@style/AppTheme.AppBarOverlay"

Now the code is the following:

Activity

public class SimulationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simulation);
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromAsset(SIMULATION_PDF).load();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                launchMapActivity(this);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

Layout

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:popupTheme="@style/AppTheme.AppBarOverlay">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:fontFamily="@font/lato_black"
                android:text="@string/app_name"
                android:textColor="@color/red"
                android:textSize="26sp"/>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84