2

Hi I'm trying to load a button into a view pager and i have it loading in but it's currently filling the entire screen i would like it it to wrap content to its exact size and then position it in the middle of the screen. i have tried to set layoutparams for the button but when i run the app it still fills parent. does anyone know how to programmatically set layout and position of a button?

heres what i have tried so far

@Override
        public Object instantiateItem(View collection, int position) {

            RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
            rel_btn.addRule(RelativeLayout.CENTER_HORIZONTAL);
            rel_btn.height = 60;
            rel_btn.width = 60;
            Button tv = new Button(cxt);
            tv.setLayoutParams(rel_btn);
            tv.setText("League " + (position+1));
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(30);
            tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.ls_level_eng1));

            ((ViewPager) collection).addView(tv,0);

            return tv;
        }

1 Answer 1

1

Is there any particular reason you're doing this via code? You could simply inflate a layout and use the Layout Editor to help you adjust your Button. That way you won't be guessing so much.

public Object instantiateItem(View collection, int position) {

final LayoutInflater mLayoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final View view = inflater.inflate(R.layout.your_layout, null);

((ViewPager) collection).addView(view, 0);

return view;

In that case, you need to do this. I added notes within the code.

    public Object instantiateItem(View collection, int position) {

    // Create your LayoutParams
    final RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    // Add your rules
    rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
    rel_btn.addRule(RelativeLayout.CENTER_HORIZONTAL);

    // And anything extra
    rel_btn.height = 60;
    rel_btn.width = 60;

    // Create a new RelativeLayout for your RelativeLayout.LayoutParams
    RelativeLayout relativeLayout = new RelativeLayout(MainActivity.this);

    // Give your RelativeLayout LayoutParams
    relativeLayout.setLayoutParams(rel_btn);

    // Create your Button
    Button tv = new Button(MainActivity.this);
    tv.setText("League " + (position + 1));
    tv.setTextColor(Color.WHITE);
    tv.setTextSize(30);
    tv.setLayoutParams(rel_btn);
    // You don't need to use setBackgroundDrawable if you're only
    // grabbing a Resource
    tv.setBackgroundResource(R.drawable.ls_level_eng1);

    // Add your Button to your RelativeLayout
    relativeLayout.addView(tv);

    // Return your RelativeLayout
    ((ViewPager)collection).addView(relativeLayout, 0);
    return relativeLayout;
}
1
  • yes because the button will become dynamic so its source will pulled from an array I'm just trying it with one for now Jul 14, 2012 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.