Advertisement
yo2man

Android TNB Make Your App Interactive

Jun 2nd, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. //Android TNB Make Your App Interactive
  2. //---------------------------------------------------------------------------------------------------------------------------------------
  3. //strings.xml
  4. <resources>
  5.     <string name="app_name">Event Handling: How To Make Your App Interactive</string>
  6.  
  7.     <string name="hello_world">Hello world!</string>
  8.     <string name="action_settings">Settings</string>
  9.     <string name="message_text">Hello</string>
  10.     <string name="button_text">Click me Hoss</string>
  11. </resources>
  12.  
  13.  
  14. //---------------------------------------------------------------------------------------------------------------------------------------
  15. //MainActivity.java
  16. package com.thenewboston.eventhandlinghowtomakeyourappinteractive;
  17.  
  18. import android.support.v7.app.ActionBarActivity;
  19. import android.os.Bundle;
  20. import android.view.Menu;
  21. import android.view.MenuItem;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.TextView;
  25.  
  26. public class MainActivity extends ActionBarActivity {
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.  
  33.  
  34.         Button buckysButton = (Button)findViewById(R.id.buckysButton);
  35.  
  36.         buckysButton.setOnClickListener(
  37.                 new Button.OnClickListener(){
  38.                     public void onClick(View v){
  39.                         TextView buckysText = (TextView)findViewById(R.id.buckysText);
  40.                         buckysText.setText("Good Job Hoss!");
  41.                     }
  42.                 }
  43.         );
  44.  
  45.         buckysButton.setOnLongClickListener(
  46.                 new Button.OnLongClickListener(){
  47.                     public boolean onLongClick(View v){
  48.                         TextView buckysText = (TextView)findViewById(R.id.buckysText);
  49.                         buckysText.setText("Holy carp, that was a long one!");
  50.                         return true; //whenever we have an OnLongClick we need a "return true"
  51.                     }
  52.                 }
  53.  
  54.         );
  55.  
  56.     }
  57.  
  58.     @Override
  59.     public boolean onCreateOptionsMenu(Menu menu) {
  60.         // Inflate the menu; this adds items to the action bar if it is present.
  61.         getMenuInflater().inflate(R.menu.menu_main, menu);
  62.         return true;
  63.     }
  64.  
  65.     @Override
  66.     public boolean onOptionsItemSelected(MenuItem item) {
  67.         // Handle action bar item clicks here. The action bar will
  68.         // automatically handle clicks on the Home/Up button, so long
  69.         // as you specify a parent activity in AndroidManifest.xml.
  70.         int id = item.getItemId();
  71.  
  72.         //noinspection SimplifiableIfStatement
  73.         if (id == R.id.action_settings) {
  74.             return true;
  75.         }
  76.  
  77.         return super.onOptionsItemSelected(item);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement