user1730789
user1730789

Reputation: 5177

ActionBarSherlock Tabs

I have to implement Tabs in my application using ActionBarSherlock. I am following this example here. Now i want to add buttons to one of the fragment, and perform an action on it. How do i do it ? Suppose this is the layout where the button is

public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.hello, container, false);
    }
}

How do i read a button from the view ?

Upvotes: 1

Views: 1968

Answers (3)

RajeshVijayakumar
RajeshVijayakumar

Reputation: 10622

public class AFragment extends SherlockFragment {
private Button button;    
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflating the layout view  to this fragment
    View view = inflater.inflate(R.layout.hello, container, false);
    button = (Button) view.findViewById(R.id.button_id);

    button.setOnClickListener(this);

    return view;
   }
    @Override onClick() {
       switch(v.getId()_ {
          case R.id.button_id:
                       //Your logic for button goes here
                       break;
       }
    }
}
}

Upvotes: 4

dougcunha
dougcunha

Reputation: 1238

private Button button;    

    public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.hello, container, false);
        //finding the button by ID inside the inflated view
        button = (Button) view.findViewById(R.id.btnid);
        //setting an event
        button.setOnItemClickListener(new OnClickListener() {

        });
        return view;
    }

Upvotes: 1

doubleA
doubleA

Reputation: 2456

I believe that this is how it is done. as far as i can tell. I have not exactly gotten mine to work yet but in dont think my problem is here.

    private Button button;    

    public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflater = inflate(R.layout.hello, container, false);
        button = (Button) inflater.findViewById(R.id.reminder_slider);
        button setOnItemClickListener(new OnClickListener() {
        //do stuff on item click aka @Override onClick()
        }
        return inflater;
    }
}

Upvotes: 2

Related Questions