Can't show a Menu Item that's been hidden in `onPrepareOptionsMenu` on a later moment - actionbarsherlock

I want to create a menu item and show it in a later moment, when an event occurs.. however, when I fire menuItemDone.setVisible(true); later on the code the menu item doesn't show. It stays hidden. Any idea of how I can create a hidden menu item and activate when an event occurs? The Menu is inflated in an activity and a what the fragment does:
#Override
public void onPrepareOptionsMenu(Menu menu) {
menuItemDone = menu.findItem(R.id.pi_menu_done);
if(some condition){
menuItemDone.setVisible(false);
}
}

This works for me..
public boolean onCreateOptionsMenu(Menu menu)
{
Menu m_menu = menu;
m_menu.add(Menu.NONE, Menu.FIRST+1, 0, "one");
m_menu.add(Menu.NONE, Menu.FIRST+2, 0, "two");
m_menu.add(Menu.NONE, Menu.FIRST+3, 0, "three");
m_menu.add(Menu.NONE, Menu.FIRST+4, 0, "four");
return super.onCreateOptionsMenu(menu);
}
public boolean onPrepareOptionsMenu(Menu menu)
{
Menu m_menu = menu;
if(bTested)
{
m_menu.findItem(Menu.FIRST+1).setVisible(false);
m_menu.findItem(Menu.FIRST+2).setVisible(true);
}
else
{
m_menu.findItem(Menu.FIRST+1).setVisible(true);
m_menu.findItem(Menu.FIRST+2).setVisible(false);
}
if(bConnected)
{
m_menu.findItem(Menu.FIRST+3).setVisible(false);
m_menu.findItem(Menu.FIRST+4).setVisible(true);
}
else
{
m_menu.findItem(Menu.FIRST+3).setVisible(true);
m_menu.findItem(Menu.FIRST+4).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}

Related

How to change the image button when it clicked in menu options

I am using image button in options menu and i am trying to change it on click. How can i change that image button when it gets clicked.
For example, in our music player pause and play button and one more subscribe and unsubscribe.
Any suggestions.
Thanks..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_edit, menu);
if (isEdit) {
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.drawable_done_all_black));
} else {
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_imagebutton_editprofile));
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
if (isEdit) {
updateProfile();
} else {
setEditable(true);
invalidateOptionsMenu();//do not forget to invalidate
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Add action on ObservableList item in javafx

I am using javafx to make a ListView in which add Observable List which contains buttons.
I want to add action on each buttons in this lists.Any help...
ObservableList videoLists = null;
if (listView.getSelectionModel().getSelectedItem().equals("class 8")) {
classTitleID.setText("class 8 video lists");
File physicsFolder = new File("D:\\videos\\physics");
File[] listOfFiles = physicsFolder.listFiles();
videoLists = FXCollections.observableArrayList();
for (File file : listOfFiles) {
if (file.isFile()) {
videoLists.add(new Button(file.getName()));
physicsListview.setItems(videoLists);
}
}
}
simply do
for (File file : listOfFiles) {
if (file.isFile()) {
Button button = new Button(file.getName());
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
//your action
}
});
videoLists.add(button);
physicsListview.setItems(videoLists);
}
}
It's generally a bad idea to have Node subclasses as the data type in ListViews (or TableViews, ComboBoxes, etc): it breaks MVC. Make your ListView a ListView<File> and use a cellFactory to show the button in the ListView cells. You can set the action handler there.
ListView<File> physicsListview = new ListView<>();
ObservableList<File> videoLists = FCollections.observableArrayList();
//...
for (File file : listOfFiles) {
if (file.isFile()) {
videoLists.add(file);
}
}
physicsListview.setItems(videoLists);
physicsListview.setCellFactory(new Callback<ListView<File>, ListCell<File>>() {
#Override
public ListCell<File> call(ListView<File>()) {
final Button button = new Button();
return new ListCell<File>() {
#Override
public void updateItem(final File item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
button.setText(item.getName());
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// handle action.
// You can access the File object item here if needed
}
});
setGraphic(button);
}
}
};
});
});

How to get Menu object with ActionBarCompat (showing progress bar in ActionBar)

I am switching from Sherlock to AppCompat, and I am used to do this thing where I replace refresh action item with a progress bar while something is loading like this
public void setRefreshButtonState(boolean refreshing) {
if (mOptionsMenu == null) {
return;
}
final MenuItem refreshItem = mOptionsMenu.findItem(R.id.action_refresh);
if (refreshItem != null) {
if (refreshing) {
refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
} else {
refreshItem.setActionView(null);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
mOptionsMenu = menu;
return super.onCreateOptionsMenu(menu);
}
But since Menu is the regular menu in AppCompat (unlike Sherlock), it obviously throws
Call requires API level 11 (current min is 9): android.view.MenuItem#setActionView
I tried all the MenuItemCompat static methods, but no luck
Thanks!
I was searching for the wrong thing. You dont want to comapt menu object but the menu item, like this
MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);
public void setActionItemInProgress(MenuItem menuItem, boolean refreshing) {
if (refreshing) {
MenuItemCompat.setActionView(menuItem, R.layout.actionbar_indeterminate_progress);
} else {
MenuItemCompat.setActionView(menuItem, null);
}
}

Display animatable instead of text on a button when clicked

I am trying to build a button with a progress bar which is displayed in the center of the button and instead of text, as soon as the button is tapped, and while some background processing takes place.
What I am doing is to have a custom button and set the animatable as a drawable left to the button, and in onDraw to move the animatable in the center of the button. However, for a short period of time after the button is tapped, I do not know why there are 2 progress bars on the button.
public class ButtonWithProgressBar extends Button {
protected int progressId = R.drawable.progress_bar;
protected Drawable progress;
protected CharSequence buttonText;
#Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if (Strings.notEmpty(text)) {
buttonText = text;
}
}
public void startSpinning()
{
if(isSpinning()){
return;
}
progress = getContext().getResources().getDrawable(progressId);
final Drawable[] old = getCompoundDrawables();
setCompoundDrawablesWithIntrinsicBounds(old[0], old[1], progress, old[3]);
setEnabled(false);
if(progress instanceof Animatable){
((Animatable)progress).start();
}
setText("");
}
public void stopSpinning()
{
if(!isSpinning()){
return;
}
if(progress instanceof Animatable){
((Animatable)progress).stop();
}
setEnabled(true);
setText(buttonText);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (progress != null) {
final int drawableHeight = progress.getIntrinsicHeight();
final int drawableWidth = progress.getIntrinsicWidth();
final int drawableTop = (getHeight() - drawableHeight) / 2;
final int drawableLeft = (getWidth() - drawableWidth) / 2;
progress.setBounds(drawableLeft, drawableTop, drawableLeft + drawableWidth, drawableTop + drawableHeight);
progress.draw(canvas);
}
}
}
And this is displayed for a short period after tap, before everything looking as I want:
Any ideas why this is happening?
Found the problem.Updated code to a working version.

How do i change the JavaFX TableCell color when it is selected?

Well, I created a custom TableCell for my TableView.
This custom TableCell contains a Link and opens the browser when clicked.
Everything is working fine, what I want to do is change the text color of this TableCell when it is selected...
This is what I am trying to do:
callback = new Callback<TableColumn, TableCell>(){
#Override
public TableCell call(TableColumn param) {
return new TableCell<Test, String>(){
EventHandler handler = new EventHandler<MouseEvent>() {
final AM_RSS_FX RSS = AM_RSS_FX.this;
#Override
public void handle(MouseEvent param) {
try {
java.awt.Desktop.getDesktop().browse(new URI(RSS.link));
} catch (IOException | URISyntaxException ex) {
Logger.getLogger(AM_RSS_FX.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
#Override
public void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if(!isEmpty()){
final AM_RSS_FX RSS = AM_RSS_FX.this;
this.setTextFill(Color.BLUE);
setText(item);
RSS.link = this.getText();
this.addEventHandler(MouseEvent.MOUSE_CLICKED, handler);
}
}
#Override
public void updateSelected(boolean arg0){
super.updateSelected(arg0);
if(isSelected()){
this.setTextFill(Color.AQUA);
}
}
};
}
};
I don't know which method i need to Override =/
I tried to Override the updateSelected, but didn't worked =/
Can someone help me?
1- You are adding a mouse event handler on TableCell instance and that event is firing when you click on it. However the table cell is still not being selected. Instead the table row cell selection is being fired. To enable cell selection do:
table.getSelectionModel().setCellSelectionEnabled(true);
2- No need to override updateSelected() to manage style, instead use CSS selectors from caspian.css:
.table-cell:selected {
-fx-background-color: lightgreen;
-fx-text-fill: green;
}

Resources