I have a GLSurfaceView, when the user click a button, it will draw some triangles with default color.
Then when the user selects a color from my inflater menu, it will re-draw the triangles with that particular color.
It works in a weird way, at first it does draw the triangles, and when i select a different color, it doesn't repaint with different color. However, when i turn off my phone and turn back on, the color changed.
Here is my code:
//------------------------------------------------------------------------------------------------
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.issm_menu,menu);
return true;
}
//------------------------------------------------------------------------------------------------
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_about:
Toast.makeText(ISSM.this, "ISSM Application", Toast.LENGTH_SHORT).show();
return true;
case R.id.cl_autumn:
colorMap.setAutumn();
drawFigure();
return true;
case R.id.cl_bone:
colorMap.setBone();
drawFigure();
return true;
default:
return true;
}
public void onClick(View view)
{
this.fillBuffer();
drawFigure();
}
//----------------------------------------------------------------------------------
public void drawFigure()
{
mGLView = new MyGLSurfaceView(this, buff, size, colorMap);;
frame.addView(mGLView);
}
I want that as soon as i hit the color in my menu, the color should change, i do not want to turn off and turn on (by the way, not completely power off, just like sleep and wake up)
Solved it, I need to called frame.removeView() first then add another view.
Related
Toolbar menu image
This is the image of my toolbar Menu there are 5 icons i.e backward, forward, reload, moon symbol, and sun symbol.
now I want to add functionality to moon and sun icon i.e when the moon icon is clicked app theme changes to night mode and when sun icon is clicked the app theme. changes to day mode.
*This is the code i have written for backward, forward and reload but now what should. i write for night mode and daymode for android app
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnecttion();
break;
}
return super.onOptionsItemSelected(item);
}
do something like this:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
//Get the night mode state of the app
int nightMode = AppCompatDelegate.getDefaultNightMode();
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnecttion();
break;
case R.id.moon:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.sun:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
}
// Recreate the activity for the theme change to take effect.
recreate();
return super.onOptionsItemSelected(item);
}
I suggest you to take a look at this link: LINK
It describe very well a different way to solve your problem but of course you can modify it according to your needs
I'm writing Notepad App in which I've got slider menu showing some text format panel. I toggle view of this panel when user tries to select some text, so I've implemented my menu-toggling code into my EditText's setCustomSelectionActionModeCallback() which looks like this:
private void manageContextMenuBar(EditText editText) {
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
// There menu is hidden
public void onDestroyActionMode(ActionMode mode) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.VISIBLE) {
findViewById(R.id.sliderMenu).setVisibility(View.GONE);
}
}
// There menu shows up
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.GONE) {
findViewById(R.id.sliderMenu).setVisibility(View.VISIBLE);
}
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
});
}
When I long click on text my format menu shows up, and also software context menu with paste/copy/cut button on it.
The problem is that because of my "Overriding" context menu functions, they stopped working. I can click the buttons, but they doesn't work.
I hope You will understand my problem
Any help will be appreciated :)
You should return false from onActionItemClicked method. This way when you click on those menu items Android uses the default actions.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
From the onActionItemClicked method Documentation:
Returns: true if this callback handled the event, false if the standard MenuItem invocation should continue.
In my ActionBar, I have a MenuItem that has attribute showAsAction="always" as seen in the image below. Based on the connection a user has to our servers, I will be changing the text as well as color of the item.
Currently, I am able to change the text of the item very easily in onPrepareOptionsMenu(...):
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_connection);
if(mIsConnected) {
item.setTitle(R.string.action_connected);
} else {
item.setTitle(R.string.action_not_connected);
}
return super.onPrepareOptionsMenu(menu);
}
This works great and if possible, I would like to change the color of the text here as well. I've seen many posts about how to change the text of ALL the overflow items or the title to the ActionBar itself but nothing about changing an individual action item PROGRAMMATICALLY. The current color is set in xml, I want to change it dynamically.
Well, each MenuItem View is actually a subclass of TextView, so this will make changing the text color easier.
A simple method you can use to locate a MenuItem View is View.findViewsWithText.
A basic implementation, considering you just have that one MenuItem you're interested in changing, might look something like this:
private final ArrayList<View> mMenuItems = Lists.newArrayList();
private boolean mIsConnected;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Add a your MenuItem
menu.add("Connected").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
// Adjust the text color based on the connection
final TextView connected = !mMenuItems.isEmpty() ? (TextView) mMenuItems.get(0) : null;
if (connected != null) {
connected.setTextColor(mIsConnected ? Color.GREEN : Color.RED);
} else {
// Find the "Connected" MenuItem View
final View decor = getWindow().getDecorView();
decor.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mIsConnected = true;
// Remove the previously installed OnGlobalLayoutListener
decor.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Traverse the decor hierarchy to locate the MenuItem
decor.findViewsWithText(mMenuItems, "Connected",
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
// Invalidate the options menu to display the new text color
invalidateOptionsMenu();
}
});
}
return true;
}
Results
I'm using sherlock action bar.
I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.
This is my code on Java
#Override
public boolean onPrepareOptionsMenu (Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menutes, menu);
todaySched=menu.findItem(R.id.todaySched);
if(todaySched.isEnabled()){
todaySched.setIcon(R.drawable.calendarselected);
}
return true;
}
but when I do this the icon become double, and the icon won't change neither.
Can someone help?
Use the on onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.todaySched:
// put your code here to change the icon
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
I'm trying to get my options menu to show up in the new Android 3.0 system bar. I can get this behavior to work with the Notepadv3 tutorial. However, when implementing nearly the identical functions, the menu shows in the above action bar, but not the system bar. Any ideas?
Attached is the appropriate code for my application:
public class AACUser extends Activity {
private static final int ADMIN_ID = Menu.FIRST;
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADMIN_ID, 0, R.string.menu_admin);
return true;
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case ADMIN_ID:
enterAdminMode();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
}
I've also tried implementing these menu functions as recomended by the creating menu documentation:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_adminMode:
enterAdminMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
but no difference: it still displays the option in the overflow of the action bar, but not the system bar.
As of Android 3.0, the options menu is not required (if not deprecated). If you target API 11 (Android 3.0) or later, it is assumed that your app will work without an options menu, so the system bar will not display one. The options menu in the system bar is only there for backwards compatibility with old applications. New applications should have another affordance for accessing options, such as that provided by the action bar.
I think that the menu button in the system bar is for backward compatibility purposes only. So if you want to design your app for Honeycomb then the menu should be and will be in the action bar.