Android java button value check - java

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

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

Related

Java Android: the button does not work after returning to the first layout

when I press two buttons the picture changes but when I move with the 3 button to the 2nd layout and I go back to the first picture it doesn't change anymore
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button changeLayout;
private Button exit;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
imageView = findViewById(R.id.imageView);
changeLayout = findViewById(R.id.changeLayoutBt);
exit = findViewById(R.id.exitBtn);
}
public void changeImage(View view) {
switch (view.getId()){
case R.id.button1:
imageView.setImageResource(R.drawable.orange);
break;
case R.id.button2:
imageView.setImageResource(R.drawable.java);
break;
}
}
public void changeLayout(View view) {
setContentView(R.layout.second_layout);
}
public void exit(View view) {
setContentView(R.layout.activity_main);
}
}
`
In order to create new layout got to File>New>Activity>Empty Activity
Change your changeLayout code to this one
public void changeLayout() {
startActivity(new Intent(MainActivity.this, Main2Activity.class);
}
Main2Activity.class is your newly created java class. It would be Main2Activity.java or what ever you named it.
Inside your onCreate() method. Just below
changeLayout = findViewById(R.id.changeLayoutBt);
Paste this code
changeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeLayout();
}
});

Android Studio / How to apply SharedPreferenced to my code

I'm doing Quiz in Android Studio. The game is divided into two categories. After passing the first category, the second button (opening the second category) changes the status from setEnable "false" to "true". How to use the SharedPreferenced method in my code that the changes related to the second button (.setEnable) will be saved after closing the application.
Last level of first category
public class win extends AppCompatActivity implements View.OnClickListener{
Button win1;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_winflagi);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
win1 = (Button) findViewById(R.id.winflagi);
win1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==win1)
{
Intent myIntent = new Intent(win.this, Activity2.class);
myIntent.putExtra("isEnabled", "enabled");
startActivity(myIntent);
}
}
}
Class containing a buttons for two categories ...
button3 opened the first category
entrycity opens the second category
public class Activity2 extends AppCompatActivity implements View.OnClickListener{
Button button3;
Button entrycity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
entrycity = (Button) findViewById(R.id.entrycity);
entrycity.setOnClickListener(this);
}
#Override
public void onClick(final View v) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.menu);
if (v == button3) {
startActivity(new Intent(Activity2.this, flagi1.class));
Bungee.zoom(this);
mp.start();
}
if (v== entrycity){
Intent intent=getIntent();
String isEnabled = intent.getStringExtra("isEnabled");
if(isEnabled==null||isEnabled.equals("disabled")){
entrycity.setEnabled(false);
}
else{
entrycity.setEnabled(true);
startActivity(new Intent(this, cities1.class));
}
}
}
}
Set a boolean preference when you are enabling the Button.
else{
entrycity.setEnabled(true);
getSharedPreferences("MY_PREF", MODE_PRIVATE).edit().putBoolean("isEnabled",true).apply();
startActivity(new Intent(this, cities1.class));
}
and in your onCreate set the enabled status of the button based on the preference :
entrycity.setEnabled(getSharedPreferences("MY_PREF", MODE_PRIVATE).getBoolean("isEnabled",false));

Passing EditText integer to TextView via Intent app crashes after clicking (button) three

I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}

How do I check if my editText field is empty and make it go to a new activity with a Button?

I have made this code :
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
});
}
else
{
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
}
The problem is that if I pressed the button with or without entering anything in the field, it will say :
You did not enter a username
And the next activity will not work neither.
How do I make the button to move the user into the next activity while making sure he inputed a name?
Use this, I have modified your code..
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ed_text = operand1.getText().toString().trim();
if(ed_text.equals(""))
{
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
else
{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
});
}
You need to do the check when the button is pressed, here is an example:
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(operand1.getText().length() == 0){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}else{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
});
}
}
The problem is that you are not checking the value in the listener. Change it to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (operand1.getText().toString().trim().length() > 0) {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
} else {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
}
});
}
Also onCreate gets called when the Activity is created so you can't check the name field's value in there. That's why you need to move that check in your button's click listener. Then it will always check if the name field is empty when you click your button.
You can try this code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.gobutton) {
if(operand1.getText().toString().trim().equals("")){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show()
}else{
Intent myIntent = new Intent(this,ScreenName.class);
startActivity(myIntent);
}
}
}
Try this instead...
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString().trim();
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
} else {
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
}
you can use this
public class MainActivity extends AppCompatActivity {
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
String ed_text = operand1.getText().toString();
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed_text.equalsignorcase("")
{
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show();
}
else
{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
});
}
cahnge your code like that
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageButton ourButton;
private EditText operand1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find Views by IDs :
ourButton = (ImageButton) findViewById(R.id.gobutton);
operand1 = (EditText) findViewById(R.id.edit_Name);
ourButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
String ed_text = operand1.getText().toString().trim();
if(ed_text.equals("")){
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_LONG).show()
}else{
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
MainActivity.this.startActivity(myIntent);
}
}
}
Don't set two OnClickListeners for one button
Try following code -
String ed_text = operand1.getText().toString().trim();
ourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!TextUtils.isEmpty(ed_text)){
Toast.makeText(MainActivity.this, "Not Empty", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, ScreenName.class);
startActivity(myIntent);
finish(); //If you want to kill current Activity.
}else{
Toast.makeText(MainActivity.this, "Empty", Toast.LENGTH_SHORT).show();
}
}
});
Hope it will help :)

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

Categories