Advertisement
hendroprwk08

Untitled

Aug 22nd, 2020
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. ...
  2.  
  3. public class MainActivity extends AppCompatActivity {
  4.  
  5.    ...
  6.  
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.  
  11.         ...
  12.     }
  13.  
  14.     @Override
  15.     public boolean onCreateOptionsMenu(Menu menu) {
  16.         MenuInflater inflater = getMenuInflater();
  17.         inflater.inflate(R.menu.menu_main, menu);
  18.  
  19.         //khusus proses search
  20.         MenuItem item = menu.findItem(R.id.cari);
  21.         SearchView searchView = (SearchView) item.getActionView();
  22.         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  23.             @Override
  24.             public boolean onQueryTextSubmit(String s) { //ketika tekan enter
  25.                 Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  26.                 return false;
  27.             }
  28.  
  29.             @Override
  30.             public boolean onQueryTextChange(String s) { //ketika text berubah
  31.                 return false;
  32.             }
  33.         });
  34.  
  35.         return super.onCreateOptionsMenu(menu);
  36.     }
  37.  
  38.     @Override
  39.     public boolean onOptionsItemSelected(MenuItem item) {
  40.         switch (item.getItemId()) {
  41.             case R.id.tambah:
  42.                 View view = findViewById(R.id.main_layout_id); //layout main activity harus constraint
  43.                 String message = "Snackbar action Tambah";
  44.                 int duration = Snackbar.LENGTH_SHORT; //alt + enter pada "Snackbar" untuk mendownload dependency
  45.                 Snackbar.make(view, message, duration).show();
  46.                 return true;
  47.             case R.id.selip:
  48.                 Toast.makeText(getApplicationContext(), "action Selip", Toast.LENGTH_SHORT).show();
  49.                 return true;
  50.             case R.id.auto_update:
  51.                 Toast.makeText(getApplicationContext(), "action Update", Toast.LENGTH_SHORT).show();
  52.                 return true;
  53.             default:
  54.                 return super.onOptionsItemSelected(item);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement