Search code examples
androidandroid-asynctask

Add Text to TextView while AsyncTask Running


I am learning AsyncTask. I want to add a TextView like Loading.... while AyncTask Working. How can I do this

Below is the code

public class MainActivity extends AppCompatActivity {
private EditText et_bookInput;
private TextView tv_titleText;
private TextView tv_authorText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_bookInput = findViewById(R.id.bookInput);
    tv_titleText = findViewById(R.id.titleText);
    tv_authorText = findViewById(R.id.authorText);

}

public void searchBooks(View view) {
    // Get the name of the book from the input field
    String queryString = et_bookInput.getText().toString();
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if(inputMethodManager != null){
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
    new FetchBook(tv_titleText, tv_authorText).execute(queryString);

}

Solution

  • Try the following code:

     tv_authorText.setText("");
     tv_titleText.setText(R.string.loding);
    

    I assumed that at tv_titleText you want to add loading Text.