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

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);
}
}

Related

Xamarin Button Command with Keyboard Open

I am working on an Xamarin.Forms project specifically for the iOS platform. I have an Editor control and a Button control next to each other. When I focus the editor, enter some text, and click the button it appears the command is not being fired but rather the keyboard is simply closing. I then have to tap the add button again for the command to be fired.
<StackLayout Orientation="Horizontal">
<Editor HorizontalOptions="FillAndExpand"
Text="{Binding EditorText}"/>
<Button Text="Add"
Command="{Binding AddCommand}"/>
</StackLayout>
I have tried creating a custom renderer that prevents the keyboard from closing initially and then close it after a delay. That allows the command to be fired, but I am stuck with the keyboard being open.
public class KeyboardEditorRenderer : EditorRenderer
{
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
if (Control != null)
{
Control.ShouldEndEditing = (UITextView textField) =>
{
Task.Delay(10).ContinueWith(_ =>
{
// THIS DOES NOT WORK
textField.EndEditing(true);
});
return false;
};
}
}
base.OnElementPropertyChanged(sender, e);
}
}
My ideal solution is that you are able to enter text, tap the add button, and the keyboard closes and the command executes simultaneously. Any ideas on how to achieve this?
EDIT: It turns out the problem is with the custom renderer I use for the page. The custom renderer resizes the page when the keyboard appears so that it does not cover my editor field.
public class KeyboardPageRenderer : PageRenderer
{
private bool keyboardShowing;
private NSObject keyboardWillShow;
private NSObject keyboardWillHide;
private double duration;
private UIViewAnimationCurve curve;
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.keyboardWillShow = UIKeyboard.Notifications.ObserveWillShow(this.KeyboardWillShow);
this.keyboardWillHide = UIKeyboard.Notifications.ObserveWillHide(this.KeyboardWillHide);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
this.keyboardWillShow.Dispose();
this.keyboardWillHide.Dispose();
}
private void KeyboardWillShow(object sender, UIKeyboardEventArgs args)
{
if (!this.keyboardShowing)
{
this.keyboardShowing = true;
var keyboardFrame = UIKeyboard.FrameBeginFromNotification(args.Notification);
this.duration = args.AnimationDuration;
this.curve = args.AnimationCurve;
this.ScrollTheView(true, keyboardFrame.Height);
}
}
private void KeyboardWillHide(object sender, UIKeyboardEventArgs args)
{
if (this.keyboardShowing)
{
this.keyboardShowing = false;
var keyboardFrame = UIKeyboard.FrameBeginFromNotification(args.Notification);
this.duration = args.AnimationDuration;
this.curve = args.AnimationCurve;
this.ScrollTheView(false, keyboardFrame.Height);
}
}
private void ScrollTheView(bool scale, nfloat scrollAmount)
{
UIView.BeginAnimations(string.Empty, IntPtr.Zero);
UIView.SetAnimationDuration(this.duration);
UIView.SetAnimationCurve(this.curve);
var frame = View.Frame;
// Assumes the page belongs to a tabbed view.
// This does not scale to pages that do not have one.
UITabBarController tabBarController = new UITabBarController();
nfloat tabHeight = tabBarController.TabBar.Frame.Size.Height;
scrollAmount -= tabHeight;
if (scale)
{
frame.Y -= scrollAmount;
}
else
{
frame.Y += scrollAmount;
}
View.Frame = frame;
UIView.CommitAnimations();
}
}
There is two issues in your approach
After Task.Delay(10), you are not on the UI thread anymore, which means you have to use Device.BeginInvokeOnMainThread in order to access UI elements.
Control.ShouldEndEditing must be cleared before you call EndEditing
A working solution would look like this:
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null || Control == null)
return;
VisualElement element = Element as VisualElement;
if (element == null)
return;
if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName && element.IsFocused == false)
{
Control.ShouldEndEditing = (UITextView control) =>
{
Device.BeginInvokeOnMainThread(() =>
{
control.ShouldEndEditing = null;
control.EndEditing(true);
});
// prevent the keyboard from closing
return false;
};
}
}

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);
}
}
};
});
});

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

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);
}

LWUIT 1.4 - Why doesn't the virtual keyboard appear after first show during textfield edit?

I want to show the virtual keyboard when the user is editing the textfield. I take this approach :
public class ChpModif extends TextField {
public ChpModif(int maxChars, FocusListener focusListener, DataChangedListener dataChangeListener, VirtualKeyboard vkb)
{
super();
setReplaceMenu(false);
if (maxChars != -1)
setMaxSize(maxChars);
addFocusListener(focusListener);
addDataChangeListener(dataChangeListener);
if (vkb != null)
VirtualKeyboard.bindVirtualKeyboard(this, vkb);
}
protected Command installCommands(Command clear, Command t9)
{
return null;
}
}
public class ModifierFicheClient extends Ecran implements ActionListener, DataChangedListener, FocusListener
{
private VirtualKeyboard vkNombre = new VirtualKeyboard();
private String textFieldStatus, listBoxStatus;
private ListBox genretxt;
private boolean modifFromUpdate;
private ChpModif nomtxt,prenomtxt,cintxt,adressetxt/*,genretxt*/,lieutxt,professiontxt,courieltxt,teltxt,datenaisstxt;
private Component cursorItem;
...
public ModifierFicheClient(SmartPhoneBanking controller,String prosp_id,int recordStoreID,Form prevForm)
{
super("");
vkNombre.setInputModeOrder(new String[]{VirtualKeyboard.NUMBERS_SYMBOLS_MODE});
...
modifFromUpdate = false;
cintxt = new ChpModif(12, this, this, vkNombre);
...
}
public void dataChanged(int type, int index) {
textFieldStatus = "CHANGED";
if (!modifFromUpdate)
{
try
{
if (type == DataChangedListener.ADDED || type == DataChangedListener.CHANGED || type == DataChangedListener.REMOVED)
{
if (Display.getInstance().isVirtualKeyboardShowingSupported())
{
if (!Display.getInstance().isVirtualKeyboardShowing())
cursorItem.pointerReleased(cursorItem.getAbsoluteX(), cursorItem.getAbsoluteY());
}
}
}
catch (ClassCastException cce)
{}
}
}
public void focusGained(Component chp) {
cursorItem = chp;
}
public void focusLost(Component arg0) {
}
protected void onShowCompleted()
{
...
update();
}
public void update()
{
modifFromUpdate = true;
cintxt.setText(fichesignalitique.elementAt(0).toString());
...
modifFromUpdate = false;
}
...
}
The problem is that at the first time when I edit the textfield then the virtualkeyboard is shown ; then I click the Ok button of the virtualkeyboard , and then I edit again the textfield. But in this time the virtualkeyboard is not shown !
So how to make the virtualkeyboard shown everytime I edit the textfield ?
Its entirely possible that this is a 1.4 bug that was fixed in 1.5 since I can't see it right now. The VKB was brand new in 1.4 .

Resources