Kar
Kar

Reputation: 6365

How is the title bar hidden?

I'm looking at the demo app of MaterialDesignLibrary.

In its manifest, it uses the AppTheme

<application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">

The AppTheme is simply Theme.Light

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

However, if I use the same theme in my app, I get a title bar but not in theirs (both using the layout previewer and the emulator). Does anyone know how they manage to hide the title bar? I know how hiding can be achieved, but I'm trying to follow their approach.

Here's what the previewer shows when the demo app is loaded

enter image description here

There's no action bar in the layout previewer for their app, but there is for mine - even with Window.FEATURE_NO_TITLE set. Here's mine:

enter image description here

Thanks

EDIT Here's my code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="148dp" >

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="128dp"
                android:background="#01579b"
                android:paddingBottom="20dp"
                android:paddingLeft="104dp" android:id="@+id/">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:text="MaterialDesign"
                    android:textColor="#FFF"
                    android:textSize="25dp" />
        </RelativeLayout>

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_marginTop="128dp"
                android:background="#88CECECE" />

        <com.gc.materialdesign.views.ButtonFloatSmall
                android:id="@+id/buttonColorSelector"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp"
                android:layout_marginTop="108dp"
                android:background="#1E88E5"
                materialdesign:iconDrawable="@drawable/icn_select_color" />
    </RelativeLayout>

</RelativeLayout>

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tagapp.tagapp" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.grade:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

Module build.grade:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.1'
    }
}
apply plugin: 'com.android.application'

repositories {
    jcenter()
}

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.tagapp.tagapp"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Upvotes: 0

Views: 186

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74098

When you look at their code in MainActivity.java, right at the beginning of onCreate() they use Window.FEATURE_NO_TITLE

protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    /* ... */

So, no magic here.


I finally came around testing this on my system. Removing the line

    requestWindowFeature(Window.FEATURE_NO_TITLE);

shows the title bar again. Of course, the other activities hide the title bar, because each of them (Buttons, Progress, Switch and Widget) have the same code in onCreate(). Removing the line there as well has the same effect, showing the title bar!


I cannot comment on some previewer, because I never heard of that before.

Upvotes: 1

Gaston Flores
Gaston Flores

Reputation: 2467

Maybe this code will be useful, try put this in your activity (onCreate method):

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

ActionBar actionBar = getSupportActionBar();

if(actionBar != null) {
    actionBar.hide();
}

Upvotes: 0

Armin Ghoreishi
Armin Ghoreishi

Reputation: 58

You should change the parent parameter to Theme.AppCompat.Light.NoActionBar to hide the action bar in your application.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

Upvotes: 0

Related Questions