0

I have an Activity (MainActivity) which extends to Activity and a related Fragment (FragmentFive)

There is a seekbar placed in Fragment whose value I want to access from MainActivity. How to do it? API level 18 or above only.

MainActivity has a Button which direct to FragmentFive when clicked: android:onClick= "goFag5"

An example code for Activity will be very useful! Code for Fragment is as below;

FragmentFive.java

public class FragmentFive extends Fragment {

private SeekBar RedBar; 

public FragmentFive() {
    // Required empty public constructor
}

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

    RedBar = (SeekBar) v.findViewById(R.id.seekBar1);

    RedBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            Log.d("HM", progress+": value");


        }
    });

    return v;
    }

}

code in Mainactivity

public void gofag5(View v) {
    // TODO Auto-generated method stub
    FragmentFive frag = new FragmentFive();
    FragmentManager fm = getFragmentManager(); // import
    FragmentTransaction ftr = fm.beginTransaction(); // import
    ftr.add(R.id.mainlayout, frag, "keyFrag");
    ftr.addToBackStack(null);
    ftr.commit();

}
user3727505
  • 1
  • 1
  • 3

2 Answers2

0

Have can create a getter and setter for the SeekBar in your Fragment. In your Activity you have to get a hold on to the Fragment. How depends on how you created the Fragment. If you did create it programmatically you probably already have an instance of it because you created it like:

MyFragment myFragment = new Fragment(); 

Or you have the Fragment embedded in your XML layout file, then you can use something like

myFragment = getFragmentManager().findFragmentById(R.id.myFragment); 

Then you can do:

myFragment.getSeekBar(); 

Note: Above is pseudocode and will not work, but it should give you an idea. When using the second approach you will need to cast the Fragment to MyFragment.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • Thx, I am using XML layout file to create Fragment. So where to put the code myFragment = getFragmentManager().findFragmentById(R.id.myFragment); inside OnCreate() of MainActivity? – user3727505 Jun 10 '14 at 20:20
  • You can do this anywhere you want. I'd do it in onCreate() and keep a reference to the obtained Fragment as a class variable. Just try to get a hold on the FragmentManager and via the FragmentManager get a hold on the Fragment. The Fragment has to have an id assigned in the layout for this to work. This works slightly different when you use the support fragments instead of the 'original' fragments. Try it in onCreate() or just whenever you need the Fragment / it's SeekBar and ask again if you're lost :) – fweigl Jun 10 '14 at 20:43
  • Sorry bit lost, Following is the code in the MainActivity which directs a Button's onClick event to a Fragment: FragmentFive `public void gofag5(View v) { FragmentFive frag = new FragmentFive(); FragmentManager fm = getFragmentManager(); // import FragmentTransaction ftr = fm.beginTransaction(); // import ftr.add(R.id.mainlayout, frag, "keyFrag"); ftr.addToBackStack(null); ftr.commit(); frag.getSeekBar(); }` – user3727505 Jun 11 '14 at 05:49
  • You should probably open a new question, post what you have tried and where you are stuck. Solving this via comments will be cumbersome. – fweigl Jun 11 '14 at 20:48
0

Interfaces can be used to call from a fragment back to the Activity.

public class SettingFollow extends Fragment implements 
    OnSeekBarChangeListener {

public interface BestRidesFollowDialogListener {
    void onSettingFollowChange(int progress);
}

BestRidesFollowDialogListener bestRidesFollowDialogListener;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // the activity may be null if this is called without implementing the
    // interface 

    activity = getActivity();
    bestRidesFollowDialogListener = (BestRidesFollowDialogListener.class
            .isAssignableFrom(activity.getClass())) ? (BestRidesFollowDialogListener) activity
            : null;
    view = inflater.inflate(R.layout.settings_follow, container,
            false);
    return view;
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // we save and process the value here

    switch (seekBar.getId()) {
    case R.id.sbFollowMillis:
    //call through the interface if its non null the 
            // only time its null is when the programmer has 
            // not implemented the interface in the activity that started
            // the fragment.

                if (bestRidesFollowDialogListener != null) {
        bestRidesFollowDialogListener.onSettingFollowChange(seekBar.getProgress());
    }

}

Then in your activity you implement the interface for the fragment which in the above example is called BestRidesFollowDialogListener and let your ide create the stubs.

Note not shown save the values of the seekbar in the fragment. initialize the seekbar to the saved value in the fragment. Sorry about the long class names but if you have a couple fragment interfaces to deal with in the activity the long class and method names really help out.

danny117
  • 5,581
  • 1
  • 26
  • 35