2

i'm beginner in android coding and im trying to switch activities using Intent class but it do nothing on all cases (Putting data or inputting data) can someone resolve my problem ? here is my layout.xml : i already added the other activity in the manifiest xml i think the problem is in where i get data from the spinner or from the radioButton


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    tools:context=".MainActivity">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Formulaire d'inscription"
            android:textAlignment="center"
            android:textSize="30dp"
            android:textColor="#FF0000"
            android:gravity="center_horizontal" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Nom" />
        <EditText
            android:id="@+id/txtNom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Saisir votre nom"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Prenom :" />
        <EditText
            android:id="@+id/txtPrenom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Saisir votre prénom"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Email :" />
        <EditText
            android:id="@+id/txtEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="Saisir votre email"
            />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Mot de passe" />
        <EditText
            android:id="@+id/txtPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Saisir le mot de passe"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Confirmation :" />
        <EditText
            android:id="@+id/txtPassConfirm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="Confirmation"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Genre" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <RadioGroup
                android:id="@+id/radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rbh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Homme"
                    android:checked="true">

                </RadioButton>
                <RadioButton
                    android:id="@+id/rbf"
                    android:layout_marginLeft="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Femme">

                </RadioButton>
            </RadioGroup>



        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="250px"
            android:layout_height="wrap_content"
            android:text="Pays :" />

        <Spinner
            android:id="@+id/spnPays"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/pays" />

    </LinearLayout>


    <LinearLayout
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <Button
            android:id="@+id/btnSignUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="INSCRIRE"
            />
    </LinearLayout>

</LinearLayout>

and my java code is :


package com.fach.app1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public static String nomInscrit;
    public static String prenomInscrit;
    public static String emailInscrit;
    public static String passwordInscrit;
    public static String payInscrit;
    public static String genreInscrit;

    Button btnSign ;
    EditText nom ;
    EditText prenom ;
    EditText email ;
    EditText password ;
    EditText passConf;
    Spinner spnPay;
    RadioGroup genre;
    RadioButton gen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSign = findViewById(R.id.btnSignUp);
        nom =  findViewById(R.id.txtNom);
        prenom = findViewById(R.id.txtPrenom);
        email = findViewById(R.id.txtEmail);
        password = findViewById(R.id.txtPass);
        passConf = findViewById(R.id.txtPassConfirm);
        spnPay =  findViewById(R.id.spnPays);
        genre =  findViewById(R.id.radiogroup);

        int selectedId = genre.getCheckedRadioButtonId();
        gen = findViewById(selectedId);
        btnSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                        nomInscrit = nom.getText().toString();
                        prenomInscrit = prenom.getText().toString();
                        emailInscrit = email.getText().toString();
                        passwordInscrit = password.getText().toString();
                        payInscrit = spnPay.getSelectedItem().toString();
                        genreInscrit = gen.getText().toString();



                        if(nom.getText().equals("") || prenom.getText().equals("") || email.getText().equals("") || password.getText().equals("")){
                            Toast.makeText(MainActivity.this,"Veuillez remplir tous les champs", Toast.LENGTH_SHORT);
                        }
                        else if(!password.getText().equals(passConf.getText())){
                            Toast.makeText(MainActivity.this,"Les mots de passe doivent etre identique", Toast.LENGTH_SHORT);
                        }
                        else{
                            switchActivities();
                        }


                    }
                });
    }

    private void switchActivities() {
        Intent ActivityIntent = new Intent(this, DataActivity.class);
        startActivity(ActivityIntent);

    }

}


Thanks for reading and resolving my problem

Kamal Fach
  • 79
  • 6

1 Answers1

1

You need to pass the data using the ActivityIntent you have created in switchActivity function amd then get that data in your second Activity from getIntent().getString(key) function.

Please refer to this, How do I pass data between Activities in Android application?

ganeshraj020794
  • 188
  • 2
  • 13
  • thank you @ganeshraj020794 i will try using intent.putExtra to pass data to other activity but the problem is onClickListner doesnt do nothing :( – Kamal Fach Jun 30 '21 at 18:55
  • @KamalFach First of all, make sure all the views are being correctly initialized (All the id names are matching correctly), Second try to debug the onClick method and see if it gets called properly or if its failing inside – Ofek Jun 30 '21 at 19:47