I have a little problem with action bar item.
I want to create clicable item in action bar which opens new java activity.
ab_button_desktop is the custom button which I created.
I tried a lot java codes from tutorials and I havent still any resoults.
Could anyone recommend me java code which opens new activity.
This is my xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:webviewapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/ab_button_desktop"
android:title="#string/ab_button_desktop"
android:icon="#drawable/ic_menu_desktop"
android:orderInCategory="1"
android:onClick="desktop"
webviewapp:showAsAction="always" />
<item
android:id="#+id/ab_button_share"
android:title="#string/ab_button_share"
android:icon="#drawable/ic_menu_share"
android:orderInCategory="1"
webviewapp:showAsAction="ifRoom" />
</menu>
By adding android:onClick="desktop" you are telling Android the name of a function within your Activity to call when your button is clicked. This function has to follow few rules though. It must have a return type of void, take only one argument which is a View. To open a new Activity you would have to add the following code to your Activity where the button currently is:
public void desktop(View v){
//handle the click here
}
Lets say you create one called InformationActivity. To launch it from example button click, you would do this:
public void desktop(View v){
startActivity(new Intent(this, InformationActivity.class);
}
For more details, read about Activities and UI in the Android dev guide.
Original Answer: Android Button to open xml layout
You can handle clicks on menu items by overriding OnOpionsItemSelectd:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getId()){
case R.id.ab_button_desktop:
//Do something
break;
case R.id.ab_button_share:
//Do something else
break;
}
return super.onOptionsItemSelected(item);
}
For a tutorial on how to launch activities, click here.
Related
I'm making an Android app that allows the user to search a collection of information. I begin by displaying the collection, but when a search button in the actionbar is tapped, an action layout (search bar) shows in the action bar. When a search is conducted, the listview information is replaced with information from the query. However, when a user taps the back arrow button next to the search bar, I would like to replace the queried information with the original information.
Is there any way to detect when the back arrow button is tapped next to the search bar? Thank you very much for your help!
Menu XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="">
<item
android:id="#+id/search"
android:title="Search"
android:showAsAction="always|collapseActionView"
android:actionLayout="#layout/some_layout"
/>
</menu>
Activity Code: trying this: SearchView onMenuItemActionCollapse Not Working
but keep getting a Null Pointer Exception
There are some ways to detect it indeed. First thing that came up to me was the following: create a boolean with false as default value. Whenever you query for specific search results, you turn the boolean to the true. When the 'back arrow' (navigation up) button has been pressed, you check whether the boolean turns out to be true. If it's true, it means that the app doesn't show you the original information set and shouldn't close your activity.
For example (from Google docs, partly.):
public boolean originalContentReplaced = false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
if(originalContentReplaced) {
// Do what you need to do here to get the original content back.
this.originalContentReplaced = false;
} else {
NavUtils.navigateUpFromSameTask(this);
// You could also use onBackPressed().
}
return true;
}
return super.onOptionsItemSelected(item);
}
I don't know how you query your new search results but when your search is successful, you should change originalContentReplaced variable to true.
You could use the very same if-else statement when you press on the hardware back button. Anyway, this should give you a very clear idea on how to detect when the navigation up/back arrow button is pressed. :)
I am new to developing Android apps.
I would like to add a settings option menu. The requirement for the app is that when you click settings in the option menu the screen will show a sentence Add beep after ___ counts.
How can I do that, please?
Its on the OnCreateOptions and OnOptionsItemSelected, Plus you need to manipulate the menu folder / menu xml also. Try to read about them and instantiating them is as easy as a switch case, as well as for the functions.
You will need a separate activity/fragment to handle Add beep after ___ counts
Here is the flow, read a little bit more on these. You can find lot of examples on each of these:
Create Main Activity with a menu.xml that has a settings action
Create another activity or a dialog fragment with a layout of
<textview> <edittext> <textview>. This is to show the Add beep
after ___ counts. In MainActivity, on menu action "settings", show this fragment.
You can save the count persistently using preferences
Hope this helps.
mainmenu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:title="#string/settings_label"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Toast.makeText(context, "Add beep after ___ counts.", duration).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
For more information read this Menu
I've Googled and come up with no code on how to manipulate what the settings button does, or what is brought up when the settings touch button on the phone is pressed.
Here is a screenshot of what I mean:
At the bottom, you can see that it says settings, but when I click on it, nothing happens. I want to change what happens when it is clicked.
Also, I want to be able to change what is brought up (the settings option in the screenshot is what's brought up) when the settings button on the actual phone is pressed. eg. on the S3, it's the touch button at the bottom left of your device.
Can anyone link me to a resource, or write some sample code for how to do both, or even one of the things I want to do?
You need to override your Activity's onOptionsItemSelected(...) method.
Do it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) { // or whatever your id is, see the .xml file
// do something
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
Check the id of the options item (e.g. settings) and do specific stuff inside the if.
In order to add more items or manipulate the Id, take a look at your projects "menu" folder and the .xml file inside it.
Here the .xml file of my main menu:
And how it looks inside:
(you can manipulate this file and add more actions to it that will show up in the menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_one" android:title="Action 1"></item>
<item android:id="#+id/action_two" android:title="Action 2"></item>
<item android:id="#+id/action_three" android:title="Action 3"></item>
</menu>
In this case my menu would have 3 Actions, and you should check for them inside your onOptionsItemSelected(...) method like that:
if (item.getItemId() == R.id.action_one) //...
... or use switch-case.
For a more detailed explanation, you can also have a look at this tutorial:
http://mobile.tutsplus.com/tutorials/android/android-sdk-implement-an-options-menu/
I am new to this android.In my application when user clicks Menu button ,i want to show 1 option at the bottom of the screen(background should remain as the current screen).Simply i have to say means if user clicks menu button as like pop up window 1 option should come at the
bottom of the screen.And user clicks on that some action should happen..I can t able put some screenshots for this.
----------------------
------------>Consider this is screen
---------------------
------------>here the option should come.
----------------------
Please help me.Thanks in advance.
Menu is generally used to give extra functionality to an application.
To achieve your goal you have to implement menu, which will open when you click menu button of device, like below.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.optionmenu, menu);
return super.onCreateOptionsMenu(menu);
}
R.menu#
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/Color" android:title="Color">
<menu>
<item android:id="#+id/RedColor" android:title="Red"/>
<item android:id="#+id/GreenColor" android:title="Green"/>
</menu>
</item>
</menu>
and if you want to do any action on click of option menu, you have to override onOptionsItemSelected to do action on click of menu option, like below.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.RedColor)
{
Toast.makeText(MenuOptionDemoActivity.this,"Red Color Selected" ,1000).show();
}
else if(item.getItemId()==R.id.GreenColor)
{
Toast.makeText(MenuOptionDemoActivity.this, "Green Color Selected", 1000).show();
}
return super.onOptionsItemSelected(item);
}
}
A clear and simple tutorial to implement menu in your app.
That can be done, but the solution is too long for me to post here, so instead follow the link to a tutorial for creating a menu
http://www.androidhive.info/2011/09/how-to-create-android-menus/
Just as a brief overview of the tutorial - You start by overriding the onCreateOptionsMenu method. In this you inflate your menu. Then you override the onItemSelected method, in which you pu a switch to determine which option phase been selected, though in your case it will only be one item. Finally you have to create an XML file which contains all the options for your menu.
This topic can look similar to others but I haven't found any usable answer for this case.
Here is what I want : I have a TabActivity with a menu, containing tabs without any menu. When I press the menu button, I want the only existing menu to be displayed. If I only inflate the menu, this is working fine. But if I want to change the content of the menu (change the visibility of items in the onPrepareOptionsMenu(menu) method) or even press the items of the menu, nothing works.
For the onPrepareOptionsMenu(menu), the problem seems to be coming from the menu.findItem(...) method which returns a null Object, and I have a NullPointerException !
In onOptionsItemSelected(item), none of the item IDs will be recognized.
I have noticed with the debugger that the menu Context is the activity of the current tab, so I have moved the menu inside of that activity instead, but without more success.
Last thing, I was using the same menu and very similar code in a previous version of the app, using a single activity (no tabs) and I didn't encounter any problem. When I moved to the TabActivity design it was first working fine (maybe the context of the menu was my TabActivity instead of the Activity of the tab), but it didn't work anymore after minor changes to the Activities of the tabs (nothing related to any menu).
If you think using ActionBar (and the support package) would fix this, I am already planning in moving to it later, but I would like to understand and fix this first.
Here is the code :
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem connect1 = menu.findItem(R.id.connect_1);
MenuItem connect2 = menu.findItem(R.id.connect_2);
// Here I do some stuff to prepare the menu, which could be simplified
// like this :
if (device1Connected)
if (!connect1.isVisible()) // here I get the NullPointerException
connect1.setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case R.id.select_device:
// do some stuff
return true;
case R.id.connect_1:
// do some stuff
return true;
case R.id.connect_2:
// do some stuff
return true;
case R.id.disconnect:
// do some stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here is the XML file of the menu, nothing special
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/select_device"
android:icon="#drawable/icon"
android:title="#string/choose_devices"/>
<item android:id="#+id/connect_1"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_1" />
<item android:id="#+id/connect_2"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/connect_2" />
<item android:id="#+id/disconnect"
android:icon="#android:drawable/ic_menu_close_clear_cancel"
android:title="#string/disconnect" />
</menu>
Edit : I have added the following in the onPrepareOptionsMenu(menu) to avoid the NullPointerException. It doesn't fix the real problem but I can now see precisely what happens when I click on a MenuItem
if (connect1 == null || connect2 == null)
return super.onPrepareOptionsMenu(menu);
When I click on the first item, getItemId() returns, for example, 2131165207, and the second item returns 2131165208 (I have checked, they are ids of Views of the second tab !!), but the values it should return to enter the switch/case are respectively 2131165215 and 2131165216, so as I said before, I have a problem with my item ids. I made this test with the menu within the Activity of the first tab, because the mContext value of the menu is always an instance of the current Activity. But even though the Context AND the Activity containing the menu are the same, it still doesn't work.
So ! I have finally figured out what was wrong. It was a building/compiling error. I have been trying before refreshing the project many time, but it was not enough, I had also tried deleting the R.java file (where are compiling the ID numbers from the XML files), but it still didn't work; and I finally remembered about the Project -> Clear... function in Eclipse while working on another project.
And it just worked. So that was nothing there very challenging, but at least I've learnt something valuable today.