3
I’m doing some test applications and I ended up coming across something that has caused me many doubts, the Android support libraries. I tried to develop an application that uses Actionbar natively (without support), works perfectly on the emulator, but when I try on my smartphone, the menu item that is with android:showAsAction="never"
is not displayed, follow the settings and the code I have.
Note: in the emulator the entry 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);
}
}
}
xml menu.
<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 that link from the OS I was able to do it with the support library, but I noticed the size of the .apk
from 53.6 KB (no support) to 0.9 MB (with support) and the smartphone space used was 72 KB (no support) to more than 3 MB (with support). So my doubts are as follows:
- It is possible to make an application use Actionbar without the support library (for devices with API 15 or higher)?
- When I should really use the support libraries?
Extra
The Actionbar Apis Were first Added in Android 3.0 (API level 11) but they are also available in the Support Library for Compatibility with Android 2.1 (API level 7) and above.
According to this section above, theoretically the Actionbar should be native from API 11 or am I confusing different subjects?
Really that’s it, not aware of this detail of the physical button, the emulator probably does not have this setting enabled (if that is possible).
– mateusalxd