1

i want to build app with viewpager and display image and text from DB, and i had a button to resize Text to Small,Medium,Big size, but my problem how to make that methode and call it? this is my code

Phrase.java

public void onCreate(Bundle savedInstanceState) {  
     setContentView(R.layout.phrase);  
     mViewPager = (ViewPager) findViewById(R.id.viewPager);  
     mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
     mViewPager.setAdapter(mMyFragmentPagerAdapter);

     text = (ImageView)findViewById(R.id.ibText);

     /* call db and store in arraylist */
     ....
}

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  
    public MyFragmentPagerAdapter(FragmentManager fm) {  
         super(fm);  
    }  

    @Override  
    public Fragment getItem(int index) {  
         var = arrayphrase.get(index);
         phrase = var.phraseKey;
         title_phrase = var.titleKey;
         sound_id = var.indexkey;
         return PageFragment.newInstance(index,sound_id,phrase,title_phrase);
    }  

    @Override

    public void destroyItem(View container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
    }

    @Override  
    public int getCount() {  

         return NUMBER_OF_PAGES;  
    }  
}  

CharSequence[] sizetext = {"Small", "Medium", "Big","Cancel"};
@Override
public void onClick(View v) {
      case R.id.ibText:
        AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
        listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) 
            {

                   if(sizetext[item].equals("Big"))
                   {
                       sizeText=24;
                       onUpdateSize(sizeText);
                   }
                   else if(sizetext[item].equals("Medium"))
                   {
                       sizeText=20;
                       onUpdateSize(sizeText);
                   }
                   else if(sizetext[item].equals("Small"))
                   {
                       sizeText=16;
                       onUpdateSize(sizeText);
                   }
            }
        });
        AlertDialog alertList = listBuilder.create();
        alertList.show();
    break;
    }

PageFragment.java

    public class PageFragment extends Fragment { 
    public static PageFragment newInstance(int index,String sound, String phrase, String title_phrase) {

    PageFragment pageFragment = new PageFragment();
    Bundle bundle = new Bundle();
    bundle.putString("title_phrase", title_phrase);
    bundle.putString("sound", sound);
    bundle.putString("phrase", phrase);
    bundle.putInt("index", index);
    pageFragment.setArguments(bundle);
    return pageFragment;
}

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
}  

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

    View view = inflater.inflate(R.layout.fragment, container, false);  
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);  
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    options.inScaled = false;
    options.inPreferQualityOverSpeed=true;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), Imgid[(int)(Math.random()*Imgid.length)], options);
    //imageView.setScaleType(ScaleType.FIT_XY);
    imageView.setImageBitmap(bitmap);

    ScrollView sv = (ScrollView)view.findViewById(R.id.scrollView1);
    LinearLayout.LayoutParams lp2= new LinearLayout.LayoutParams((int)(TorasanApp.device_width*0.7), (int) (TorasanApp.device_height*0.65),Gravity.CENTER);
    sv.setLayoutParams(lp2);
    sv.setBackgroundColor(Color.argb(20, 65, 65, 65));

    TextView tvdata = (TextView)view.findViewById(R.id.tvDetailPhrase);
    tvdata.setText(getArguments().getString("phrase"));

    TextView tvtitle = (TextView)view.findViewById(R.id.tvTitlephrase);
    tvtitle.setText(getArguments().getString("title_phrase"));
    return view;  
}  
}

you see in my Phrase when onclick i have onUpdateSize(sizeText) and i want call it to change TextView tvdata fontsize that was in PageFragment.java , so how to make that method and call it? because i try but still cant

Frank Junior
  • 575
  • 1
  • 9
  • 19

2 Answers2

1

In your problem,

You can have a Fragment instance in onClick method for which you want to update textview and from there you can call any method of that class(you have to create such method in Fragment class which will update textview.).

For more understanding refer this link.

in above link Step 10 contain the method calling(viewer.updateUrl(tutUrl);) to fragment class from FragmentActivity class

And in step 8 defination of updateUrl method given which is in Fragment class

rajpara
  • 5,203
  • 1
  • 29
  • 46
1

In your Fragment's onCreateView, do

mButton.setOnClickListener(new View.OnClickListener() {
    AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
    listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) 
        {

               if(sizetext[item].equals("Big"))
               {
                   sizeText=24;
                   onUpdateSize(sizeText);
               }
               else if(sizetext[item].equals("Medium"))
               {
                   sizeText=20;
                   onUpdateSize(sizeText);
               }
               else if(sizetext[item].equals("Small"))
               {
                   sizeText=16;
                   onUpdateSize(sizeText);
               }
        }
    });
    AlertDialog alertList = listBuilder.create();
    alertList.show();
});

Then create accessor methods in your Activity so that your Fragment can retrieve the required fields.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • still confuse, tvData is TextView in Fragment, and my button to call that DialogInterface was in Activity. sory because im new in android progaming with fragment – Frank Junior Jul 25 '12 at 04:47