- Open Android Studio
- Start A New Android Studio Project
- Name it "Hello"
- Change the domain to ladev.com
- Change the location as you wish
- Next
- Choose the defaults for Target Android Devices (Phone and Tablet Only)
- Next
- Choose an Empty Activity
- Next
- Leave the defaults
- Next
- Finish (Wait)
- Expand the Res Folder in the Project View, then expand the Layouts folder
- Open activity_main.xml
- Click the Hello World TextView
- In the Properties Pane, change the text to something different.
- In the main Toolbar, click the Run button
-
Open Android Studio
-
Start A New Android Studio Project
-
Name it "CatchPhrase" (no spaces)
-
Change the domain to ladev.com
-
Change the location as you wish
-
Next
-
Choose the defaults for Target Android Devices (Phone and Tablet Only)
-
Next
-
Choose an Empty Activity
-
Next
-
Leave the defaults
-
Next
-
Finish (Wait)
-
Expand the Res Folder in the Project View, then expand the values folder
-
Open strings.xml
-
Add the following XML snippet:
<string name="quote_bang">Bazinga!</string> <string name="quote_seinfeld">Hello Neuman</string> <string name="quote_friends">How you Doin?</string> <string name="quote_mother">Suit Up!</string>
-
Expand the Res Folder in the Project View, then expand the layouts folder
-
Open activity_main.xml
-
Change "android.support.constraint.ConstraintLayout" to "RelativeLayout"
-
Delete the entire TextView element
-
Add the following XML Snippet inside the RelativeLayout
<Button android:id="@+id/bang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="60dp" android:layout_marginTop="100dp" android:text="@string/quote_bang"/> <Button android:id="@+id/friend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/bang" android:layout_toRightOf="@id/bang" android:layout_marginLeft="50dp" android:text="@string/quote_friends"/> <Button android:id="@+id/mother" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/bang" android:layout_below="@id/bang" android:layout_marginTop="50dp" android:text="@string/quote_mother"/> <Button android:id="@+id/seinfeld" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_toRightOf="@id/bang" android:layout_below="@id/bang" android:layout_marginTop="50dp" android:text="@string/quote_seinfeld"/>
-
Right-Click the com.ladev.catchphrase package in the Project pane
-
Add a new Empty Activity
-
Name it Display
-
Finish
-
Open activity_display.xml
-
Change "android.support.constraint.ConstraintLayout" to "RelativeLayout"
-
Add a TextView
<TextView android:id="@+id/phrase" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textSize="36sp"/>
-
Open MainActivity.java
-
Create a private inner class for handling button taps
private class ButtonListener implements View.OnClickListener { @Override public void onClick(View v) { Button button = (Button) v; String phrase = button.getText().toString(); Intent displayIntent = new Intent(MainActivity.this, Display.class); displayIntent.putExtra("phrase", phrase); startActivity(displayIntent); } }
-
Wire up each button to an instance of the ButtonListener class in the onCreate method below the layout inflation:
Button bangButton = (Button) findViewById(R.id.bang); bangButton.setOnClickListener(new ButtonListener()); Button friendButton = (Button) findViewById(R.id.friend); friendButton.setOnClickListener(new ButtonListener()); Button seinfeldButton = (Button) findViewById(R.id.seinfeld); seinfeldButton.setOnClickListener(new ButtonListener()); Button motherButton = (Button) findViewById(R.id.mother); motherButton.setOnClickListener(new ButtonListener());
-
Open Display.java
-
Retrieve the data from the intent and load it into the TextView
Intent intent = getIntent(); String phrase = intent.getStringExtra("phrase"); TextView display = (TextView) findViewById(R.id.phrase); display.setText(phrase);
-
Try the app in the emulator or device