Android onCreateOptionsMenu is not displaying - java

In my android activity, i removed the Action Bar by using this Theme
<style name="Theme.NoTitle" parent="#android:style/Theme.NoTitleBar"></style>
but my problem is “onCreateOptionsMenu” is not display, I tried this method to create
that pls see
private void getOverflowMenu()
{
try
{
ViewConfiguration config = ViewConfiguration.get(Home.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null)
{
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config , false);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
in this “onCreateOptionsMenu” is not displaying. please help me
but it displaying in Samsung
Because: in Samsung mobile has its own hardware menu button, but not in nexus etc
please see the image

finally i got the answer
in my AndroidManifest.xml i just removed this line
android:targetSdkVersion="18"

If you don't have action bar and neither a menu button (on most devices) where do you want to display your menu??
The easiest way is to use PopupMenu. Add a button somewhere in your layout and on the click even add the PopupMenu (Dropdown menu). If you want to use it for devices starting Android 2.1 use a Support Library.
You have HERE the android documentation about PopupMenu.
From SDK Manager you can download and see the support library v7 sample which includes PopupMenuHelper.
Hope it helps. Good Luck!

Related

First addCommand() item in a Form fails when used with native theme

I have encountered a problem when using the the addCommand() method of the Form class along with the Native theme - other themes work fine. See the following example:
Form hi = new Form("Hi World");
hi.addComponent(new Label("Hi World"));
// with native theme - can't click on the first command in the list
hi.addCommand(new Command("Dummy1") {
public void actionPerformed(ActionEvent ev) {
Dialog.show("Dummy1 Clicked!", "You clicked the Dummy1", "OK", null);
}
});
hi.addCommand(new Command("Dummy2") {
public void actionPerformed(ActionEvent ev) {
Dialog.show("Dummy2 Clicked!", "You clicked the Dummy2", "OK", null);
}
});
hi.show();
When I create an application using the code above, a click on the second command ("Dummy2") produces the expected Dialog, but a click on the first command ("Dummy1") does nothing.
This only happens when using the Native theme. If I switch to Flat Blue, then clicking on either command produces the expected Dialog.
This behavior happens both on the Simulator and on a real Android device (don't know about iOS).
Fyi, my toolchain is NetBeans IDE v8.2, Java 1.8.0_25, with the CodenameOne plugin v3.6.0.
Has anyone else seen this? Am I missing something? If so, is there a workaround?
If the element is very narrow and very close to the top the click might be misinterpreted as a click out of bounds or on the status bar area. You need to set the styling of the SideCommand to have a sensible default as this element is very application specific. Otherwise touches might be lost.
I tried styling the SideCommand but it didn't seem to help. What worked for me was to define a style for TitleArea and simply uncheck Derived for the Padding settings (I left them all set to 0px).
I have no idea why this works - I would have thought that the derived values would have been zero in any case.

Calling two different activities on different devices (tablet and phone)

Here, I'm trying to start an activity on different devices (Android Tablet and Android Phone). Is there a way to use if statement to check which activity should to load based on the devices?
Here's my code:
Button buttonGo = (Button) findViewById(R.id.button_goToActivity);
buttonGo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (tablet device) {
startActivity(new Intent(MainActivity.this, TabletActivity.class));
} else {
startActivity(new Intent(MainActivity.this, AndroidPhoneActivity.class));
}
});
Thanks for your effort...
In order to achieve that you must use Fragment not an Activity. the Design Philosophy of Fragments is exactly what you want. you can take a look at Example to see how it is implemented. This is a master-detail app with two Fragments, when device in a portrait mode or it is not a tablet it just shows master, otherwise it also shows detail beside the master and when you select an item from the list you will see the details without going to other Activity.
Firstly as Android Best Practice it is adviced to use Fragments rather than Activity to achieve your requirement.
But if you are keen on using Activity, then below is the solution to find device type and load different activities.
1) Have 2 values folders: values -> strings.xml and values-sw600dp -> strings.xml
2) Create string value in both the xml
<string name="device_type">PHONE</string> in values -> strings.xml
<string name="device_type">TABLET</string> in values-sw600dp -> strings.xml
3) create a method to check if current device is Phone or not
public boolean isPhone() {
String deviceType = getResource().getString(R.string.device_type);
if (deviceType.equalsIgnoreCase("PHONE") {
return true;
} else {
return false;
}
}
4) Depending on the device load your activity as you have already done in your above code snippet
Let me know if this helps...
Most likely, you should not have two separate activities here. Instead, you should have two different layouts for the same activity. The layout for phones should be in the regular res/layout folder and the layout for tablets should be in the res/layout-large folder. Read Supporting Different Screens for more details.
If you are using same fuctionality for Tablet Activity and Phone activity then you do not need two activities for this purpose. You need to make two different Layout xml for Tablet as well as Phone.
Make a Layout folder named : layout-large and layout-xlarge.
Now copy the xml file from normal layout folder and paste in both new layout folders.
No need to change the name of xml file.
Now Adjust xml file of layout-large and layot-xlarge as you want to make design for tablet.
Now on running application Tablet and phone bot will pick xml according to there size and screen density.
Note : the functionality should be same.
And if: you want to show different screen with different functionality for phone and tablet then. use this code for check.
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
}
Hope this wll help you.

Android Showcase View how to use?

Well For Showcase View
I using this:
https://github.com/amlcurran/ShowcaseView
After importing files it gives error. this is my errors and improted .jar files
Errors says
in java
R cannot be resolved to a variable
in style
error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light'.
again, in style
error: Error: No resource found that matches the given name: attr 'android:fontFamily'.
Also Are there any tutorial to use Showcase view in my project. I can not find and I didnt understant from github project. it is not clear.
This library works Awesome for all. How it works?? i want to highlight a toolbar option. Now just add the library and write code like this.
you can use multiple showCaseView using this code
1 add Library in our Gradle.build
implementation 'com.github.mreram:ShowCaseView:1.0.5'
simple call this method with pass title, Description Text, view or view Id and Type
ShowIntro("SetTheme", "Select Theme and Apply on your video", R.id.button_tool_theme, 1);
method create like this
private void ShowIntro(String title, String text, int viewId, final int type) {
new GuideView.Builder(this)
.setTitle(title)
.setContentText(text)
.setTargetView((LinearLayout)findViewById(viewId))
.setContentTextSize(12)//optional
.setTitleTextSize(14)//optional
.setDismissType(GuideView.DismissType.targetView) //optional - default dismissible by TargetView
.setGuideListener(new GuideView.GuideListener() {
#Override
public void onDismiss(View view) {
if (type == 1) {
ShowIntro("Editor", "Edit any photo from selected photos than Apply on your video", R.id.button_tool_editor, 6);
} else if (type == 6) {
ShowIntro("Duration", "Set duration between photos", R.id.button_tool_duration, 2);
} else if (type == 2) {
ShowIntro("Filter", "Add filter to video ", R.id.button_tool_effect, 4);
} else if (type == 4) {
ShowIntro("Add Song", "Add your selected song on your video ", R.id.button_tool_music, 3);
} else if (type == 3) {
ShowIntro("Overlay", "Add your selected overlay effect on your video ", R.id.button_tool_overlay, 5);
} else if (type == 5) {
SharePrefUtils.putBoolean("showcase", false);
}
}
})
.build()
.show();
}
Here for this I am using this library
https://github.com/amlcurran/ShowcaseView
This library works really great. How it works??
Suppose i want to highlight a menu option. Now just add the library and white this code.
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu_main);
setSupportActionBar(toolbar);
ViewTarget target = new ViewTarget(toolbar.findViewById(R.id.menu_id_launcher));
new ShowcaseView.Builder(this)
.setContentTitle("Its My Navigation Drawer")
.setContentText("Click here and you will get options to navigate to other sections.")
.useDecorViewAsParent() //this is the difference
.setTarget(target)
.build();
}
and finally you have to make your menu show always like this
<item
android:id="#+id/menu_id_launcher"
android:orderInCategory="100"
android:icon="#mipmap/ic_launcher"
android:title="#string/action_settings"
app:showAsAction="always" />
Now just run the app i think this will works perfectly.
I usually use another library for showcase views. It creates bubbles instead of concentric circles but it works really well. Maybe it is also useful for your requirements and with this one you don't have importing problems.
You just have to add 'implementation 'com.elconfidencial.bubbleshowcase:bubbleshowcase:1.3.0' in your build.gradle file of your app and synchronize your project.
It works similar to ShowCaseView library:
BubbleShowCaseBuilder(this) //Activity instance
.title("foo") //Any title for the bubble view
.targetView(view) //View to point out
.show() //Display the ShowCase
Source https://github.com/ECLaboratorio/BubbleShowCase-Android
The sample you use needs the ActionBarSherlock Library, you can read it in the instructions from the github page: "To use the sample download ActionBarSherlock and add it as a dependency to the library project. Use point 1 on the "Including in your project" instructions at the link."
download ActionBarSherlock and import it (only the library, you don't need the samples)
right click to your SampleActivity project and choose Properties
choose Android, click Add and include the ActionBarSherlock project
Then the style errors will go away and R can be built.
import, add actionbarsherlock library
android:fontFamily minSDK is 16, or delete this
project -> clean all

Can you restrict a specific Android API version from running/downloading app from googlePlay store?

Alright so I'm having this one problem with an app I'm working on and planning on releasing, but the problem is only occurring on one version of the android SDK (Version 4.1.1 API 16).
Can I restrict JUST android version 4.1.1 API 16 from downloading and running my app?
I know googlePlay has a restrict certain device list, but the problem is occurring because of the OS version, NOT the device, at least it seems that way from debugging...
I also know there's the uses-sdk attributes in the manifest file:
<uses-sdk android:minSdkVersion="integer"
android:targetSdkVersion="integer"
android:maxSdkVersion="integer" />
But setting the android:minSdkVersion to 17 will be way to high and not be available to many users, I also put a lot of work into making the app backwards compatible and works fine on pre 3.0 devices, it's just this android version 4.1.1 API 16 that is causing me trouble...
Please help! Thank you!
Edit: This is my current uses-sdk in my apps manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
Edit with relevant code:
//button listener for the number buttons. Each button will display a number 1 - 10
//upon clicking the button the number that is displayed on the button will be
//added to what is displayed in the textView
//
//mCurrentWorkingText is the current expression that is being added updated.
View.OnClickListener genericNumberButtonListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//cast the incoming view to a textView so we can get
//the text the button displays
TextView textView = (TextView) v;
String textFromButton = textView.getText().toString();
if (mCurrentWorkingText.length() == 0) {
mWorkingTextView.setText(mWorkingTextView.getText()
.toString().concat(textFromButton));
mCurrentWorkingText = textFromButton;
} else {
// if the working TextView isn't zero we need to
// append
// the
// textFromButton to what is already there.
mWorkingTextView.setText(mWorkingTextView.getText()
.toString().concat(textFromButton));
mCurrentWorkingText = mCurrentWorkingText
.concat(textFromButton);
}
}
};
Edit with log of problem:
The string changes almost randomly to a completely random number in the last log statement.
You can prevent the app from running.
And no, you cannot prevent the app from downloading when on an API in between minSDKVersion and maxSDKVersion/targetSDKVersion
You can check it like this:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD && android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.HONEYCOMB) {
// For gingerbread but lesser than HONEYCOMB, show a dialog or something and close the app
finish();
}
So the problem I was having was caused by the pageTransformer that I was using. Android 4.1.1 really didn't like it. I don't know why but all I know is that if you're using pageTransformer with custom views and ActionBarSherlock you better be careful when using the pageTransformer to add animation to the page turns.

Robotium - Trying to click home button in app

I'm new to robotium and i'm trying to write a quick and dirty script to run through all screens in an app.
The problem i have mainly with the 'home button' in the app. I've tried lots of options but i cant seem to get it to click there except with index, which is not what i want.
When i check out the button with the hierarchyviewer it looks like this:
Link
However when i try for example:
assertTrue(
"Wait for text (id: myapp.R.id.home) failed.",
solo.waitForImageById("myapp.R.id.home", 20000));
solo.clickOnImage((ImageView) solo.findViewById("myapp.R.id.home"));
solo.waitForActivity("MenuActivity");
It fails at the waitForImageByID line. Ive tried multiple options like waitForImageButton etc, but i just cant seem to get it clicked. What am i missing here?
junit.framework.AssertionFailedError: View with id: '0' is not found!
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1990)
at com.jayway.android.robotium.solo.Solo.getView(Solo.java:1970)
at com.bitbar.recorder.extensions.OtherUtils.a(OtherUtils.java:246)
at com.bitbar.recorder.extensions.OtherUtils.b(OtherUtils.java:241)
at com.bitbar.recorder.extensions.v.a(Waiter.java:71)
at com.bitbar.recorder.extensions.ExtSolo.waitForImageButtonById(ExtSolo.java:4176)
at com.example.android.apis.test.Test.testRecorded(Test.java:137)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1740)
Use the following line to press the home button in the action bar
solo.clickOnActionBarHomeButton();
The issue is that the id that it is referring is not in your application, it is in androids default R file, try android.R.id.home and it should work fine. It is worth noting though that if your application uses action bar sherlock to support the action bar pre 4.0 that this will have a different ID there and you will have to handle this in your test.
You can see this for yourself looking at: http://developer.android.com/reference/android/R.id.html
When you are using ActionBarSherlock there are two different Ids you have to check, android.R.id.home for API-Level>11 and abs__home for lower levels (provided by ActionBarSherlock):
View homeButton = activity.findViewById(android.R.id.home);
if (homeButton == null) {
homeButton = activity.findViewById(R.id.abs__home);
}
What about this code:
ArrayList<LinearLayout> ll = solo.getCurrentViews(LinearLayout.class);
//You can change 1 with the ordinal number of LinearLayout you want to click.
solo.clickOnView(ll.get(1));
or also
ArrayList<ImageView> iv = solo.getCurrentViews(ImageView.class);
//You can change 0 with the ordinal number of Image you want to click.
solo.clickOnView(iv.get(0));
I think if you identify the correct id for view or linear layout or image view it should work.
Dave C's answer was working only partially for me. The button was clicked but before the preceding screen was loaded assertions had started and thus were always false. The solution is to run "home click" on the main thread (Robotium 5.2.1):
getInstrumentation().runOnMainSync(new Runnable() {
#Override
public void run() {
solo.clickOnActionBarHomeButton();
}
});
From your question I can see that it is an image view. You can click on any view using the following piece of code.
View view = solo.getView("View_name_from_hierachy_viewer");
solo.clickOnView(view);
View_name_from_hierachy_viewer in your case will be "home".
Let me know if this does not work.

Categories