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