Is it possible to use ActionBar on Android natively?

3

I'm doing some test applications and I've come across something that has caused me a lot of doubts, the Android support libraries. I tried to develop an application that uses ActionBar natively, it works perfectly in the emulator, but when I try on my smartphone, the menu item that has android:showAsAction="never" is not displayed, follow the settings and the code I have.

  

Note: In the emulator the item with android:showAsAction="never" appears within the three dots.

Emulator: Android Lollipop (API 21)
Smartphone: Android KitKat (API 19)

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_1:
                Toast.makeText(this, "Você clicou em procurar.", Toast.LENGTH_LONG).show();
                return true;
            case R.id.action_2:
                Toast.makeText(this, "Você clicou em editar.", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_1"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom"
        android:title="Procurar" />

    <item
        android:id="@+id/action_2"
        android:showAsAction="never"
        android:title="Editar" />
</menu>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "br.com.exemplo.myapplication"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.exemplo.myapplication">

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

    </application>

</manifest>

styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light" />

</resources>

Following this OS link I was able to do with the support library, however I noticed that the size of the .apk of 53.6 KB (unsupported) was to 0.9 MB (supported) and the space used in the smartphone was 72 KB (unsupported) for more than 3 MB (with support). So my doubts are as follows:

  • Can an application use ActionBar without the support library (for devices with API 15 or higher)?
  • When should I really use the support libraries?

Extra

  

The ActionBar APIs were first added in Android 3.0 (API level 11) but are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.

According to this passage above, theoretically should ActionBar be native from API 11 or am I confusing different subjects?

    
asked by anonymous 18.02.2015 / 03:03

2 answers

5

The ActionBar can be used natively, remembering that ActionBar is the entire toolbar, not just the menu.

I think the problem is that a physical menu is present on your device, it's just a kick. In the case of ActionBar of the support library, it uses the ActionBar native. But the difference is who implements the menu organization, which in the case of the Support Library is itself, there may be some difference in the final result.

In the case of the native menu, when it has physical menu the overflow menu (three dots) is hidden, having to press the physical menu for the options appear ( link ). This is not to cause ambiguity (two ways of doing the same thing).

You can use this hack to force the appearance, but it is not recommended as it is good to remain consistent with other applications.

try {
    ViewConfiguration config = ViewConfiguration.get(this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");

    if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
    }
} catch (Exception e) {}

Sources:

Currently ActionBar is falling into disuse, in favor of Toolbar . The Toolbar is a generalization of ActionBar because it allows a generic positioning, greater control and a greater power of stylization in several aspects, in the special case it plays the role of ActionBar .

Furthermore, it came with great force because of Material Design, which has some topics about it in guideline : link and link .

It came in version 21, but the support library itself implements it for lower versions. If you want to see how to use it: How to use the Toolbar widget? .

The support library has many more features than just ActionBar/Toolbar , so it's good to have a look at the features: .

Remembering that there are several almost independent modules:

  • Support Library v4
  • Support Library v7 (depends on Support Library v4):

    • Appcompat
    • Gridlayout
    • Media-router
    • Cardview
    • Palette
    • Recyclerview
  • Support Library v8

  • Support Library v13

In fact, because the support library is very extensive in functionality, it ends up weighing. It is worth evaluating whether this "cost" pays off in relation to the benefit you will have. Remembering that the vast majority (I have no statistical data, it's just a conjecture) of the applications have much larger sizes (because they use many other libraries).

One cool app to check out is the Detective Droid , which lists which libraries a particular application has.

In my opinion, it's almost time to ignore certain versions. According to the Android Dashboard , there are only 7.4% of devices with GingerBread. It's a small number but I do not know if it could be ignored right now, but soon.

Only this statistic would lead to the abandonment of the Support Libraries, but sometimes its implementation of several functionalities are much more stable than the native ones, that do not undergo corrections, unlike the Support Library. This can be seen in the list of revisions: link .

I think this post: link already mentions a problem with Fragment's natives.

    
18.02.2015 / 20:30
0

In addition to what @Wakim said:

If you do not use the compatibility libraries, you will see that your application may look slightly different, depending on the version of Android and the manufacturer of the device. Using the Holo theme, you'll have an "old-face" application without using Material Design. Depending on your target audience, this can have a big impact.

Also, search for proguard ( link ). It decreases the final apk and application size after installation. You can enable it in the "minifyEnabled" command of your build.gradle. You can find in Google a setting that optimizes support libraries, but AndroidStudio already provides a basics.

By default, Gradle only runs proguard when you build the apk for publishing (buildTypes > release> minifyEnabled) because it takes time to execute.

    
05.08.2015 / 14:38