Java Android Hello World Instructions

From CERES
Jump to: navigation, search
  • Create a new Android Virtual Device (AVD)
  • Create a new Android application project
  • Run and see the behavior
  • Check out src/com.example..., res/layout and res/values
  • Make the layout linear and remove the text-box
  • Add a button and an edit text with the following spec


     <EditText
         android:id="@+id/edit_message"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:hint="@string/enter_text" />
   <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_text" />

       
  • Add a method name (e.g., sendMessage) to handle the OnClick event of the button
 android:onClick="sendMessage"


  • Add sendMessage to src/MainActivity
 public void sendMessage(View view) {
 }
  • Include android.view.View (Ctrl+Shift+o)
  • Add to the body of sendMessage:
       	Intent intent = new Intent(this, ShowMessage.class);

EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent);

and to the class MainActivity:


   public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";


  • Add a new activity (New | Other| Activity)
  • Add the following declarations to the new activity's onCreate():
   Intent intent = getIntent();
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   
   TextView textView = new TextView(this);
   textView.setTextSize(40);
   textView.setText(message);

Back to Embedded Systems Programming