-2

I am new to this Java programming and android development and still just working with Hello World App while following a youtube video Series.

Youtube tutorial link

Need to change the interface from send Messeage Button press to Welcome Message screen.

My project have two Java claases

MainActivity.java

package com.example.helloworldapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
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);
    }

    // Specify the onclick method for the button
    // Access modifier must be public
    // Method name is send Message
    // Specify parameter and object of view class
    //WHen user click the button system will invoke this method
    //With in the method have to create newly created activity
    //TO create a new activity have to create a object of intend
    // intent is intention of doing something by the android application
    // E.g. Start a new activity start a new service broadcast a message

    public void sendMessage (View view){

        //TO create a new activity have to create a object of intent
        //Have to pass 2 parameter 1. context and 2. class name of the target activity
        // Context name is this, target activity is MessageActivity

        Intent intent = new Intent(this,MessageActivity.class);
        //To start the activity have to call the method call start activity
        //Then pass intent parameter
        startActivity(intent);

    }

}

MessageAcitivity.java

package com.example.helloworldapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
    }

}

Screenshot of activity_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"
    android:onClick="sendMessage"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="156dp"
        android:layout_height="54dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/edit_message"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="154dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="@string/button_label"
        app:layout_constraintBaseline_toBaselineOf="@+id/editTextTextPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot 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=".MessageActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/welcome_message"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Harsha Madusanka
  • 337
  • 1
  • 3
  • 12

2 Answers2

0

You need to handle your button-click listener to navigate to the next screen.

public class MainActivity extends AppCompatActivity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

button = findViewById(R.id.button);
button.setOnClickListener(v -> openMessageAcitivity());
}


public void openMessageAcitivity (View view){
    String message = "Your message";

    Intent intent = new Intent(this,MessageActivity.class);
    intent.putExtra("STRING_YOU_NEED", message);
    startActivity(intent);
    
}

}

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_YOU_NEED");
    }
} 
Asif A. Sohan
  • 202
  • 1
  • 5
-2

The button click not working could be solved via declaring sendMessage in the layout activity_main.xml in the Button code inserted android:onClick = "sendMessage"

If someone can elaborate on the above it will be very helpful since it is not mentioning in the tutorial itself. Onlick attribute in the layout has alrady as sendMessage as instructed in the tutorial.

Harsha Madusanka
  • 337
  • 1
  • 3
  • 12