Question

I am designing dynamic layout in which I am adding a spinner, a edittext and a button. It shows proper for AVD but it changes as per the device changes. Here is my code.

Spinner newMobileSpinner = new Spinner(CustomerDetails.this);
newMobileSpinner.setLayoutParams(new 
    LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 0.2f
    )
);
newMobileSpinner.setAdapter(arrayAdpType);

EditText newMobileEditText = new EditText(CustomerDetails.this);
newMobileEditText.setLayoutParams(new 
    LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 1.2f
    )
);
newMobileEditText.getLayoutParams().width=0;
newMobileEditText.setHint("Mobile No");
newMobileEditText.setBackgroundDrawable(
    getResources().getDrawable(android.R.drawable.editbox_background_normal)
);
newMobileEditText.setInputType(InputType.TYPE_CLASS_NUMBER | 
    InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

Button newBtnRemove = new Button(CustomerDetails.this);
newBtnRemove.setLayoutParams(new LinearLayout.LayoutParams(20,45, 0.15f));
newBtnRemove.setBackgroundResource(R.drawable.minusred);
final LinearLayout Matrix = (LinearLayout)findViewById(R.id.sub5);
final LinearLayout layout = new LinearLayout(CustomerDetails.this);
layout.setOrientation(LinearLayout.HORIZONTAL);

layout.addView(newMobileSpinner);
layout.addView(newMobileEditText);
layout.addView(newBtnRemove);

Matrix.addView(layout);

please help me out.

Was it helpful?

Solution

// try this way,hope this will help you...

        Spinner newMobileSpinner = new Spinner(this);
        newMobileSpinner.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
        newMobileSpinner.setAdapter(arrayAdpType);

        EditText newMobileEditText = new EditText(this);
        newMobileEditText.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
        newMobileEditText.setHint("Mobile No");
        newMobileEditText.setBackgroundResource(android.R.drawable.editbox_background_normal);
        newMobileEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

        Button newBtnRemove = new Button(this);
        newBtnRemove.setLayoutParams(new LinearLayout.LayoutParams(0,45, 1f));
        newBtnRemove.setBackgroundResource(R.drawable.minusred);
        LinearLayout Matrix = (LinearLayout)findViewById(R.id.sub5);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);

        layout.addView(newMobileSpinner);
        layout.addView(newMobileEditText);
        layout.addView(newBtnRemove);
        Matrix.addView(layout);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top