Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set Toolbar in an Activity

I am trying to set a ToolBar to an activity I have created.

Here is my the tool bar layout, app_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#000">

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

Here is my activity layout, home_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/com_facebook_button_send_background_color"
    android:weightSum="1">

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

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

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/docs_button"
        android:layout_weight="0.17"
        android:layout_gravity="center"
        android:background="@drawable/docs"/>

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/music_button"
        android:layout_weight="0.17"
        android:layout_gravity="center"    
        android:background="@drawable/music2"
        android:textStyle="bold"
        android:textSize="40sp"
        />    
    </LinearLayout>    
</RelativeLayout>

However, when I try to add the toolBar to the activity I get an error:

public class HomeActivity extends AppCompatActivity  implements View.OnClickListener{

    private static final String TAG = HomeActivity.class.getSimpleName();

    Toolbar toolbar;
    private Button docsButton;
    private Button musicButton;

    @Override
    protected void onCreate(Bundle savedInstanceSate) {

        Log.d(TAG ,"OnCreate() - Ini");
        super.onCreate(savedInstanceSate);

        setContentView(R.layout.home_layout);
        toolbar= (Toolbar) findViewById(R.id.app_bar);

            setSupportActionBar(toolbar);

The error I get is in the line : setSupportActionBar(toolbar); and it says:

setSupportActionBar
(android.support.v7.widget.Toolbar)
in AppCompatActivity cannot be applied
to
(android.widget.Toolbar)

I don't know why I get this error knowing that my toolBar is clearly: android.support.v7.widget.Toolbar as you can see in its layout file.

like image 868
skylight Avatar asked Dec 15 '22 03:12

skylight


2 Answers

Change your toolBar declaration to android.support.v7.widget.Toolbar toolbar; instead of Toolbar toolbar;

and the line toolbar= (Toolbar) findViewById(R.id.app_bar); to:

toolbar= (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
like image 169
rainman Avatar answered Dec 30 '22 06:12

rainman


Delete import widget.app.toolbar and then import android.support.v7.widget.Toolbar,it should work fine.:-)

like image 34
Manasvi Avatar answered Dec 30 '22 07:12

Manasvi