androidtextviewwidthoffsetwidth

Error with setWidth in android


My problem is that i want change width of my TEXTVIEW (INPUT TEXT). In emulator that code work success but when i try to work with my app in mobile that crash it.

My answer is: Why my app crash when i try to change .setWidth? and how can i solve my problem?

A lot of thanks.

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.binomial);

    EditText TextVA, TextVB, TextVC, EtiquetaA, EtiquetaB, EtiquetaC;

    TextVA = (EditText)findViewById(R.id.editTextP);
    TextVB = (EditText)findViewById(R.id.editTextK);
    TextVC = (EditText)findViewById(R.id.editText3);

    ArrayList<TextView> listaTexto = new ArrayList<TextView>();
    listaTexto.add(TextVA);
    listaTexto.add(TextVB);
    listaTexto.add(TextVC);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int y = size.y;
    int x = size.x;

    ResolucionPantalla respant = new ResolucionPantalla(y, x);

    for (int i=0;i<listaTexto.size();i++)
            //ResolInput METHOD return size in px according to the screen size
        listaTexto.get(i).setWidth(respant.ResolInput());

    Button BotonCalcular = (Button)findViewById(R.id.buttonCalcular);
    BotonCalcular.setOnClickListener(this);       
}

EDIT: TextView to EditText


Solution

  • I see several potential issues...

    For starters:

    TextVA = (TextView)findViewById(R.id.editTextP);
    

    Is R.id.editTextP an EditText? You are casting it as a TextView

    And also:

    Calling setWidth here will have no effect. There is an onLayout method for each view that gets called after onCreate is finished. The width/height get set to whatever is specified in your XML layout during that call

    You can either extend and create your own EditText and override the onLayout method, or you can figure out how to size your views correctly via XML.