Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value in edittext don't show until it get focus. Android

I'm building an app that get its layout based on external data. The layout is split into different blocks depending on the indata. These blocks are displayed with the help of a viewflipper. In each block there is, for the time being, one or more "textview" and "edittext". On the first page of the flipper all data is showing as it should in the textviews and edittexts. But on the other pages in the viewflipper the value in the edittexts is not showing until the edittext get focused. I have no idea why. So to be clear. The values for all edittexts is actually there, but doesn't show until the edittext get focused. The problem is the same on all devices I have runned the app on (emulator, HTC Desire, HTC Wildfire). Does anyone know how to fix this problem?

Here is the class that generate the layout:

public class ModuleLayout extends LinearLayout {

public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
    super(context);
    TableLayout tempTable = new TableLayout(context);

        for (int i = 0; i < itemList.size(); i++)
        {
            TableRow tempRow = new TableRow(context);
            TextView tempTextView = new TextView(context);
            tempTextView.setText(itemList.get(i).getName());

            EditText tempEditText = new EditText(context);
            tempEditText.setText(itemList.get(i).getItemValue());

            tempRow.addView(tempTextView);
            tempRow.addView(tempEditText);

            tempTable.addView(tempRow);
        }

        tempTable.setColumnStretchable(1, true);
        this.addView(tempTable);
    }
}

Here is a picture of the problem in action.

The left picture displays all its values fine. The right picture is on the second position in the viewflipper and does not display the values in the edittexts, with the exception for the first that have focus. I should also mention that after an edittext has gotten focus it continues to display the value even if I fling to other views in the viewflipper and then back.

like image 508
taktiger Avatar asked May 05 '11 12:05

taktiger


People also ask

How can I detect focused EditText in Android?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class.

How do you Unfocus textfield on Android?

setFocusable(false); editText. setFocusableInTouchMode(true); editText. setFocusable(true); EditText will lose focus, but can gain it again on a new touch event.


1 Answers

That Layout from how it appears here is not on the UI thread meaning no matter what you change you won't see anything until the UI thread (also known as the Main Thread) updates. The UI/Main thread is what all your activities run through so it's easy to think that stuff just updates automatically (since most of the previous work you have done is also somewhere within the Activity, I am assuming). The onFocusChanged event (clicking in the edittext) will update the UI thread by calling invalidate() on itself. You can force it to update by calling EditText.invalidate() if you need to, but it's usually best to have it update naturally after creation. Without seeing the rest of your code this is the best I can do for you, I hope this helps :)

like image 121
BinaryBazooka Avatar answered Oct 25 '22 22:10

BinaryBazooka