3
votes

I have developed an application where i am using the same textview again and again so i need to clear textview very frequently.

Since it is related to UI i have to perform this task on UI thread but it takes 5-6 milliseconds to clear the textview only which seems very huge i guess. Can anybody help me to reduce this time so i can manage my UI task very smoothly without skipping any frames?

I am attaching my code snippet for a reference where i have tried two methods but the least time was 5 milliseconds.


    public class MainActivity extends AppCompatActivity {
    EditText editText;
    Button btnClearText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.editText);
        btnClearText = findViewById(R.id.btnClearText);
        btnClearText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // test with empty text
                editText.setText("");

                // try with setText('');
                Log.e("clearText", "setText(''): start "+ new Date().getTime());
                editText.setText("");
                Log.e("clearText", "setText(''): end" + new Date().getTime()); // difference of 15 millisecond

               // try with getText().clear();
                Log.e("clearText", "getText().clear(): start" + new Date().getTime());
                editText.getText().clear();
                Log.e("clearText", "getText().clear(): end" + new Date().getTime()); // difference of 5 millisecond
            }
        });
    }
}



Any suggestions or alternate methods to improve performance?

Thanks,

1
And why is it that you use same textview/edittext?sanjeev
you can try <string name="empty" /> to strings.xml and use textView.setText(R.string.empty). and also textview.settext(null)Majid Ali
I have developed a chat application where i am using a same edit text to send a message and once we press send button i need to clear edit text to type a new message.vavadiya hiren
@MajidAli i have already tried that but it takes more time you can check in my code snippet.vavadiya hiren

1 Answers

-1
votes

Well the one of the best effective way to clear edit text use Data Binding

By using Data Binding, edittext effectively observe the value and clear the text .

You can also use MVVM Architecture for better performance improvement.

Alternatively just use

et.setText("");

Or

et_city.getText().clear();