Reputation: 8251
I want to control the size of the TextView
with the help of a SeekBar
i.e the TextView
size increases when the SeekBar
is dragged towards right and decreases while dragging towards left.I can do it when the TextView
is displayed in a simple activity UI.But here the problem is the TextView
have to be displayed inside a ViewPager
.Please help me.
Following is my code,
public class MainActivity extends FragmentActivity {
TextView tv;
SeekBar seek;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar) findViewById(R.id.seekBar1);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
public CharSequence getPageTitle(int position) {
return "Page #" + (position + 1);
}
}
public class MyFragment extends Fragment {
MainActivity m;
int mCurrentPage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,
false);
tv = (TextView) v.findViewById(R.id.tv);
tv.setText("You are viewing the page #" + mCurrentPage + "\n\n"
+ "Swipe Horizontally left / right");
return tv;
}
}
}
Upvotes: 0
Views: 789
Reputation: 87064
I'm guessing your SeekBar
is outside the ViewPager
. First set a OnSeekBarChangeListener
on the SeekBar
:
private OnSeekBarChangeListener mListener = new OnSeekBarChangeListener() {
// ...
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
MyFragment mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem);
if (mf != null && mf.getView() != null) {
mf.updateText(progress);
}
mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem - 1);
if (mf != null && mf.getView != null) {
mf.updateText(progress);
}
mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem + 1);
if (mf != null && mf.getView != null) {
mf.updateText(progress);
}
}
}
};
where mCurrentItem
is an int
field which will store the current page of the ViewPager
:
private int mCurrentItem = 0;
// ... in the onCreate method
seek.setMax(100); // the maximum for the TextView size
seek.setProgress(50); // the starting textSize for the TextView
seek.setOnSeekBarChangeListener(mListener);
You'll also need a ViewPager.OnPageChangeListener
on the ViewPager
to update the mCurrentItem
field:
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mCurrentItem = position;
MyFragment mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + position);
if (mf != null && mf.getView() != null) {
mSeekBar.setOnSeekBarChangeListener(null);
mSeekBar.setProgress((int) (mf.getTextSize()));
mSeekBar.setOnSeekBarChangeListener(mListener);
}
}
// ...
}
Don't forget to add this methods to your fragment class:
public void updateText(int progress) {
textSize = progress;
((TextView)getView().findViewById(R.id.the_textview_id)).setTextSize(textSize);
}
and
public float getTextSize() {
return textSize;
}
where mTextsize
is a field in your fragment's class which will hold the text size:
private float textSize = 50;
Just realized I may misunderstood the question. If you want to update the TextView
size, its width/height, then instead of setting the text like in my answer, simply retrieve the LayoutParams
from the TextView
and update as desired.
Upvotes: 1