I have created my dialog when I press one of the menu buttons:
#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_first__window, 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();
switch (id)
{
case R.id.action_settings:
About_us aboutus = new About_us(this);
aboutus.show();
return true;
case R.id.close: System.exit(0); return true;
}
return super.onOptionsItemSelected(item);
}
My Dialog layout
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Close"
android:onClick="ext_btn"/>
My Dialog Class
public class mydialog extends Dialog
{
public About_us(Context context) {
super(context);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about_us);
}
public void ext_btn(View view) {
About_us.this.dismiss();
}
}
I Have tried a lot of codes to make the close button dismiss the dialog.
My app always crashes.
Where is the mistake ?
Try this one.
Button btn_ext = (Button) findViewById(R.id.btn_ext);
btn_ext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
I have an ActionBarActivity and one Fragment. The Activity has no menu inflated, while the Fragment has a menu with two buttons. Fragment menu is visible, but the buttons don't react at all when tapped. At debugging I can see that both onCreateOptionsMenu() for Fragment and Activity get called, but when tapping buttons no onOptionsItemSelected() gets called, neither from Activity nor from Fragment.
Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainActivity = (NavigationActivity)getActivity();
setHasOptionsMenu(true);
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState){
return (ScrollView) inflater.inflate(R.layout.tutoring_detail, container, false);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.query_details_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.accept_query:
respondToQuery(true);
return true;
case R.id.decline_query:
respondToQuery(false);
return true;
default:
break;
}
return false;
}
Menu to be displayed in Fragment
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<item
android:id="#+id/accept_query"
android:orderInCategory="100"
app:showAsAction="always"
android:checkable="true"
style="?android:attr/borderlessButtonStyle"
app:actionViewClass="android.widget.ImageButton"/>
<item
android:id="#+id/decline_query"
android:orderInCategory="101"
app:showAsAction="always"
android:checkable="true"
style="?android:attr/borderlessButtonStyle"
app:actionViewClass="android.widget.ImageButton"/>
</menu>
In the Activity class,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
return false;
}
In the Fragment,
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "called " + item.getItemId(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
You must use super.onOptionsItemSelected(item) in the parent activity's onOptionsItemSelected(...) method.
From Activity | Android Developers:
Derived classes should call through to the base class for it to perform the default menu handling.
Try moving setHasOptionsMenu(true) inside of the onCreateView() method in your Fragment instead of onCreate().
I had a similar issue after making special layout for my action button (I made an Image Button and needed to pad it and change some other things so I had to use layout for it).
Then onOptionsItemSelected lost connection with this imageButton so I just use clickListener for it inside onCreateOptionsMenu. It might not be the best practice, maybe there is a better solution, but this is what solve my problem.
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.picker_list_menu, menu);
MenuItem itemDone = menu.findItem(R.id.menu_done);
MenuItemCompat.setActionView(itemDone, R.layout.menu_layout_done);
menuDoneIB = (ImageButton) MenuItemCompat.getActionView(itemDone);
itemDone.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code..
}
});
}
Try using oncreateview
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
If you have dynamically changed menu item, for instance, a badge menu (https://stackoverflow.com/a/16648170/2914140 or https://stackoverflow.com/a/26017587/2914140), you should initialize the item in onCreateOptionsMenu and re-set setOnClickListeners after every change of the item.
In my case:
private MenuItem menuItem;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_catalog, menu);
menuItem = menu.findItem(R.id.action_badge);
writeBadge(0);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_badge) {
// Your code.
return true;
}
return super.onOptionsItemSelected(item);
}
private void writeBadge(int count) {
MenuItemCompat.setActionView(menuItem, R.layout.item_badge);
RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(menuItem);
// A TextView with number.
TextView tv = (TextView) layout.findViewById(R.id.badge);
if (count == 0) {
tv.setVisibility(View.INVISIBLE);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(String.valueOf(count));
}
// An icon, it also must be clicked.
ImageView imageView = (ImageView) layout.findViewById(R.id.image);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
};
menuItem.getActionView().setOnClickListener(onClickListener);
imageView.setOnClickListener(onClickListener);
}
I am passing the click event to fragment from the activity. This worked for me.
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
return getSupportFragmentManager().getFragments().get(getSupportFragmentManager().getFragments().size() - 1)
.onOptionsItemSelected(item);
}
I have a MainActivity on that I have a navigation drawer,Lists of navigation drawers are fragments.
I want to have refresh button on one of my fragment,to add refresh button on action bar i added following code in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.action_settings) {
return true;
} else if(item.getItemId() == R.id.action_refresh) {
if(haveNetworkConnection(getApplicationContext()))
{
LoadAllProducts task = new LoadAllProducts(ListDepartmentSemesterActivity.this);
task.execute();
}else{
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
But the thing is on every fragment refresh button is visible and from any fragments i can refresh products,how to hide it on other fragments???
Navigation drawer MainActivity and Fragments share same action bar???
Define onOptionsItemSelected(MenuItem item) in fragment and add your refresh button to it. And insert this in your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setRetainInstance(true);
}
if you want handle menu button in each fragment try this code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.simple_calendar_view,
container, false);
// you code here
setHasOptionsMenu(true);
return rootView;
}
inside same fragment you want handle its menu item clicks
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
// you Custom code here
// Example i make method called Refresh_event
Refresh_event();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
if you need hide some menu item when you open slide menu navigation
in you activity that host fragments
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action item(About App && Refresh)
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.about_app).setVisible(!drawerOpen);
menu.findItem(R.id.refresh).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
I don't understand why in my activity I don't see the refresh button on the action bar.
Here's my code
public class CommentsActivity extends Activity {
String linkcommenti, cleanedLink;
WebView webview;
private ProgressBar loadingProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.bar_back));
actionbar.setDisplayHomeAsUpEnabled(true);
linkcommenti = (String) getIntent().getExtras().get("linkcommenti");
webview = (WebView) findViewById(R.id.webview);
loadingProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
loadingProgressBar.setVisibility(View.VISIBLE);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view,
final String url) {
webview.setVisibility(View.INVISIBLE);
loadingProgressBar.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
public void run() {
final CleanHtml toClean = new CleanHtml(url);
runOnUiThread(new Runnable() {
public void run() {
webview.loadDataWithBaseURL(null,
toClean.url_cleaned, "text/html",
"charset=UTF-8", null);
webview.setVisibility(View.VISIBLE);
loadingProgressBar.setVisibility(View.GONE);
}
});
}
}).start();
return true;
}
});
new Thread(new Runnable() {
public void run() {
final CleanHtml toClean = new CleanHtml(linkcommenti);
runOnUiThread(new Runnable() {
public void run() {
webview.loadDataWithBaseURL(null, toClean.url_cleaned,
"text/html", "charset=UTF-8", null);
loadingProgressBar.setVisibility(View.GONE);
}
});
}
}).start();
}
#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.comments, 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.
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_refresh:
reload();
return true;
}
return super.onOptionsItemSelected(item);
}
And here's the resource R.menu.comments
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.intertiamo.CommentsActivity" >
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
app:showAsAction="ifRoom" />
</menu>
I can't really understand, I call the resource from another activity and I see the refresh button there...the code doesn't return any error.
And the button "android.R.id.home" IS displayed!
Thanks
Try:
app:showAsAction="always"
If that doesn't work (ie as indicated by the comments, if you don't extend ActionBarActivity and extend Activity instead), try this as well:
android:showAsAction="always"
I'm assuming your ActionButton is displaying in the overflow currently.
i want to cast Menuitem to button so i can set listener and perform action
i have tried with custom Actionbar where i set imagebutton and perform a task
can anyone have please share it here
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getSupportMenuInflater() if you are using sherlock action bar
getMenuInflater().inflate(R.menu.urmenuxml, menu);
MenuItem mi = menu.getItem(0);
View anyView = new View();
anyView.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
mi.setActionView(anyView);
}
I hope this helps.