How to add Toolbar in Activity without inheriting Appcompatactivity - Android Studio 1.4

Asked

Viewed 1,127 times

0

Good night.

I updated my Android Studio for version 1.4 and by default, any activity used as an inheritance to AppCompatActivity (public class MainActivity extends AppCompatActivity). I developed an app that needs to inherit a class responsible for controlling the life cycle of each activity as in the following example: public class MainActivity extends LifeCycleActivity. The problem is that in doing so, the name of the project does not appear on toolbar when running the app. Like updating Android Studiobrought two layouts (content_main.xml and activity_main.xml) for each activity, I wonder if I solve the problem of displaying the project name in the toolbar through the layout files themselves, or through code, since I am not using as inheritance the AppCompatActivity.

  • Do LifeCycleActivity inherit from AppCompatActivity.

3 answers

0

When we create Actionbar without Appcompatactivity You need to set the Actionbar layout in your xml. Place the code below as the first element of your layout.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    />

Then put in the respective Activity (in the onCreate method) the following:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTittle("titulo");

I hope I’ve helped

-1

Just set up the toobar title.

Toolbar toobar  = (Toolbar)findViewById(R.id.toobar);
setSupportActionBar(toolbar);
toolbar.setTitle("seu título");
  • 1

    The problem is that without extend in Appcompatactivity, the code returns error in 'setSupportActionBar(Toolbar);'.

  • Would not be setActionBar(toolbar);

-1


I followed the help I received and the solution was solved by making the class LifeCycleActivity inherit from AppCompatActivity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.