ActionBar doesn't shows up when installed to Android Gingerbread - java

I've used a custom Dialog theme which i name poptheme and when i installed it to Gingerbread, the action bar doesn't shows up but when i installed it to Kitkat it works fine.
Since it's a dialog theme, a customized a new theme because i can't put an action bar using the theme theme.dialog. It worked fine on my phone which has Kitkat version, but when i try to run it on Gingerbread version, the layout is not what i wanted it to look like.
This is the code in my onCreate() for the customized dialog theme:
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = this.getWindow().getAttributes();
params.alpha = 1.0f;
params.dimAmount = 0.7f;
params.height=250;
params.width=420;
this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
// This sets the window size, while working around the IllegalStateException thrown by ActionBarView
// this.getWindow().setLayout(420,250);
// this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_main);
//getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);

Window.FEATURE_ACTION_BAR was added in API 11 (Android 3.0) so this approach won't work for Gingerbread (API 9/10).
You will need to use Android support library.

In below API 11 Action Bar Showed as a menu
click on Menu Button to see your Action Bar
Action Bar Features was Added in API 11 .
For Backward Compatibility Please use
ActionBarSherlock
Actionbar sherlock link

Related

How to get notified about Android system theme change?

Android introduced Dark Theme in API level 29 and higher (https://developer.android.com/guide/topics/ui/look-and-feel/darktheme). To support Dark Theme in your own app your app's theme needs to inherit from DayNight theme.
But what if you did your own theming, is there any intent from Android to get noticed about the system theme change?
If you add android:configChanges="uiMode" to your activities in the manifest, when the user changes the theme, the onConfigurationChanged method is called. If you override that you can do all the related work in there. In order to check what the current theme is you can do the following:
val currentNightMode = configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active, we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active, we're using dark theme
}
EDIT: Since the original question isn't specific to Kotlin, here is the Java version of the above for reference:
int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
break;
}
(source)

How to click Android phone "home" button programmatically in espresso

I need to simulate home button click in Espresso on Android phone.
I tried
onView(withId(android.R.id.home)).perform(click());
and onView(withContentDescription("Navigate up")).perform(click());
as some posts suggested but it always fails to find the view.
I'm new to Espresso and not sure how to debug it. Thanks.
Better to use withContentDescription(R.string.abc_action_bar_up_description) rather than "Navigate up" but it doesn't act like a home button click anyway, it only uses the navigation bar's "back" button, so it would only work if you have it in your app.
If you want to simulate the home button click and you are using the UI Automator library, you can simulate it like this
fun pressHome() {
// Might be a good idea to initialize it somewhere else
val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
uiDevice.pressHome()
}
Alternatively, if you don't want to or can't use UI Automator, you might try calling Intrstumentation.sendKeyDownUpSync() with the KeyEvent.KEYCODE_HOME.

Make Status bar Translucent on pre lollipop devices(kit-kat) while being compatible with jelly bean

Making an Android application with Minimum sdk version jelly bean.
I want to make status bar translucent there are many ways to make status bar translucent on kitkat devices. On applying some of them error says jelly bean does not support this type of theme and then app would not compile. (I don't even want or think for making status bar translucent in jelly bean).I just want application to compatible with jelly bean and having status bar translucent in Kit-Kat.
Ideas please.
At present there are 3 types of status bar in Android. TYPE 1 : Status Bar in Pre-Kitkat devices. TYPE 2 : Status Bar in Kitkat devices which is having Translucent Property which can merge The content of our application with content of status bar. TYPE 3 : Status bar after Lollipop which is Translucent and also we can set color of status bar by adding one line on Styles.xml. Now, to have different properties of different status bars in different devices we have to define different Style.xml file for each api like v16 , v19, v21.

3 dot setting menu for android apps with custom title

in an android app,
if you create a new project,
then automatically the 3 dot settings menu is created on phones where it is needed
and it is handled the same way as it would have in older versions by:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
if you create a custom title, you have to add the 3 dot menu yourself
how do I know when it is needed? meaning phones that don't have the settings button
also how do i create a context menu that is customized and attached to the 3 dot button
Edit:
after a disscusion with Shobhit Puri, I understood that I was not considering the actionbar, since I am using a minimum API 8, I don't have it,
so there is the option that CommonsWare just supplied to check if the settings menu exists (I still need to check if it exists in API 8)
Shobhit Puri's suggestion was:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(...) ;
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title") ;
but that of cores requires API 11 or the support library V7
either way I am excepting Shobhit Puri's answer, because of all his help, and I will post my final solution when I know it works
also thanks to CommonsWare for a nice answer
Edit2:
I decided to go with CommonsWare solution for now, I wrote it like this:
if (android.os.Build.VERSION.SDK_INT >= 14) {
ViewConfiguration vc = ViewConfiguration.get(this);
if (!vc.hasPermanentMenuKey()) {
setting_dots.setVisibility(View.VISIBLE);
setting_dots.setOnClickListener(this);
registerForContextMenu(setting_dots);
}
}
ideally I think you should use the actionbar, because it provides you with most of the work, but it has a lot of compatability issues with API 8 which for now I would rather avoid
As #dumazy pointed out that the Action bar's Menu Overflow icon is only shown on those devices which do not have a hardware menu-button.
How do I know when it is needed? meaning phones that don't have the settings button
This is handled by Android itself. You don't need to worry.
how do i create a context menu that is customized and attached to the 3 dot button
You can just have a an xml file inside Menu folder in res. Then you can specify the xml file inside the MenuInflater. Eg:
lets name it list_menu.xml
?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_1"
android:title="#string/menu_string_1"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_item_1"
android:title="#string/menu_string_2"
android:showAsAction="ifRoom|withText"/>
</menu>
In the onCreateOptionsMenu you can set it as:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
This menu would be attached to the overflow-icon and have the items that you want to show when it is clicked. There are some hacks like this which can show the overflow-icon on all devices but using them is highly discouraged. Let android handle this itself.
You seem to use Title bar. Instead, try to use Action Bar for the same.
Hope this answers your question.
how do I know when it is needed? meaning phones that don't have the settings button
Call hasPermanentMenuKey() on a ViewConfiguration.
also how do i create a context menu that is customized and attached to the 3 dot button
By programming. Since you are not using an action bar, it is impossible to give you specific advice that would be relevant.
Google says Actionbar overflow only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
If you still want to implement this you may follow this solution.
Click here for your reference.
Just copy this method in your activity and call the method from onCreate method
private void getOverflowMenu() {
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) {
e.printStackTrace();
}
}

Android action bar home

Simple enough,
At the top left hand corner of the action bar sits the default icon for the application. In most apps, it is clicked and returns you to the homepage. I'm working with 2 devices, a 3.2 and a 2.3.3 and I am trying to implement the action bar on the 3.2 without affecting the other.
I imagine its implemented like this:
case android.R.id.home:
Intent intent = new Intent(this, ActOnThisActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
One last thing is to set the flag
getActionBar().setHomeButtonEnabled(true);
This should work but as anyone familiar with android will know, this cant be run on an API of 11 or below. So it will run on 3.2+ but not the 2.3.3. Is there a way to specify this method to only work on api11 and above?
NOTE that #TargetApi(11) annotation might work but I've had some weird errors with it.
Is there a way to specify this method to only work on api11 and above?
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setHomeButtonEnabled(true);
}
Taking a look at ActionBarCompat from the sdk samples will give you fair idea of how to do it.
The following code in the ActionBarHelper class(from the sample) decides the instance for different versions.
public static ActionBarHelper createInstance(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return new ActionBarHelperICS(activity);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return new ActionBarHelperHoneycomb(activity);
} else {
return new ActionBarHelperBase(activity);
}
}
Have a look at ActionbarSherlock - I use it in a few apps and it works flawlessly. Mimics the Action Bar perfectly in Android versions below Honeycomb (3.0).
http://actionbarsherlock.com/
Main API for nearly all interaction with the action bar. This is the exact API getSupportActionBar() exposes.

Categories

Resources