I'm trying to learn basic stuff in android studio with java. I need to create 1 activity where we need a Button and a text to input. When the button is clicked to check is the input value is between 3 words "Primary, Secondary , Higher). Here is the mainactivity code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private int check = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// Check for valid education
// if yes -> next view
if (check == 0 ) {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
} else if ( check == 1) {
startActivity(new Intent(this, Activity2.class));
}
}
}
CodePudding user response:
Follow this way
add input field in you xml file above button view .
<EditText
android:id="@ id/edu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your education" />
In your mainactivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private EditText edu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edu = findViewById(R.id.edu);
button = findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v){
String check_edu= edu.getText().toString().toLowerCase();
if ( check_edu.equals("primary") || check_edu.equals("secondary") || check_edu.equals("higher") ){
startActivity(new Intent(this, Activity2.class));
} else {
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
}
});
}
}