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?