Skip to content

Latest commit

 

History

History
56 lines (35 loc) · 1.32 KB

File metadata and controls

56 lines (35 loc) · 1.32 KB

Enhanced User Input Project

Based on the previous demo project, this version includes the following improvements:

  • Button click using setOnClickListener
  • Toast implementation
  • Hiding keyboard after button press

Updated version


Snippet

Button click or tap

 public class MyActivity extends Activity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.content_layout_id);

         final Button button = findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Code here executes on main thread after user presses button
             }
         });
     }
 }

Toast

    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;
    
    Toast.makeText(context, text, duration).show();;
    

Hiding keyboard on button press

    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow((null == getCurrentFocus()) ? null : getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);