Action bar icon working fine on one activity not on other - android

I am using following menu.xml for action bar menu and its working fine on my main activity
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_drawer"
android:orderInCategory="0"
android:title="navigation"
app:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/settings"
android:orderInCategory="0"
android:title="Settings"
app:showAsAction="always">
</item>
But when I use same menu.xml on other activity menu icon doesn't appear. Here is a snapshot of my main activity
Now here is a snapshot of my other activity.
Why am I facing this problem, Please let me know your feedback on it.
Here is my onCreateOptionsMenu code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test,menu);
return true;
}
I am using this code for both activity.
Here is the code of my SecondActivity
package com.example.actionbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
#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.
int id = item.getItemId();
if (id == R.id.action_refresh) {
Toast.makeText(this, "navigation selected", Toast.LENGTH_SHORT)
.show();
}
if(id == R.id.action_settings){
Toast.makeText(this, "settings selected", Toast.LENGTH_SHORT)
.show();
}
return super.onOptionsItemSelected(item);
}
}

have you used onCreateOptionsMenu and onOptionsItemSelected method in the other activity if no then try it...

Related

Can not add two buttons in action bar Android

I am very new. I want to show two button and change their icons but I failed.
I don't understand the problem.
You can also use "always" to declare that an item always appear as an action button. I read it and used always but it didn' work.
menu_mainxml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_toggle_setting"
myapp:showAsAction="always"/>
<item android:id="#+id/action_about"
android:title="#string/action_about"
android:icon="#drawable/ic_action_toggle_about"
myapp:showAsAction="always"/>
MainActivityjava:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private MenuItem item1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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_main, menu);
item1= menu.findItem(R.id.action_about);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
//some code
return true;
}
if (id == R.id.action_about) {
//some code
return true;
}
return super.onOptionsItemSelected(item);
}}
My result:
Change the following in your activity:
private MenuItem item1;
#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_main, menu);
item1= menu.findItem(R.id.action_about);
return true;
}
Instead of Activity, try using AppCompatActivity.
MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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_main, menu);
return true;
}
#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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/ic_action_toggle_setting"
myapp:showAsAction="always"/>
<item android:id="#+id/action_about"
android:title="#string/action_about"
android:icon="#drawable/ic_action_toggle_about"
myapp:showAsAction="always"/>
you should use below code and remove myapp prefrex instead used android like below
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="always"
android:visible="true" />
</menu>

how to slide down the action bar in android to display something like coversation

i want to display the conversation between me and admin when i dragged the action bar. since i was new to android programming i dont know how to slide the action bar also. the following code is a decoy(dummy) code, coz its not letting me to post the question without sample code. please help me to drag or slide down the action bar to display conversation. thank you.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
{
}
}
#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_main, menu);
return true;
}
#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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Can't add action buttons on action bar

I want to add an ActionButton on the ActionBar, but it is placed on the overflow. What i'm doing wrong?
below is a code file and the main class
///--main_activity_actions.xml--///
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action buton -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom"/>
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
//--MainActivity.java--//
package com.example.my_first;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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.main_activity_actions, menu);
return true;
}
#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.
switch(item.getItemId()){
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSettings() {
// TODO Auto-generated method stub
}
private void openSearch() {
// TODO Auto-generated method stub
}
public void sendMessage(View view){
//Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
If you want an action to have its own icon on the ActionBar, try changing
android:showAsAction="ifRoom"
to
android:showAsAction="always"
Add this schema to your menu resource:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
Then change the showAsAction attribute:
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
Took me days to figure that out. Answer was here.

Can't create action buttons

I am currently trying to learn android programming and have encountered a problem. When I try to complete the Action Bar tutorial it does not work. It runs, but on the Action Bar only the overflow button diplays and that contains the settings button. I have no idea why this does not work. Here is the code:
package com.example.usingui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.usingui.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and res/menu/main.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
I have no idea what I am doing wrong. Please help me!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
//Do something here
return true;
case R.id.action_search:
//do something here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That is my code for my action bar
I see that in your XML for the menu, you have android:showAsAction="never" change it to always for each menu item that you want to see in the action bar so it would be this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="always" />
</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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true; ->you can handle every item of action bar and you can do any interaction in here!
}
return super.onOptionsItemSelected(item);
}
I am agree with Cybergei you should set visible of every item of action bar by writing android:showAsAction="always" and as I wrote above,you can do anything what you want by handling id of every item inside of the onOptionsItemSelected method

How do I create a clickable item in menu to open a new activity?

I have made an app with a lot of buttons and activities. I'm having trouble though understanding how to start a new activity through a button that is in my menu (when the menu button is clicked on phone) (inflatable menu). This is my code for the menu connected to my activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1" android:title="#string/menu_home"></item>
</menu>
Here is my activities in Java:
package com.gmail.derekcraigsmith.nanaimobus;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class Route1TimesCcMonfriAActivity extends Activity implements
OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route1_times_cc_monfri_a);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
return true;
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
}
int id = item.getItemId();
switch (id) {
case R.id.your_menu_item_id : {
startActivity(new Intent(start_activity, next_activity));
}
where start activity is your main activity and next_activity is the activity you want to start.
Give a look on my blog here for more info.
activity_main.xml in menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/next"
android:title="Next" />
</menu>
In your activity inflate the menu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) //get the id which is an int
{
case R.id.next: // check if its the menu item next selected
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
break;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources