The graphical user interface for an Android app is built using a hierarchy of View
and ViewGroup
objects. View
objects are usually UI widgets such as buttons or text fields. ViewGroup
objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.
Layouts are subclasses of the ViewGroup
. In this exercise, you'll work with a LinearLayout
.
> Run Your App: adb install -r app/build/outputs/apk/app-debug.apk
/** 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); }>
public class DisplayMessageActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle app bar item clicks here. The app bar // automatically handles clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_display_message, container, false); return rootView; } } }
Note: If you are using an IDE other than Android Studio, your project does not contain theactivity_display_message
layout that's requested by setContentView()
. That's OK because you will update this method later and won't be using that layout.
>In your manifest file, AndroidManifest.xml
, within the Application
element, add the <activity>
element for your DisplayMessageActivity
class, as follows:
<application ... > ... <activity android:name="com.mycompany.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.mycompany.myfirstapp.MyActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.mycompany.myfirstapp.MyActivity" /> </activity> </application>
The system uses this value to implement default navigation behaviors, such as Up navigationon Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for older versions of Android by using the Support Library and adding the <meta-data>
element as shown here.
> The complete onCreate()
method for DisplayMessageActivity
now looks like this:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null) .show(); } }); getSupportActionBar().setDisplayHomeAsUpEnabled(true); Intent intent = getIntent(); String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); layout.addView(textView);