I have a basic project and question. My start-up code is MainActivity.java, and menu belonging to it is res/menu/main.xml. Here is the content of it:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_add"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/menu_add_planet"/>
<item
android:id="#+id/menu_config"
android:orderInCategory="105"
android:showAsAction="never"
android:title="#string/menu_config_planet"/>
When user clicks (or taps) on the add planet option on the menu, and I want the class to be activated.That class is NewPlanet.java Here is the content of it:
import android.app.Activity;
import android.os.Bundle;
public class NewPlanet extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
}
I anticipate that there must be a binding between the item element in the res/menu/main.xml and newPlanet but how?
Thanks in advance.
In your activity from where user will click the add planet option have the following:
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch(item.getItemId()) {
case R.id.menu_add:
intent = new Intent(this, NewPlanet.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.startActivity(intent);
break;
}
return true;
}
Make sure you have the Action bar. You can show it by having getActionBar().show(); in onCreate(). Also make sure you are not having both title bar and action bar at the same time.
Related
I have the problem, I use icon search on Toolbar, I want to click this icon search, it will move to new activity and expand editview search in here, but my code can not achive
My class (First Activity):
#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_icon_toolbar, menu);
// Retrieve the SearchView and plug it into SearchManager
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(item.getItemId()==R.id.action_search){
Intent i = new Intent(this,SearchCarActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
my layout_activity:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- NavigationDrawer Menu -->
<item android:id="#+id/action_search"
android:title="Search"
app:showAsAction="always"
android:icon="#drawable/ic_action_name_search"
app:actionViewClass="android.support.v7.widget.SearchView"
/>
</menu>
My Search activity (Second activity):
public class SearchCarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_car_menu, menu);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setIconified(false);
return super.onCreateOptionsMenu(menu);
}
}
My search_car_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"/>
It does't move to second activity, it seem still achive in first activity
How to I can achive move to second activity when clicked icon search on Toolbar?
Thanks
I created a test project an added your code
from what you said you want to click on the search icon on the first activity and then open second activity and do your search there
removing this line from your layout_activity did the trick :
app:actionViewClass="android.support.v7.widget.SearchView"
when you added that line your item would behave as a SearchView and not even respond to
public boolean onOptionsItemSelected(MenuItem item)
One Solution is to do it in same activity.
Set listener for input query text
searchView.setOnQueryTextListener(MyActivity.this);
and implement MyActivity with this interface SearchView.OnQueryTextListener
This interface provides 2 callback methods:
#Override
public boolean onQueryTextSubmit(String query) {
//get this query and search on server
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Hope this helps you.
I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto=" >
<item android:id="#+id/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.
My code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
}
This works to open the settings menu (Preferences.class) but I would like it to close the menu if it's already open when this system button is pressed. What can I add or change to make that happen?
Here is how you should be implementing what you want:
First thing first, you want to supply a "Settings" item that can selected by the user to get to your Preferences class. You do that by adding an item to your menu/main.xml and inflating it in onCreateOptionsMenu().
menu/main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Settings"/>
...
</menu>
Inside your Main Activity, override this to inflate your main.xml file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
...
return super.onCreateOptionsMenu(menu);
}
And then override this to handle what happens when a Menu item is selected, in this case the Settings item:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
startActivity(settingsActivity);
return true;
...
default:
return super.onOptionsItemSelected(item);
}
}
I was able to get the menu to close by adding this to my customized preferences menu class:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
onBackPressed();
return false;
}
which calls this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
which takes the user back to the screen to play another round. I don't know if this is an acceptable solution for every case but it worked for me.
I am a newbie to android and I was wondering if someone could guide me about how to reuse the action bar in all of my android activities. As far as I have explored, I found out that we have to make a BaseActivity class and extend it in our Activity where we want to reuse it, and also we have to make a xml layout and include it in our activity xml file. I have finished with the BaseActivity part. Now I am sort of confused in framing the xml part and including it. I know how to merge and include a layout, But in case of Action Bar, what necessary steps are to be taken. Any help would be appreciated.
This is my BaseMenuActivity:
public class BaseMenuActivity extends Activity{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setIcon(R.drawable.ic_social_share);
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.apptitle, null);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
}
Manifest part for the same:
<activity
android:name="com.example.travelplanner.MenuActivity"
android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden"
android:uiOptions="splitActionBarWhenNarrow"
android:label="WeTrip"
android:theme="#style/MyTheme" >
Style.xml part:
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#F0F1F1</item>
<item name="android:backgroundSplit">#000000</item>
</style>
MenuActivity.java
public class MenuActivity extends BaseMenuActivity implements OnItemClickListener{
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_menu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_action_search:
{}
case R.id.menu_action_locate:
{}
case R.id.menu_action_mail:
{}
case R.id.menu_action_call:
{}
}
return super.onOptionsItemSelected(item);
}
}
Well Your code looks good, but if you want to reuse exactly the same ActionBar with the same icons and menus and generally the same functionality in every activity.
You could add the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_action_search:
{}
case R.id.menu_action_locate:
{}
case R.id.menu_action_mail:
{}
case R.id.menu_action_call:
{}
}
return super.onOptionsItemSelected(item);
}
in your BaseMenuActivity class and your actionbar will be populated the same for every activity that extends from it.
Update:
To create a menu layout you should create a folder 'menu' in your resources folder res/menu.
Then create a xml file inside called : some_title.xml
A typical example of a menu xml file is like below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="#drawable/abs__ic_search"
android:showAsAction="ifRoom|withText|collapseActionView"
android:title="#string/menu_action_search"/>
<item
android:id="#+id/menu_sort"
android:icon="#drawable/content_sort_icon"
android:showAsAction="always"
android:title="#string/menu_action_sort">
</item>
</menu>
and then inflate that 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.some_title, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
return true;
}
For some more reading this tutorial is very very good on using ActionBar:
http://www.vogella.com/tutorials/AndroidActionBar/article.html
Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.