hierarchy of View and ViewGroup :
View objects
are usually UI widgets such as buttons or text
fields andViewGroup objects
are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.ViewGroup is subclass of View.
LinearLayout is a subclass of ViewGroup is
a view group and is a view group.
<EditText android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" />
The at sign (@) is required when you're referring to any resource object from XML. It is
followed by the resource type (id in this case).
Make the Input Box Fill in the Screen Width
<EditText android:layout_weight="1" android:layout_width="0dp" ... />
Setting the width to zero improves layout performance because using"wrap_content" as the width requires the system to calculate a width that is ultimately irrelevant.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />
/** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button }
the method must:
- Be public
- Have a void return value
- Have a View as the only parameter (this will be the View that was clicked)
Build an Intent:
An Intent is
an object that provides runtime binding between separate components (such as two activities).
Intent intent = new Intent(this, DisplayMessageActivity.class);
The constructor used here takes two parameters:
- A Context as its first parameter (this is
used because the Activity class is a subclass of Context) - in this case, the activity that should be started.
An Intent can carry a collection
of various data types as key-value pairs called extras. The putExtra()method
takes the key name in the first parameter and the value in the second parameter.
intents
can also be implicit, in which case the Intent does
not specify the desired component, but allows any app installed on the device to respond to the intent as long as it satisfies the meta-data specifications for the action that's specified in various Intent parameters.
/** Called when the user clicks the Send button */ public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); }
startActivity(),The
system receives this call and starts an instance of the Activity specified by the Intent.
Every Activity is
invoked by an Intent, regardless of how the user navigated there. You can get
the Intentthat started your activity by calling getIntent() and
retrieve the data contained within it.
Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Then add theTextView as the root
view of the activity’s layout by passing it to setContentView().
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView); }