0

I have a java file which has the reference to the button:

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        createMenus();
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

@Override
    public void onClick(final View view) {
        final int actionBarHeight = getActivity().findViewById(R.id.title_main_container).getHeight();
        switch (view.getId()) {
            case R.id.news_article_menu_button:
                mOptionsMenuHelper.showMenu(view.getBottom() + actionBarHeight, Gravity.RIGHT);
                break;
            case R.id.text_size:
                mTextSize = TextSize.values()[(mTextSize.ordinal() + 1) % TextSize.values().length];
                updateFontSize();
                break;
            default:
                throw new IllegalArgumentException("Invalid view Id" + view.getId());
        }
    }
private void setOnclickListeners(final View view){
        final ImageButton button = (ImageButton) view.findViewById(R.id.menu_button);
        button.setOnClickListener(this);
    }

    private void createMenus() {
        mPreferenceMenuItems.add(PreferenceMenuItems.MENU_PREFERENCES);
        mPreferencesMenuHelper = new MenuHelper(getActivity(), mPreferenceMenuItems,this);

        mDeleteMenuItems.add(DeleteMenuItems.DELETE_PREFERENCES);
        mDeleteMenuHelper = new MenuHelper(getActivity(), mDeleteMenuItems,this);
    }

This is the corresponding button "over_btn" where the selector is defined:

<?xml version="1.0" encoding="utf-8"?>


    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/over" />
    </selector>

and this is one of the corresponding xml files using the same fragment_test:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:background="@color/background_bar" >

    <ImageButton
        android:id="@+id/menu_button"
        android:layout_width="49dp"
        android:layout_height="48.5dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:gravity="right"
        android:paddingRight="14dp"
        android:src="@drawable/over_btn" />

</RelativeLayout>

I want the button to switch to a different image onclick and stay in that state till pressed again instead of highlighting once when pressed, is there any way to go about this?

Thanks! Justin

  • You cant use this as a context for a fragment becuase it does not have its own context - you need to use getActivity(). – Rarw Jul 11 '13 at 12:36

2 Answers2

0

add

<item 
     android:state_selected="true" 
     android:drawable="@drawable/info_selected" />

to your selector and when you click on the ImageButton, call view.setSelected(!view.isSelected()) to switch between status selected and unselected

public void onClick(final View view) {
          switch (view.getId()) {
               case R.id.imageButtonId:
               view.setSelected(!view.isSelected());
               break;
          }

 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I didn't get the java part? can you elaborate? –  Jul 10 '13 at 15:37
  • of course. You call setOnClickListener on the imageButton, am I right? Then when you press it the onClick(View) is called – Blackbelt Jul 10 '13 at 15:39
  • can you modify my java file code and explain , I dont get it. –  Jul 10 '13 at 15:51
  • I did this : case R.id.news_article_menu_button: mOptionsMenuHelper.showMenu(view.getBottom() + actionBarHeight, Gravity.RIGHT); view.setSelected(!view.isSelected()); break; –  Jul 10 '13 at 19:02
0

You should be able to do this with an onClick listener as show in this post. //these are instance variables private ImageButton button = null; private boolean imageSwitched = false;

//call this method in your onActivityCreated() method. If the
//layout containing the button is not yet inflated you will not
//be able to find your image button. 
private void initializeButton(){     
    button= (ImageButton)getActivity().findViewById(R.id.imgButton);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if(!imageSwitched){
                button.setBackgroundResource(R.drawable.a);
                imageSwitched = true;
            } else {
                buttons.setBackgroundResource(R.drawable.b);
                imageSwitched = false;
            }
        }
    });
}

To switch beteen 2 views you will need to use some state variable. I chose a boolean thats set on click to handle which image is swiched to on click.

Also if you don't want to use a listener you can set your button to have an onClick method in XML and use the same state handling idea

public void buttonSwitch(View v){
 //state handling here
}

This way the method is called only when click and does not use the overhead of a listener.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • can you explain with respect to the above code for oncreateview instead of oncreate? –  Jul 10 '13 at 15:50
  • Sure - the code should work so long as you just transpose it to where you initilize your button. Im guessing that's the createMenus() method but wasn't sure. onCreate is just an activity lifecycle method (activities don't have an onCreateView or onActivityCreated since they are not tied to an underlying activity like a fragment.) You could use the listener approach in your onCreateView/createMenus method - wherever you initialize your buttons. Or just use the XML approach and define android:onClick for your buttons XML to call this method. – Rarw Jul 10 '13 at 15:54
  • can you edit your answer with respect to the code I have given above? I don't get the answer explained. Thanks! –  Jul 10 '13 at 19:07
  • I edited the code to reflect what it could look like if you were initializing your button within your createMenus method. If not, just copy and paste that in either onCreateView or onActivityCreated – Rarw Jul 10 '13 at 19:17
  • caused a crash, with the fragment where it is defined crashing due to it, is there anything that can be done to edit it so it can work with the given code? –  Jul 10 '13 at 20:01
  • edited the java code to show more details, can you please check out the same? –  Jul 10 '13 at 20:05
  • You're not setting up your button correctly. Also, where are you calling set onClick listeners from. I'm going to edit this answer again to try and make it more clear but then I think you need to just figure out the implementation. Also are there multiple XML files for this fragment? The button needs to be contained within the layout that you are inflating in your onCreateView method. Otherwise you will not be able to find that view. – Rarw Jul 11 '13 at 12:31