Camera Preview Example - java

I know this is really basic but it is a error that I cannot find a solution to.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
I have 2 errors and I dont know what Im doning wrong.
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate our menu which can gather user input for switching camera
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.camera_menu, menu);
return true;
}
Im getting an error at "menu" in "R.menue..."
error 2:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.switch_cam:
// check for availability of multiple cameras
if (numberOfCameras == 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
/**builder.setMessage(this.getString(R.string.camera_alert))
.setNeutralButton("Close", null);*/
AlertDialog alert = builder.create();
alert.show();
return true;
}
Im getting an error on "id" in "case R.id..."
Thanks

First, do you have : <uses-permission android:name="android.permission.CAMERA" />in your AndroidManifest.xml ?
You can try to check a full Camera preview here for Android : Camera Preview Android
Another good website with source files : Using Camera API with SourceFile

You most likely just copied the source file you linked above into your project. You also have to add res/menu/camera_menu.xml. This defines the options menu that comes up when you press the menu button. See the menu doc for more information how that works if you're interested.
If you don't do that the tools notice that you miss a file that you reference in code but that's not actually there, which results the first error. The second error is also caused by this in an indirect way. The missing ID is also created within the menu file.

Related

Android how to open a menu / voice recognition

Android Studio here. I am trying to open the menu (the three dot one in the right upper corner) but without clicking it. I am using a voice recognition commands.
I already tried to call it in many ways, e.g. openOptionsMenu();, MapsActivityCurrentPlace.this.openOptionsMenu(); etc. but it didn't work, the menu doesn't open.
// This is my menu - current_place_menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.current_place_menu, menu);
//optionsMenu = menu;
return true;
}
// Later in the code, this is the place I want to open my menu by recognizing the "menu" command
private int voiceR() {
switch (OPERATOR) {
case 'M':
//getMenuInflater();
//openOptionsMenu();
// MapsActivityCurrentPlace.this.openOptionsMenu(); // activity's onCreateOptionsMenu gets called
// optionsMenu.performIdentifierAction(R.id.groupp, 0);
//optionsMenu.performIdentifierAction(R.menu.current_place_menu, 0);
//getMenuInflater();
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.current_place_menu, optionsMenu);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.current_place_menu, menu);
//Inflater.performIdentifierAction(R.id.groupp, 0);
//mShowMenu = true;
//invalidateOptionsMenu();
break;
case 'T':
break;
}
return -999;
}
Expected result would be the opened menu with an implemented list. For now, it recognizes the command but doesn't open the menu.
Thanks!
I tried with other stuff and still can't open the Menu.
Maybe my question was a bit twisted but does anyone know how to perform a click on menu without actually clicking it?
Ok, that's what helped finally:
switch (OPERATOR) {
case 'M':
optionsMenu.performIdentifierAction(R.id.groupp, 0);
break;
where groupp is the id of the menu.

MenuItemCompat.getActionView(menuItem) returns null

I am trying to get the View of the MenuItem.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
View miView = MenuItemCompat.getActionView(item);
if (miView == null) {
Log.e(X, "mView is null");
}
}
but everytime miView is null.
Here's my onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, R.string.Foo);
return true;
}
I know that MenuItemCompat returns null because the created Menu is not a from support library, so the MenuItem can't be handled by the MenuItemCompat class, isn't?
1) I am looking for some method like onCreateOptionsMenuCompat, is there any method like that?
2) How can i get ActionView from MenuItemCompat class?
and What am i doing wrong ?
PS: my project's minSdkVersion is 9
To get an ActionView from a menu item, you will need to set an ActionView on it first. Normal menu items do not come with ActionViews. ActionViews are used when you need to do something extra in your menu (outside of a normal icon and/or text).
Why are you trying to get a View from the menu item? What are you trying to do with your menu item?

How to call public boolean onPrepareOptionsMenu(Menu menu)

I am using Java and Android Studio. I need to change an image on the actionbar. What I need to do is to change the image I am displaying on the actionbar based on a reading received from a remote sensor. Therefore, I cannot use a clickable method but instead I have some code that activates when the data is received from the sensor. The code then analyzes the data and decides which of several images to display. The program then needs to update the actionbar image. This is the part I need your help on. I think I should use onPrepareOptionsMenu to update the image. However, I cannot seem to get the code correct to call onPrepareOptionsMenu. In particular, onPrepareOptions uses parameters (Menu menu). I cannot seem to specify the second menu properly as it always gives an error. The code below gives:
"required android.view.Menu found int". If I initialize menu with null I get a null reference error. Any suggestions how to correct my code or maybe do something totally different to be able to update the image? Thanks. Below is my java code.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); // Clear the menu first
Log.d(TAG, " Before inflation");
getMenuInflater().inflate(R.menu.gatt_services, menu);
Log.d(TAG, "Before the findItem");
menu.findItem(R.id.menu_batlevel).setIcon(R.drawable.low25);
return super.onPrepareOptionsMenu(menu);
}
Code to call the onPrepareOptionsMenu with:
...
else if (dataType == 2.0) {
Menu menu = (R.menu.gatt_services);
getMenuInflater().inflate(R.menu.gatt_services, menu);
onPrepareOptionsMenu(Menu menu);
...
XML code for the action bar. The image I am trying to change is item id/menu_batlevel:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_batlevel"
android:checkable="false"
android:icon="#drawable/discharged"
android:title="BatLev"
android:orderInCategory="2"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_connect"
android:title="#string/menu_connect"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_disconnect"
android:title="#string/menu_disconnect"
android:orderInCategory="101"
android:showAsAction="ifRoom|withText"/>
</menu>
My solution is shown below. I put the if statements that update the image into the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptions Before inflation");
getMenuInflater().inflate(R.menu.gatt_services, menu);
...
if (dataType == 2.0){
if (bat_level < 10) {
MenuItem batItem = menu.findItem(R.id.menu_batlevel);
batItem.setIcon(R.drawable.discharged);
}
...
I added a class:
class VersionHelper
{
static void refreshActionBarMenu(Activity activity)
{
activity.invalidateOptionsMenu();
}
}
I call the class from my program:
...
else if (dataType == 2.0) {
VersionHelper.refreshActionBarMenu(DeviceControlActivity.this);
...
The above code is working. As you can tell, I am new to java so if you see a problem with this code or have a better method, please let me know.
If you want to change the image on an existing menu item, you can save a reference to the menu item when it's created, something like this:
private MenuItem photoMenuItem;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_end_details, menu);
// find the photo menu and save it
photoMenuItem = menu.findItem(R.id.photoMenuItem);
}
Then when it's time to update the menu item, just call setIcon:
if (photoMenuItem != null)
{
photoMenuItem.setIcon(R.drawable.camera_active);
}
If you want to decide whether or not to show a particular menu item, you can call setVisible instead of setIcon on the items whose presence may change.

Is there a way to avoid having fullscreen SearchView/Keyboard on landscape?

I'm trying to avoid having fullscreen keyboard editing on landscape in order to access to the suggestions.
I already read a lot of threads explaining that I have to add EditorInfo flags like flagNoFullscreen and/or flagNoExtractUi.
I added them programmatically but not really helpful.
Is there a way to figure this out?
It took me a while to figure this one out, but it's actually quite simple.
Initially I created a custom class that extended the SearchView class, and used a onCreateInputConnection() override, however I couldn't get it working that way.
I eventually got it working in a much more simple way, with just two added lines of code.
You just need to call search.getImeOptions() to get the current configuration, and then "or" the result with EditorInfo.IME_FLAG_NO_EXTRACT_UI with a call to setImeOptions():
Java:
search.setImeOptions(options|EditorInfo.IME_FLAG_NO_EXTRACT_UI);
Kotlin:
search.setImeOptions(search.imeOptions or EditorInfo.IME_FLAG_NO_EXTRACT_UI)
If you don't "or" with the existing options, then you don't get the "Search" completion button in the lower right, you just get a "Done" button instead.
Here is the full onCreateOptionsMenu() override I used to test (I used a SearchView in the xml, but this solution should work for you even if you're not inflating your SearchView from xml):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchableInfo si = manager.getSearchableInfo(getComponentName());
//Here is where the magic happens:
int options = search.getImeOptions();
search.setImeOptions(options|EditorInfo.IME_FLAG_NO_EXTRACT_UI);
//!!!!!!!!!!!
search.setSearchableInfo(si);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
return true;
}
});
return true;
}
Here is the xml I used for the SearchView in menu_main.xml:
<item android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
Result without the call to setImeOptions():
Result with the call to setImeOptions():
Another alternative to the code is to add the property in the searchView xml, it is useful for APIs less than 16:
<SearchView
android:imeOptions="flagNoExtractUi" />
Same idea as above written but without SearchView, only using searchable XML
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
...
android:imeOptions="actionSearch|flagNoExtractUi"
...
</searchable>

android - sharing image inside imageview that gets changed with viewpager

I have a screen where images are swiped left and right using imageview and viewpager. It picks up images that are specified in the code (drawables in the project). I was wondering what would be the best way for the user to shars the images.
Would it be better to have a button that saves each image to card and shares it, or have a button that screenshots whatever is on the screen and shares it? I tried searching but found that the screenshot thing only works on rooted phones (which wouldn't work for my app) but those threads were from 2 years ago, so maybe things have changed RE: screenshotting programataically?
I'm not looking for code snippets, just want to know what the best strategy for this would be, without changing too much how my images are viewed.
According to the documentation, you want to look at creating a share intent as follows:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
You can then add an easy share action to your action bar which is what the user expects when browsing shareable images.
Create a menu resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
...
</menu>
Then inflate it in your onCreate and assign it some functionality using the Intent you created earlier:
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
I would steer away from screenshotting the image and creating a copy of data you already have available.

Categories

Resources