Issue
I was creating an app then a question come out: how can i keep user in activity? I’ve put two editTexts with input of int. i want user be kept in activity until he/she puts valid numbers in edittexts. any ideas?
Solution
The solution is very simple all you need to do is get the string values from the edit text box and have a condition in your setonclicklistener that checks the strings if they match to a certain value, if yes the intent from current activity will take you to next if not it will show an error on the edit text box (by using .setError()
) and return.
Here is the Java Class Code:
package com.example.text_to_speech;
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.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity);
EditText editText1,editText2;
Button button;
editText1findViewById(R.id.ed1);
editText2findViewById(R.id.ed2);
buttonfindViewById(R.id.check_input);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!editText1.getText().toString().equals("Don"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText1.setError("Input Correct values");
return;
}
if(!editText1.getText().toString().equals("Tom"))
{
Toast.makeText(ActivityA.this, "You Can't Leave this Activity, Input Correct Fields", Toast.LENGTH_SHORT).show();
editText2.setError("Input Correct values");
return;
}
Intent intentnew Intent(ActivityA.this,MainActivity.class);
startActivity(intent);
}
});
}
}
XML Code:
<RelativeLayout
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".ActivityA">
<EditText
android:layout_margin"10dp"
android:id"@+id/ed1"
android:layout_width"wrap_content"
android:layout_height"wrap_content"
android:layout_centerInParent"true"
android:hint"Input Data Here"
android:textColor"#000000"
android:textSize"20sp" />
<EditText
android:layout_margin"10dp"
android:layout_below"@id/ed1"
android:id"@+id/ed2"
android:layout_width"wrap_content"
android:layout_height"wrap_content"
android:layout_centerInParent"true"
android:hint"Input Data Here"
android:textColor"#000000"
android:textSize"20sp" />
<Button
android:layout_width"wrap_content"
android:layout_height"wrap_content"
android:layout_below"@id/ed2"
android:id"@+id/check_input"
android:layout_centerHorizontal"true"
android:layout_centerVertical"true"
android:layout_margin"20dp"
android:text"Go to Next Activity"
/>
</RelativeLayout>
Output:
Answered By – MoonLight