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

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

Related

Going to Main Activity from fragment

I implemented a navigation drawer and had used fragments. Now i can switch to fragments from the drawer menu. But when i press back, my app closes from that fragment and don't know how to go back to the main activity.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
displaySelectedScreen(id);
return true;
}
private void displaySelectedScreen(int id) {
Fragment frag = null;
switch (id) {
case R.id.trigger:
break;
case R.id.setup:
if (android.os.Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#7B1FA2"));
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8E24AA")));
frag = new SettingsF();
break;
case R.id.recordings:
if (android.os.Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#4E342E"));
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8D6E63")));
break;
case R.id.howto:
if (android.os.Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#7B1FA2"));
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8E24AA")));
break;
case R.id.about:
if (android.os.Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#7B1FA2"));
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8E24AA")));
break;
case R.id.email_us:
if (android.os.Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#7B1FA2"));
}
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8E24AA")));
break;
}
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, frag);
ft.addToBackStack(ft.getClass().getSimpleName()).commit();
}
DrawerLayout drawer = findViewById(R.id.drawer);
drawer.closeDrawer(GravityCompat.START);
}
And here is the setting fragment only:
public class SettingsF extends ListFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_settings, container, false);
String[] datasource = {"Trusted Contacts", "Custom Text Set-Up"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout, R.id.txtitems, datasource);
setListAdapter(adapter);
setRetainInstance(true);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Context Set-Up");
}
public void onListItemClick(ListView l, View view, int position, long id) {
ViewGroup viewGroup = (ViewGroup) view;
TextView tv = (TextView) viewGroup.findViewById(R.id.txtitems);
Toast.makeText(getActivity(), tv.getText().toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onResume() {
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener( new View.OnKeyListener()
{
#Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
//Already Used Intent, doesn't work
return true;
}
return false;
}
} );
super.onResume();
}
}
I updated the question. I already used addToBackStack(), then overrided the method onResume() but still the app terminates when pressing the back button.
I'm really that new to this, so don't misunderstand me what did i implemented throughout the codes.
Thanks In Advance
for Hardware backbutton
add this code in onResume method in fragment
//You need to add the following line
fragment.getView().setFocusableInTouchMode(true);
fragment.getView().requestFocus();
fragment.getView().setOnKeyListener( new OnKeyListener()
{
#Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
//here use intent to call mainActivity
return true;
}
return false;
}
} );
hope this will help you
Add this line after ft.replace():
ft.addToBackStack(null);
This line will make it so that each time you select an item from the navigation drawer, a history of fragments is built up. Then when you press the back button, you'll go back through the history. If you have no history items, the app will close.
A more sophisticated implementation might use a name argument (like "FragA" or "FragB") instead of null as the argument for addToBackStack(), and then check to see if your fragment already exists in the back stack instead of always opening a new one. That way if you went A->B->A->B->A->B you wouldn't have three copies of A and two copies of B in the history.

Using onTouch how to handle Swiping and Tapping both in Android Layout

In the below code snippet (only the listener portion), i want to use the Single Tap only for Tapping the capture and Swiping if you are swiping.
In onTouch() if I return false then it gives me the Single Tapping feature and if I return true then I can handle the swipe feature using both ACTION_DOWN and ACTION_UP.
In Case of Tapping If I am trying to handle using both the action it's not happening, only through ACTION_DOWN it's getting.
Please help me out for the same.
this.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
public void onSwipeTop() {
Toast.makeText(getContext(), "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(getContext(), "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(getContext(), "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(getContext(), "bottom", Toast.LENGTH_SHORT).show();
}
public boolean onTouch(View v, MotionEvent event) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = e.getX();
case MotionEvent.ACTION_UP:
x2 = e.getX();
break;
}
Toast.makeText(getContext(),"touch",Toast.LENGTH_SHORT).show();
return false;
}
});

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

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

Resources