tags:

views:

27

answers:

2

I have a form with several Views on it, the last one is a Spinner that is bound to an adapter to get it's data from a Web Server via a POST request, at the end I append an additional entry for "Other...". If this option is selected on the spinner, a new EditText View at the bottom where the user enters a custom value, I've managed to get the EditText View to show on the screen, but it's positioned at the very top, over my other Views and I can't seem to find the way to make it appear at the bottom, below the Spinner as I want it to, here is the code I have so far:

EditText suggestCarrierField = new EditText(getBaseContext());
suggestCarrierField.setLayoutParams(new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.FILL_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));
                        suggestCarrierField.setHint("Suggest your carrier");

((AbsoluteLayout) findViewById(R.id.createAccountView)).addView(suggestCarrierField);
((AbsoluteLayout) findViewById(R.id.createAccountView)).invalidate();
A: 

When setting the LayoutParams for suggestCarrierField, don't use ViewGroup but instead use AbsoluteLayout.LayoutParams. It has a constructor which takes a height, width AND x and y coordinates. See the AbsoluteLayout.LayoutParams doc . Here is a quick app I whipped which demos this:

public class AbsoluteLayoutTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AbsoluteLayout as = new AbsoluteLayout(this);

        TextView tvTop = new TextView(this);
        tvTop.setText("top");
        tvTop.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0));

        TextView tvMid = new TextView(this);
        tvMid.setText("middle");
        tvMid.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 80));

        TextView tvBot = new TextView(this);
        tvBot.setText("bottom");
        tvBot.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 180));

        as.addView(tvTop);
        as.addView(tvMid);
        as.addView(tvBot);

        setContentView(as);
    }
}

This will result in three text views. One at the top (y-coord = 0), one in the middle (y-coord = 80) and one at the bottom (y-coord = 180).

Noel
A: 

Using an AbsoluteLayout, you need to use AbsoluteLayout.LayoutParams, rather than ViewGroup.LayoutParams.

int width = 100, height = 25, x = 0, y = 200;
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(width, height, x, y);
suggestCarrierField.setLayoutParams(lp);

Having said that, I strongly urge you to consider implementing this with a RelativeLayout (or LinearLayout) instead... AbsoluteLayout is deprecated, and for very good reason. There are so many different Android devices with different sized screens now, AbsoluteLayout just won't work across them all.

Andy
Thanks so much, I got working with your suggestion, I'm aware of issues with AbsoluteLayout I'm already working on migrating my activities to RelativeLayout but I needed this fixed quickly. Cheers!
adrzip