dan
dan

Reputation: 189

Pressing button in fragment, refresh the activity

I have fragment with few buttons. I put that fragment in all activities in my app. I am using the button to travel between the activities

So far everything work fine. I want to highlight the button that was press.(i am using image button, so i want to change the image)

For example, if i press Home button, i will move to home activity and the home button will have different image

I want the handle this code form the fragment.

here is the relevant code from the fragment:

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_buttons, container,
            false);

    ImageButton btnHome = (ImageButton) view.findViewById(R.id.btnHome);
    btnHome.setOnClickListener(this);

    ImageButton btnSetting = (ImageButton) view.findViewById(R.id.btnSetting);
    btnSetting.setOnClickListener(this);

    return view;

}

 @Override
public void onClick(View v) {
    ImageButton imageButton = (ImageButton) v;

    switch (imageButton.getId()) {
    case R.id.btnHome:
        imageButton.setImageResource(R.drawable.btnhomev);
        startActivity(new Intent("com.example.sonoside2.HOME"));
        break;
    case R.id.btnSetting:
        imageButton.setImageResource(R.drawable.btnsettingv);
        startActivity(new Intent("com.example.sonoside2.SETTING"));
        break;
 }

THI

Upvotes: 0

Views: 368

Answers (1)

Sigi
Sigi

Reputation: 260

Implement the method onActivityCreated in your fragment. Here you can get your Activity Object like this:

public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Activity a = getActivity();
    String packageName = a.getPackageName();
    if (packageName.equalsIgnoreCase("com.example.sonoside2.HOME")) {
        //do something to highlight your buttons
    } else ...
}

Upvotes: 1

Related Questions