1

This is similar to my previous question but it didnt work with the Kindle Fire (2.3.4).
I used a FragmentTransaction to add a Fragment into a FrameLayout. I want to dynamically change the margin of the RelativeLayout used by the Fragment.

However, the margins are not changing with FrameLayout.layoutParams on the Kindle Fire. However, this works on 3.2.1. I also tried using setMargins() and it didnt work.

Does anyone know how I can dynamically change the margins on the Kindle Fire?

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

        View view = inflater.inflate(R.layout.infocard, container, false);

        RelativeLayout infoLayout = (RelativeLayout) view.findViewById(R.id.info);
        infoLayout.setOnClickListener(new EmptyClickListener());

        final int width = 250;
        final int height = 270;
        int leftMargin = 0;
        int topMargin = 0;
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);

        if (x - width < 0) {
            leftMargin = 0;
        } else {
            leftMargin = x - width + 40;
        }

        if (y >= 400 && y <= 480) {
            topMargin = 160;
        }

        params.leftMargin = leftMargin;
        params.topMargin = topMargin;

        //params.setMargins(leftMargin, topMargin, 0, 0); //this didnt work either
        infoLayout.setLayoutParams(params);

        TextView titleView = (TextView) view.findViewById(R.id.titleCard);
        titleView.setText(title);

        return view;
    }
Community
  • 1
  • 1
heero
  • 1,941
  • 5
  • 23
  • 33
  • Is infoLayout a subview of a FrameLayout or a RelativeLayout? – Sam Judd Mar 22 '12 at 00:04
  • infoLayout is a RelativeLayout – heero Mar 22 '12 at 00:08
  • Why are you using FrameLayout.LayoutParams instead of RelativeLayout.LayoutParams? Have you tried using RelativeLayout.LayoutParams? More to the point, have you tried calling params = infoLayout.getParams(); and then editing the params rather than recreating them? – Sam Judd Mar 22 '12 at 00:15
  • I used FrameLayout because I add the fragment into it, and this does work on a 3.2.1 device, just not the Kindle. I also tried using LinearLayout and RelativeLayout, and those did not work. Also I cannot call getparam() on infolayout because there is no such method. – heero Mar 22 '12 at 00:39
  • Sorry typo, meant: getLayoutParams() not getParams() – Sam Judd Mar 22 '12 at 07:28
  • ok I am now using `FrameLayout.LayoutParams params = (LayoutParams) infoLayout.getLayoutParams();`, however it didnt fix the margin problem on kindle fire; but it works on 3.2.1 device. – heero Mar 22 '12 at 17:32
  • 1
    Ok got it, I we were miscommunicating. I don't see anything obviously wrong with what you're doing. You could do something horribly hacky and add generic invisible views above and to the left of your fragment and set their width which would give you the same effect as setting the margins. You may also want to check through you're other code in case you have something else wrong that is indirectly causing the issue. The kindle could be reacting correctly to bad code and producing the wrong output and the other device could be reacting incorrectly to produce the right output. – Sam Judd Mar 22 '12 at 17:48

1 Answers1

3

Ok I was able to fix this by wrapping my infoLayout in another RelativeLayout, and now it works perfectly

heero
  • 1,941
  • 5
  • 23
  • 33