Focus Edittext Android

Asked

Viewed 3,898 times

1

How do I get focus on one EditText but I’m still in trouble?

I’m making a ordering system, I have 3 EditText (product, quantity and discount) and a button Add.

I need that when the user click the add button, the focus goes back to the product field, today the focus is going to a editText of listView (order items) that has just been added.

So if the user adds 30 products he has to scroll the screen up and put the focus on the product.

I’m using Android 4.1

Code (in the onclick of the add button)

try{
    validaProdutoLista(item, pedido.getListaItemPedido());

    pedido.adicionaItens(item);

    listViewItensPedidos.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, pedido.getListaItemPedido().size() * 50));

    TabelaItensPedidoAdapter adapter = new TabelaItensPedidoAdapter(PedidoActivity.this, pedido.getListaItemPedido());
    listViewItensPedidos.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    Log.i(TAG, "Itens add "+pedido.getListaItemPedido().size());

    codProduto.setText("");
    codProduto.requestFocus(); //voltar o foco para o produto
}catch (MyException e){
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  • tries to use request Focus in xml

  • If I use requestFocus in xml, the focus already goes to the field as soon as it opens the screen, but that’s not what I need, I need it only when a product is added that the focus goes back to Edittext product.

  • Just like you’re doing should work...

  • 1

    @Jorgeb. this is the bad of Android, what should work sometimes does not work depending on the version and the device... Maybe you have to see another way

  • Alves tries to make codProduto.clearFocus(); before the codProduto.requestFocus();

  • then Jorge, has not changed anything, the focus always goes to the listview editText that was added.

  • If anyone has any other ideas, they are very welcome.. rs

  • What is the name of editText of listview that is receiving the phocus?

  • @Alvesfelipe On the line with codProduto.requestFocus(); change to codProduto.requestFocus(FOCUS_UP, null); and see what happens. and also create a Boolean var to see if the codProduct.requestFocus is returning true or false.

  • ramaral, the name is itbProduct.

  • Olimon, no good, the focus keeps going to the next field, but returns true in the codProduct.requestFocus

Show 6 more comments

1 answer

1

I believe it is not working because you are setting the focus at the moment you are still pressing the button (you are changing the focus within the on click).

A possible solution is to use a Handler. You add a Handler attribute in your class and in onclick you send a message to Handler.

Take this example:

import android.os.Handler;
import android.os.Message;
...
final Handler myHandler = new Handler() {
    public void handleMessage(Message msg) {
        Log.i("HANDLER", "handleMessage::recebendo msg " + msg.what);
        codProduto.setText("");
        codProduto.requestFocus(); //voltar o foco para o produto
    }
};

There in onClick you do, in the place where is the request of the focus you send a message to the Handler:

myHandler.sendEmptyMessage(0);
  • 3

    It would be interesting to include the alternative without Handler using only postRunnable on its own EditText.

  • Derzu, really that’s what happens, I created a Handler and it worked right.

  • Wakim, I did as you suggested and it also worked. Thank you all so much for your help!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.