Rashmi S
Rashmi S

Reputation: 267

dynamic EditText different size in different resolutions

 for(int l=0;l<c.getCount();l++)
 {
     EditText etpob = new EditText(this.getActivity());
     etpob.setInputType(InputType.TYPE_CLASS_NUMBER);
     int po=Integer.parseInt("1" + c.getInt(3)) ;
     etpob.setId(po);
     etpob.setHint("POB");
     etpob.setText("");
     etpob.setTextSize(15);
     etpob.setVisibility(View.VISIBLE);
     etpob.setLayoutParams(new LinearLayout.LayoutParams(0, 50, 15));
     etpob.setGravity(Gravity.RIGHT);
}

I have added the EditText dynamically above is my code, But it resolution fails out for different types of screen,in layout using styles we can change it out.

Upvotes: 2

Views: 356

Answers (5)

Rashmi S
Rashmi S

Reputation: 267

etpob.setLayoutParams(new LinearLayout.LayoutParams(0, ConvertPixels(50), ConvertPixels(15))); 

public int  ConvertPixels(int ht)
    {
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,ht, getActivity().getResources().getDisplayMetrics());
        return height;
    }

Used existing utility method built called TypedValue.applyDimensions(int, float, DisplayMetrics)

Upvotes: 0

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

Incase the editText needs to match_parent

 Point size = new Point();
 getWindowManager().getDefaultDisplay().getSize(size);
 width = size.x;

And set width to EditText

 editText.setWidth(width);

Upvotes: 0

Haniyeh Khaksar
Haniyeh Khaksar

Reputation: 804

if you want to use an static integer in your code for size of your EditText, must use values folder in different sizes. create in res folder these folders: values-small, values-normal,values-large,values-xlarge. in these folder make a resources file with Integer values. like below

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="edittext_width">70</integer>
    <integer name="edittext_height">30</integer>
</resources>

you can set this values with same name but different values in these folders. then, you must use it in your code:

etpob.setLayoutParams(new LinearLayout.LayoutParams(0,  getResources().getInteger(R.integer.edittext_width),  getResources().getInteger(R.integer.edittext_height)));

GOOD LUCK.

Upvotes: 0

Ajay Pandya
Ajay Pandya

Reputation: 2457

You can use dimension for setting width of dynamic generated edittext

int pixels = 15; // use dimen here 
float scale = getContext().getResources().getDisplayMetrics().density;
float dips = pixels / scale;

So,

 etpob.setLayoutParams(new LinearLayout.LayoutParams(0, pixels , pixels ));

or if it is require you can set dimen and than you can do it like this way

getContext().getResources().getDimension(R.dimen.activity_horizontal_margin); //set size of dimen in required resolution

Upvotes: 1

sanky jain
sanky jain

Reputation: 873

You can do the same way.

Define the parameters in style files and use it in code.

Check below link for reference

How to retrieve style attributes programmatically from styles.xml

Upvotes: 0

Related Questions