Difference between revisions of "Android Studio: Start another activity"

From OnnoWiki
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub:
 
file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub:
  
 +
import androidx.appcompat.app.AppCompatActivity;
 +
import android.os.Bundle;
 +
import android.view.View;
 +
 
  public class MainActivity extends AppCompatActivity {
 
  public class MainActivity extends AppCompatActivity {
 
     @Override
 
     @Override
Line 15: Line 19:
 
     }
 
     }
 
  }
 
  }
 +
 +
 +
 +
  
 
Klik View > Alt+Enter > Import class
 
Klik View > Alt+Enter > Import class
Line 21: Line 29:
 
* klik button > Attribute > onClick property > sendMessage [MainActivity]
 
* klik button > Attribute > onClick property > sendMessage [MainActivity]
  
 +
Penampakan activity_main.xml
 +
 +
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 +
    xmlns:app="http://schemas.android.com/apk/res-auto"
 +
    xmlns:tools="http://schemas.android.com/tools"
 +
    android:layout_width="match_parent"
 +
    android:layout_height="match_parent"
 +
    tools:context=".MainActivity">
 +
 +
    <Button
 +
        android:id="@+id/button2"
 +
        android:layout_width="wrap_content"
 +
        android:layout_height="wrap_content"
 +
        android:onClick="sendMessage"
 +
        android:text="Button"
 +
        app:layout_constraintBottom_toBottomOf="parent"
 +
        app:layout_constraintEnd_toEndOf="parent"
 +
        app:layout_constraintStart_toStartOf="parent"
 +
        app:layout_constraintTop_toTopOf="parent" />
 +
</androidx.constraintlayout.widget.ConstraintLayout>
  
 
==Build an intent==
 
==Build an intent==
  
 
Tambahkan di MainActivity
 
Tambahkan di MainActivity
 +
 +
import androidx.appcompat.app.AppCompatActivity;
 +
import android.os.Bundle;
 +
import android.view.View;
 +
import android.content.Intent;
 +
import android.widget.EditText;
 
   
 
   
 
  public class MainActivity extends AppCompatActivity {
 
  public class MainActivity extends AppCompatActivity {
Line 37: Line 73:
 
     public void sendMessage(View view) {
 
     public void sendMessage(View view) {
 
         Intent intent = new Intent(this, DisplayMessageActivity.class);
 
         Intent intent = new Intent(this, DisplayMessageActivity.class);
         EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
+
         EditText editText = (EditText) findViewById(R.id.editText);
 
         String message = editText.getText().toString();
 
         String message = editText.getText().toString();
 
         intent.putExtra(EXTRA_MESSAGE, message);
 
         intent.putExtra(EXTRA_MESSAGE, message);
Line 43: Line 79:
 
     }
 
     }
 
  }
 
  }
 +
 +
  
 
Tambahkan
 
Tambahkan
Line 51: Line 89:
 
  import android.view.View;
 
  import android.view.View;
 
  import android.widget.EditText;
 
  import android.widget.EditText;
 
  
 
==Create the second activity==
 
==Create the second activity==
Line 59: Line 96:
 
* Project window > app (Right Click) > New > Activity > Empty Activity.
 
* Project window > app (Right Click) > New > Activity > Empty Activity.
 
* Configure Activity window
 
* Configure Activity window
   Name   DisplayMessageActivity
+
   ActivityName   DisplayMessageActivity
   Finish.
+
   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 Class
 +
 
 +
package itts.onno.hms.myapplication;
 +
import androidx.appcompat.app.AppCompatActivity;
 +
import android.os.Bundle;
 +
import android.content.Intent;
 +
import android.widget.TextView;
 +
 +
public class DisplayMessageActivity extends AppCompatActivity {
 +
 +
    @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);
 +
    }
 +
}
  
==Create the second activity==
 
  
 +
==activity_display_message.xml==
 +
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 +
    xmlns:app="http://schemas.android.com/apk/res-auto"
 +
    xmlns:tools="http://schemas.android.com/tools"
 +
    android:layout_width="match_parent"
 +
    android:layout_height="match_parent"
 +
    tools:context=".DisplayMessageActivity">
 +
 +
    <TextView
 +
        android:id="@+id/textView"
 +
        android:layout_width="wrap_content"
 +
        android:layout_height="wrap_content"
 +
        android:text="@string/textview"
 +
        android:textSize="16sp"
 +
        app:layout_constraintBottom_toBottomOf="parent"
 +
        app:layout_constraintEnd_toEndOf="parent"
 +
        app:layout_constraintStart_toStartOf="parent"
 +
        app:layout_constraintTop_toTopOf="parent" />
 +
</androidx.constraintlayout.widget.ConstraintLayout>
  
 +
==Add upward navigation==
  
 +
Menambahkan Up button, file app > manifests > AndroidManifest.xml,
  
 +
<activity android:name=".DisplayMessageActivity"
 +
          android:parentActivityName=".MainActivity">
 +
    <!-- The meta-data tag is required if you support API level 15 and lower -->
 +
    <meta-data
 +
        android:name="android.support.PARENT_ACTIVITY"
 +
        android:value=".MainActivity" />
 +
</activity>
  
 
==Referensi==
 
==Referensi==
  
 +
* https://developer.android.com/training/basics/firstapp/starting-activity
 
* https://developer.android.com/training/basics/firstapp/starting-activity
 
* https://developer.android.com/training/basics/firstapp/starting-activity

Latest revision as of 13:19, 19 March 2022

Respond to the Send button

file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

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]

Penampakan activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Build an intent

Tambahkan di MainActivity

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;

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.editText);
        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 Class

package itts.onno.hms.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    @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);
    }
}


activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayMessageActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Add upward navigation

Menambahkan Up button, file app > manifests > AndroidManifest.xml,

<activity android:name=".DisplayMessageActivity"
          android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

Referensi