Open Option menu on device without hardware keys - android

In my app I want to use an activity without titelbar or actionbar. I solved this with:
<activity
android:name="com.cilenco.lockscreen.Position"
android:label="#string/title_activity_position"
android:theme="#android:style/Theme.Light.NoTitleBar" >
<intent-filter>
<action android:name="com.cilenco.lockscreen.Position" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
My problem now is that I have to use an option menu and if I run my app on a device without hardware keys I can not open the option menu. I thought I will the Button with 3 dots (Overflow menu) in the bar where the back and home key is but it is not visible. What can I do to open the option menu on devices without hardware keys?

There is a function openOptionsMenu() that you could call
manually in your activity when you want to open the menu.
public void openMenu()
{
openOptionsMenu();
}
Better:
if you want the 3 dot overflow button in the navigation bar
just create your optionsMenu as usual:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
//add your menu
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
//select what happens when you click at an item
return true;
}
and make sure to set both minSdkVersion and targetSdkVersion to "10" or lower!
For example:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name">
<uses-sdk android:minsdkversion="4" android:targetsdkversion="10" />
</manifest>

Related

Why to modify Java code while adding "Up button" in activity?

My aim is to add Up button to the Activity. The reason to question is that it is accomplished successfully just by adding android:parentActivityName=".MainActivity" in AndroidManifest.xml
Now, I am wondering why
https://developer.android.com/training/implementing-navigation/ancestral#java suggest us to make changes in the java code.
Android Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.miwok">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
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>
<activity android:name=".NumbersActivity" android:label="Numbers" android:parentActivityName=".MainActivity"/>
</application>
For devices running Android 4.1(API level 16) and above, you can specify the android:parentActivityName attribute in the AndroidManifest.xml file.
But if the app is targeting devices below Android 4.1, then you need to add the parent activity information in the <meta-data> tag and add the java code that uses the NavUtils class for it to work properly.
By modifying the java code you can adjust the appearance in the gui.
In different Android versions there are various appearances. For example the style of the back arrow changed. Furthermore, depending on the android version the app icon is clickable. When you want to influence this behavior you can do it by writing java code.
For a close look I suggest : http://codetheory.in/difference-between-setdisplayhomeasupenabled-sethomebuttonenabled-and-setdisplayshowhomeenabled/
Now, I am wondering why
https://developer.android.com/training/implementing-navigation/ancestral#java
suggest us to make changes in java code.
Referring to your statement, I assume you are talking about this code snippet below.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
The reason for adding this code to your android java file is to allow you to have more customisation towards the action bar, just in case you want to change the default back option, such as UI and functionality (for example: you can perform some action such as sending a request to the server before returning to the parent?)

Option button not showing in action bar Android

I am trying to add action button , but they do not showing in action bar, they are display at button when click on hardware menu button . And the up button is also not working .
i am sharing my code , tell me where i am wrong
this is my manifest file :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
>
<!-- Parent activity meta-data to support API level 7+ -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secindActivity"
android:label="Items"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
I am adding the up button in second activity ,. it is showing but not working .
And also trying to add action bar buttons in secondActivity . Menu file is as below :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:Digicare="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_back"
android:icon="#drawable/home2"
Digicare:showAsAction="ifRoom"
android:title="Home">
</item>
</menu>
this is my java file of secondActivity :
it never comes in the if statement .
i put log in it , so i get it that it never comes in if statement
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if ( item.getItemId() == R.id.home){
Log.w("asdfasdfasdf","asdfasdf");
Intent upIntent = NavUtils.getParentActivityIntent(this);
NavUtils.navigateUpTo(this, upIntent);
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Mymenu = getMenuInflater();
Mymenu.inflate(R.menu.item_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
These are problems .
how to done this ????
First issue
This is a duplicate of this question. I think that it doesn't show up because you have an options button on your device.
See this post about modifying this behavior in reflection (however, I'm not sure I would recommend it, as people have expectations about their device's behavior).
Second issue
If I followed your code correctly, then the item id is item_back and not R.id.home...

Targeting different APIs for contextual menu

I returned to reading this book today after finishing my simple program and publishing it, and I stopped on chapter 18 which is about context menus, anyways I read this:
With contextual actions the situation is more complicated , you still define one menu resource but you implement two separate sets of callbacks , one for contextual action bar and one for floating context menus.
I'm worried now since I only looked at a reference to implement context menus, I didn't read about it much, so I didn't actually target the two things.
I have a context menu with options without icons, only text.
Is this going to be a problem?
I've tested this on API 8 and 17 and 18 but it's the same.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_rename" android:title="Rename" />
<item android:id="#+id/menu_delete" android:title="Delete" />
</menu>
Code
I'm using registerForContextMenu(getListView()) so I can handle the view and used:
#Override
public void onCreateContextMenu(ContextMenu menu , View v , ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.notes_handler_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getItemId() == R.id.menu_rename){
// do something
}
return false;
}
Didn't need to put all of the code since there's no problem. I only wanted to know if this is going to cause problems to earlier devices before API 11 or after.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appabdulmohsen.subjectstracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/iconsubjecttracker"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.appabdulmohsen.subjectstracker.SubjectList"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity_subject_tracking"
android:label="#string/app_name"
>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".SubjectList"
/>
</activity>
<service android:name=".Service_reminder"></service>
<activity
android:name=".activity_todialog_notecontent"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:excludeFromRecents="true"
></activity>
</application>
</manifest>
--
my question is simple , is what I used enough for both api 8 and up ? or do I need to implement other methods ?
Sorry, I'm not thinking straight.
You have no code for a contextual action bar, based upon what you have posted. Hence, you will not see a contextual action bar. I don't even know if you have a regular action bar -- you probably do, but that would depend upon the definition of AppTheme.
Your code will bring up an old-style context menu.
Is this going to be a problem?
That depends on your definition of "problem". Using a context menu on API Level 15+ devices (i.e., the vast majority of Android devices in use) will look out of date to some people.

How to show option menu in android 4.2

I am trying to create menu option in my test application.
I am able to see the menu when I set the theme in manifest to just default (The menu shows up in the top). If I set the theme in manifest to NoTitleBar. I can't see the menu option?
I want to get the menu when I set theme "NoTitleBar" in manifest.
How to fix it?
Below are things that I have used in my test application:
with Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity
android:name="com.ssn.menuoptions.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And
Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_preferences"
android:title="Preferences" />
</menu>
Java file:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Is it possible to get something like this:
How do I make menu to show up down?
Thanks!
Add following code below Activity.setContentView();
setContentView(R.layout.activity_detail);
// set option menu if has no hardware menu key
boolean hasMenu = ViewConfiguration.get(this).hasPermanentMenuKey();
if(!hasMenu){
//getWindow().setFlags(0x08000000, 0x08000000);
try {
getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
}
catch (NoSuchFieldException e) {
// Ignore since this field won't exist in most versions of Android
}
catch (IllegalAccessException e) {
Log.w("Optionmenus", "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);
}
}
If you don't want an action bar in your app, then you can just set up a button with this handler:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) { openOptionsMenu(); }
});
Now myButton works like the menu key. This is an easy way to migrate a fullscreen app to 4.x, though longer term a more elegant solution should be sought.
Using the notitlebar theme removes the actionbar where the menu button is. You'd probably have to make a custom theme to have your menu in it.
If you're not doing anything about it, you can always at least use this attribute: android:showAsAction="Never" so that users with phones that have a menu button can pop up the menu with their button.
Add in Android Manifest android:targetSdkVersion="10"
<uses-sdk
android:targetSdkVersion="10" />

Two action bars (Bottom and Up) at the same time?

I have need to make two action bars , I am using actionBarSherlock by the way . So what I need exactly is to put a "Welcome screen" toggle on the normal action bar up , and add two normal ActionBar Action options . Similar to what I need are Gmail and Maps like here : http://cdn.androidcommunity.com/wp-content/uploads/2012/03/Screenshot_2012-03-28-12-58-16.png (They didn't allow me to post an image for reputation is low , see the link please)
This Maps app has an upper and bottom action bar , exactly what I need , because once I reach the point where I can add the second actionbar I know where to start ...
I have searched for about a week about this topic and I have found a few similar questions , but however I have understood non of the answers ; the answers were about a custom view which I am not (at all) familiar with and I can't understand a thing , but yes I tried to make a "custom view" from whatever I thought is right and I got crrassshheesss which I didn't find any solution for ... If you may plz show me as an example this Maps app how is it using those two action bars ?? (I don't want to navigate like in maps and gmail , but only the two actionbars)
Here is some of my code :
//Part of my MainActivity.class
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inf = getSupportMenuInflater();
inf.inflate(R.menu.upper_navbar, menu);
final String name = "welcome";
final SharedPreferences pref = getSharedPreferences(name, 0);
final Editor edit = pref.edit();
boolean oldState = pref.getBoolean("w", true);
ToggleButton tog = (ToggleButton) menu.findItem(R.id.welcome_screen).getActionView();
if(oldState){
tog.setChecked(true);
}else{
tog.setChecked(false);
}
tog.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
edit.putBoolean("w", true);
edit.apply();
}else{
edit.putBoolean("w", false);
edit.apply();
}
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="seaskyways.editpad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:name="android.app.Application"
android:icon="#drawable/ic_launcher"
android:uiOptions="splitActionBarWhenNarrow"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="seaskyways.editpad.MainActivity"
android:label="MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.editpad.Splash"
android:theme="#style/Theme.Sherlock.Dialog.NoActionBar"
android:label="Splash" >
<intent-filter>
<action android:name="android.intent.action.SPLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And my menus which I intend to put them in my action bars ...
menu\activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/bottom_copy" android:title="Copy" android:orderInCategory="3" android:showAsAction="ifRoom|withText" />
<item android:id="#+id/bottom_paste" android:title="Paste" android:orderInCategory="2" android:showAsAction="ifRoom|withText" />
</menu>
menu\upper_navbar.xml (Should have named it upper_actionbar but its just a thinking mistake , its a name afterall , :/ plz proceed )
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/welcome_screen"
android:orderInCategory="1"
android:showAsAction="ifRoom|withText"
android:title="Welcome Screen"
android:actionLayout="#layout/toggle"
/>
</menu>
If you need anymore info please tell me , I will tell , its for me and everyone else afterall !
EDIT !!! : I found a much near example to my question , the wifi settings (http://omgdroid.com/wp-content/uploads/2012/07/Screenshot_2012-07-06-01-34-29-576x1024.png) It uses a switch in the normal actionbar and a normal split actionbar down ! Exactly what I need !
EDIT 2 !!!! : Here is a screenshot from my Galaxy nexus using a custom ROM based over aosp , This is exactly what I need and mean :
Settings-Wifi: https://lh4.googleusercontent.com/-aeL_sHjIcQQ/UPVptGQiuqI/AAAAAAAAAGE/UGc-CuLP4Qw/s512/Screenshot_2013-01-15-16-36-32.png
Settings-Bluetooth: lh3*googleusercontent.com/-4j6ca1Nm1VI/UPVqAiDn_PI/AAAAAAAAAGM/LLB2ILWVjQY/s512/Screenshot_2013-01-15-16-38-14.png
EDIT 3 !!! : Big progress in my investigation in the Bluetooth and Wifi , and as I said and asked , they were true actionbars ! see what I got in Settings-Bluetooth:
BluetoothSettings.class/onCreateOptionsMenu :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (mLocalAdapter == null) return;
boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
boolean isDiscovering = mLocalAdapter.isDiscovering();
int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
R.string.bluetooth_search_for_devices;
menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
.setEnabled(bluetoothIsEnabled && !isDiscovering)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, MENU_ID_RENAME_DEVICE, 0, R.string.bluetooth_rename_device)
.setEnabled(bluetoothIsEnabled)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, MENU_ID_VISIBILITY_TIMEOUT, 0, R.string.bluetooth_visibility_timeout)
.setEnabled(bluetoothIsEnabled)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
super.onCreateOptionsMenu(menu, inflater);
}
These can be options are clearly shown in the splitActionBarDown with the exact properties up ...
now :
BluetoothSettings.class/addPreferencesForActivity :
#Override
void addPreferencesForActivity() {
addPreferencesFromResource(R.xml.bluetooth_settings);
Activity activity = getActivity();
Switch actionBarSwitch = new Switch(activity);
if (activity instanceof PreferenceActivity) {
PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
final int padding = activity.getResources().getDimensionPixelSize(
R.dimen.action_bar_switch_padding);
actionBarSwitch.setPadding(0, 0, padding, 0);
activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
}
}
mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);
setHasOptionsMenu(true);
}
Noting that this class isn't extending Activity , it extends DeviceListPreferenceFragment ...
Now that we know it's possible to have two actionbars(split and normal) in the same time , we need a way to simplify that , maybe by a class or library ??
That is a split action bar. It is available for handset devices with a screen width of 400dp<
To enable split action bar, simply add uiOptions="splitActionBarWhenNarrow" to your or manifest element.
Edit:
The second image is indeed not a split action bar. These are just buttons with the buttonBarButtonStyle attribute. Look into this here.
Edit:
splitActionBarWhenNarrow has been deprecated.
I have need to make two action bars
Then you are on your own. This is not supported in Android or ActionBarSherlock, other than the split action bar, which you dismissed.
Similar to what I need are Gmail and Maps like here
That is a split action bar, specifically one using overlays. This sample project demonstrates this technique.
This Maps app has an upper and bottom action bar , exactly what I need
Then use the split action bar, via android:uiOptions="splitActionBarWhenNarrow" in your <activity> element, the way that Maps does. The main Maps activity uses android:uiOptions="splitActionBarWhenNarrow".
If you may plz show me as an example this Maps app how is it using those two action bars ?
As noted above, this sample project demonstrates this technique.
I know how to make one action bar (split and normal)
Maps is using a split action bar.
but what I need is making them both at a time so I can have two actionbars !
In your screenshot, you will notice that Maps has a split action bar, and they are both on the screen at the same time. This is the way split action bars work, in narrow situations. The same Maps app does not show the split action bar when the screen is not presently narrow.
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
//this is main line for that
android:uiOptions="splitActionBarWhenNarrow"
>
<activity
android:name="com.example.androidactionbarbottam.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is main line for bottam side
android:uiOptions="splitActionBarWhenNarrow"

Categories

Resources