Aspicas
Aspicas

Reputation: 4497

Material design toolbar on devices pre-lollipop

I'm flollowing next tutorial:

I want do that similar design:

md

I know i need use Two toolbars.

values/themes.xml

<resources>
<style name="Theme.MiThema" parent="Theme.AppCompat.Light">
    <!-- Here we setting appcompat’s actionBarStyle -->
    <!--<item name="actionBarStyle">@style/MyActionBarStyle</item>-->


    <!-- ...and here we setting appcompat’s color theming attrs -->
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>
</style>
    </resources>

class.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toolbar;

public class RegistrarInciTraActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registrar_inci_traf_layout);

        //ocultamos ActionBar
        getSupportActionBar().hide();


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

layout

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- drawer view -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start">

            <!-- drawer content -->

        </LinearLayout>

        <!-- normal content view -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- The rest of content view -->

        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

Tha code report one error on "setSupportActionBar(toolbar);":

md2

(first problem) ¿What is the problem about that? (SOLVED)

(Second problem)

On my layout appear

"?attr/actionBarSize" and "?attr/colorPrimary"

Where do I have to declare those attributes?

(Third problem)

When I do two toolbars only appear once of them and I want one inside the other to display content.

md3

What I do have to display both toolbars?

Thanks

Upvotes: 0

Views: 1712

Answers (2)

aB9
aB9

Reputation: 592

As 1st Problem is solved, let me try Second one,

First of all, you do not have to define these values anywhere, they are pre-defined.

  1. "?attr/actionBarSize" : this is a default value provided by Android. You will get it at android.support.v7.appcompat/R,but it will give you an int reference. If you want to know the actual height of the Actionbar, you have to resolve the attribute actionBarSize at runtime. Follow this Link : Get ActionBar Size(answer given on other SO Q.). Also Now with Material Design, Google shared some standards to be used while designing new Apps (or upgrading old one) in Material Design. You can follow those norms here : LayoutMetrics & keylines

  2. "?attr/colorPrimary" : This is also a pre-defined value. You can get it at android.support.v7.appcompat/R, but it will give you an int reference. But if you want a custom background color to your ToolBar then just add an appropriate color palette, android:background="@color/colorPrimary" and define colorPrimary value in color.xml. Use this site to get different color values : Material Palette (I prefer this site).

Upvotes: 1

Tomate_Salat
Tomate_Salat

Reputation: 78

I can give you an answer for your first problem. It is very simple - you only have to read the message given by your IDE. You're using the wrong toolbar. The toolbar you're trying to pass to the setSupportActionBar-Method is from the package android.widget. But you've to use the toolbar from the package android.support.v7.widget. So your imports are wrong - correct them and your code should work.

Upvotes: 2

Related Questions