Difference between revisions of "Android Studio: Start another activity"
Jump to navigation
Jump to search
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
| Line 68: | Line 68: | ||
* Palette panel > Text, TextView (di drag ke layout) | * Palette panel > Text, TextView (di drag ke layout) | ||
* Jika di perlukan tambahkan constraint | * Jika di perlukan tambahkan constraint | ||
| + | * set textSize | ||
| + | |||
| + | ==Display the message== | ||
| + | |||
| + | In DisplayMessageActivity | ||
| + | |||
| + | @Override | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); | ||
| + | setContentView(R.layout.activity_display_message); | ||
| + | |||
| + | // Get the Intent that started this activity and extract the string | ||
| + | Intent intent = getIntent(); | ||
| + | String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); | ||
| + | |||
| + | // Capture the layout's TextView and set the string as its text | ||
| + | TextView textView = findViewById(R.id.textView); | ||
| + | textView.setText(message); | ||
| + | } | ||
| + | |||
| + | tambahkan | ||
| + | |||
| + | import androidx.appcompat.app.AppCompatActivity; | ||
| + | import android.content.Intent; | ||
| + | import android.os.Bundle; | ||
| + | import android.widget.TextView; | ||
==Referensi== | ==Referensi== | ||
* https://developer.android.com/training/basics/firstapp/starting-activity | * https://developer.android.com/training/basics/firstapp/starting-activity | ||
Revision as of 07:59, 4 March 2022
Respond to the Send button
file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
Klik View > Alt+Enter > Import class
file app > res > layout > activity_main.xml
- klik button > Attribute > onClick property > sendMessage [MainActivity]
Build an intent
Tambahkan di MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Tambahkan
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText;
Create the second activity
Lakukan:
- Project window > app (Right Click) > New > Activity > Empty Activity.
- Configure Activity window
ActivityName DisplayMessageActivity Finish
Add a text view
- app > res > layout > activity_display_message.xml.
- Enable Autoconnection to Parent
- Palette panel > Text, TextView (di drag ke layout)
- Jika di perlukan tambahkan constraint
- set textSize
Display the message
In DisplayMessageActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
tambahkan
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView;