I'm a newbie in android. I have got four buttons when I click my first tab. I want user to click the first button first rather than randomly clicking other buttons first. so need to disable other buttons until my first button is clicked and once the first button is clicked and when the user returns back all the buttons need to be enabled. How do I do this? Please help!!
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.imgbtn_details)
{
attendees_imgbtn.setEnabled(true);
resources_imgbtn.setEnabled(true);
contacts_imgbtn.setEnabled(true);
count = 1;
Intent detail_intent=new Intent(getActivity().getApplicationContext(),DetailsActivity.class);
detail_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(detail_intent);
}
else if(v.getId() == R.id.imgbtn_attendees && count == 0)
{
if(count == 1)
{
Intent attendee_intent = new Intent(getActivity().getApplicationContext(),AttendeesActivity.class);
attendee_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(attendee_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
else if(v.getId() == R.id.imgbtn_resources && count == 0)
{
if(count == 1)
{
Intent resources_intent = new Intent(getActivity().getApplicationContext(),ResourcesActivity.class);
resources_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(resources_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
else if(v.getId() == R.id.imgbtn_contacts && count == 0)
{
if(count == 1)
{
Intent contact_intent = new Intent(getActivity().getApplicationContext(),ContactsActivity.class);
contact_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(contact_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
}
Use setEnable(true) for buttons to enable and disable buttons
I think you need to pass variable specifying that you want to enable buttons or not.
First time, pass it as:
Intent intent = new Intent (this, NextAct.class);
intent.putExtra ("ENABLE", "No");
When you want to enable the buttons, pass String as Yes:
Intent intent = new Intent (this, NextAct.class);
intent.putExtra ("ENABLE", "YES");
Then check in your NextAct class for this string.
Intent intent = getIntent();
String enableBtn = intent.getStringExtra("ENABLE");
if (enableBtn != null && enableBtn.equalsIgnoreCase("YES")) {
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
} else {
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
}
So your buttons will be enabled only when you want them to.
Hope that helps.
define a static Boolean variable wasClicked....if but1 was clicked make variable true otherwise false...override this method....as user comes back it will check first than enable or disable as 1st but was clicked or not.
#Override
protected void onResume() {
// TODO Auto-generated method stub
if(!wasClicked){
but2.setClickable(false);
but3.setClickable(false);
but4.setClickable(false);
}else{
but2.setClickable(true);
but3.setClickable(true);
but4.setClickable(true);
}
}
Related
I tried to make a simple app which can keep scores on a game with four players. When I press the plus or minus button it opens a new activity (dialog theme) where the user should enter their score. That score should be stored into a text view.
Some part of code looks like this:
Here I opened new activities
buttonPlus1.setOnClickListener(this);
buttonPlus2.setOnClickListener(this);
buttonPlus3.setOnClickListener(this);
buttonPlus4.setOnClickListener(this);
buttonMinus1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttPlusPlayer1:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
break;
case R.id.buttPlusPlayer2:
Intent i2 = new Intent(this, ThirdPlusActivity.class);
startActivityForResult(i2, 2);
break;
case R.id.buttPlusPlayer3:
Intent i3 = new Intent(this, FourthPlusActivity.class);
startActivityForResult(i3, 3);
break;
case R.id.buttPlusPlayer4:
Intent i4 = new Intent(this, FifthPlusActivity.class);
startActivityForResult(i4, 4);
break;
case R.id.buttMinusPlayer1:
Intent i5 = new Intent(this, FirstMinusActivity.class);
startActivityForResult(i5, 1);
}
}
That's an example how activities send data back to main activity
public class SecondActivity extends AppCompatActivity {
private EditText mEditText;
private ImageButton mBackSpace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("ADD POINTS");
mEditText = findViewById(R.id.editTxtActivity);
//implementing backspace button
mBackSpace = findViewById(R.id.backSpaceButton);
mBackSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = mEditText.getText().toString();
if (str.length() > 0) {
str = str.substring(0, str.length() - 1);
mEditText.setText(str);
}
}
});
}
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message", mEditText.getText().toString());
setResult(RESULT_OK, i);
Toast.makeText(this, "You added " + mEditText.getText().toString() + " points", Toast.LENGTH_LONG).show();
finish();
}
}
and "now" I receive the results from activities
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String a = data.getStringExtra("message");
int i =Integer.parseInt(a);
String b = data.getStringExtra ("message5");
int z = Integer.parseInt(b);
Integer fResult = i - z;
String firstResult = fResult.toString();
mFirstScore.setText(firstResult);
}
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
if (requestCode == 3 && resultCode == RESULT_OK) {
mThirdScore.setText(data.getStringExtra("message3"));
}
if (requestCode == 4 && resultCode == RESULT_OK) {
mFourthscore.setText(data.getStringExtra("message4"));
}
the problem is on the last part of the code because I don't know how to receive data from activities and to calculate the result. I want when the user hit the plus button to add numbers and when minusButton is clicked to substract and the result to be stored on a TextView which, for player1, in my case is mFirstScore.
Is this possible or I did everything wrong and it is not possible to manipulate data received from another activity how I image
PS: on case buttMinusPlayer1 and on first if with requestCode 1 I've tried something ;
On SecondActivity put your key message2
#Override
public void onBackPressed() {
Intent i = new Intent();
i.putExtra("message2", mEditText.getText().toString());
setResult(RESULT_OK, i);
finish();
}
and get the data from SecondActivity by the same key message2
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK) {
mSecondscore.setText(data.getStringExtra("message2"));
}
}
Hope this helps.
I am trying to pass Bundle from second activity to the first(launch) activity. In order not to get NPE on launch, I am checking if bundle != null, however, it looks, like even after returning from second activity with Bundle, it still doesn't run the "if" body.
Here is my part of code of first activity
Bundle bundle = getIntent().getExtras();
if (bundle!=null) {
Player player = new Player();
player.setStatus(bundle.getInt("Status"));
player.setName(bundle.getString("Name"));
addPlayerToList(player);
Log.e("Player with a name " + player.getName(), "Has been created");
}
And code of second activity
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = nameEditText.getText().toString();
if (defaultRadioButton.isChecked()) {
status=0;
} else if (driverRadioButton.isChecked()) {
status=1;
} else {
Toast.makeText(getApplicationContext(), "Suka viberi galochku", Toast.LENGTH_SHORT).show();
}
Intent i = new Intent(getApplicationContext(),StartActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("Status",status);
bundle.putString("Name", name);
Log.d("Object " + name, "Status: " + status);
startActivity(i);
}
});
Thanks for any help/advice
Use startActivityForResult() for this situation.
1) You open second activity from the first using this method, not startActivity()
2) Do whatever you want in the second activity
3) Set result bundle
4) Finish activity
5) Open bundle in the first activity
In your case it will look like:
1) Call second activity like this:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, REQUEST_SECOND_ACTIVITY); // request code const
2-4)
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = nameEditText.getText().toString();
if (defaultRadioButton.isChecked()) {
status=0;
} else if (driverRadioButton.isChecked()) {
status=1;
} else {
Toast.makeText(getApplicationContext(), "Suka viberi galochku", Toast.LENGTH_SHORT).show();
}
final Intent returnIntent = new Intent();
returnIntent.putExtra("Status", status); // set values
returnIntent.putExtra("Name", name);
setResult(Activity.RESULT_OK, returnIntent); // set result code
finish(); // finish this activity and go back to the previous one
}
});
5) Override this method in the first activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case REQUEST_SECOND_ACTIVITY: // same request code const
if(resultCode == Activity.RESULT_OK){
Player player = new Player();
player.setStatus(data.getIntExtra("Status"));
player.setName(data.getStringExtra("Name"));
addPlayerToList(player);
}
break;
}
}
try Intent.putExtra() instead of putting data into a bundle, and use Intent.getStringExtra() to get a String data;
In your code, there is no code to put your bundle into intent. actually you never pass the bundle to first activity. you can use this answer to solve your problem.
good luck!
This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
I got this problem where I have three if statements and every one of them has IF String == null or String == "" and even the getText doesn't give any value, these doesn't activate.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_two:
final String hei = h.getText().toString();
final String wei = w.getText().toString();
String waist = wc.getText().toString();
if (hei == null || wei == null ){
final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
checker.setText("Please, put your information in these spots!");
checker.startAnimation(animationFadeIn);
}
else if (waist == null){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure?");
builder.setMessage("If you let Waist circumference empty, we will ask it from you later.")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getApplicationContext(), number3.class);
i.putExtra("height" , hei);
i.putExtra("weight" , wei);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
});
AlertDialog alert = builder.create();
alert.show();
}
else{
Log.d("BMI Height" , String.valueOf(wei));
Log.d("BMI Weight" , String.valueOf(hei));
Intent i = new Intent(getApplicationContext(), healthcalculator3.class);
i.putExtra("height" , hei);
i.putExtra("WC" , waist);
i.putExtra("weight" , wei);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
And even if all those (waist, wei, hei) are null or not value set, it only activates the last ELSE and the machine thinks that they have some data/value.
You can also try this like
if (hei == null || wei == null || hei.equals("") || wei.equals("")){
// your body here
}
Try this check
if(!TextUtils.isEmpty(string){
//body here
}
The best way to compare strings is use stringInstance.equals("what to compare"), so your code will be:
hei.equals("")
Also you can use length of string:
hei.length() > 0
I am trying to make registration activity without using SQLite in android.
In which user enters data at run time and if he do not fill all sections then user will not proceed to another activity. But the problem is if user is not entering anything then its also proceeding to another activity.
here is my code. Plz help me whats the error.
public void onClick(View v) {
EditText us = (EditText)findViewById(R.id.us);
EditText pas = (EditText)findViewById(R.id.pas);
EditText em = (EditText)findViewById(R.id.em);
EditText ph = (EditText)findViewById(R.id.ph);
String UserName= us.getText().toString();
String Email= em.getText().toString();
String Password= pas.getText().toString();
String PhoneNo= ph.getText().toString();
us.getEditableText().toString();
em.getEditableText().toString();
pas.getEditableText().toString();
ph.getEditableText().toString();
if ((us != null) && (em != null) && (pas != null) &&(ph != null)){
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SendOfferActivity.class);
startActivity(intent);
}
else{
AlertDialog.Builder builder1 = new AlertDialog.Builder(Registration.this);
builder1.setMessage("Enter Valid Information.");
builder1.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}});
}
But the problem is if user is not entering anything then its also
proceeding to another activity. here is my code
you are just checking if the object are not null, but what you want really check is if the EditTexts contain something. You use
if (!TextUtils.isEmpty(am.getText().toString()) /* do the same for the other */) {
}
to check the content of your EditTexts
You are just checking the EditText elements and you show test if the user typed something.
String usTxt = us.getText().toString();
String emTxt = em.getText().toString();
String pasTxt = pas.getText().toString();
String phTxt = ph.getText().toString();
if (!usTxt.isEmpty() && !emTxt.isEmpty() && !pasTxt.isEmpty() && !phTxt.isEmpty())
{
// your code
}
if ((!UserName.isEmpty()) && (!Email.isEmpty()) && (!Password.isEmpty() &&(!PhoneNo.isEmpty())){}
Picture 1. This is my create_layout.
Picture 2. This is my custom contact list when I clicked on add member button.
Picture 3. NOW HERE IS THE PROBLEM. When press on the select button. I wanted to list the chosen contact value back to my first image layout. But it is opening a duplicate of my first layout and appearing there.
Here is my code.
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent (getApplicationContext(), CreateTab.class);
i.putExtra("str",checkedcontacts.toString());
startActivity(i);
finish();
}
});
I know the problem is that I make an intent so that when user clicks on select button, it will point back to CreateTab class which will repeat the onCreate. But how can I prevent from the onCreate again?
you should you use onActivityResult() method to read the returned result.
Do this in your CreateTab.class
Intent i = new Intent(getApplicationContext(), AddMember.class);
startActivityForResult(i, 100); // 100 is some code to identify the returning result
Method to read the result from newly created activity
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
String str = data.getExtras().get("str");
}
}
Send result back to old activity when StartActivityForResult() is used
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
and close your AddMember activity
finish()
so your click event should look like this;
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
finish();
}
});
http://developer.android.com/training/basics/intents/result.html
Don't start previous activity again, just update selector Activity's result, and call finish().
Intent resultIntent = new Intent(this, PreviousActivity.class);
resultIntent.putExtra("selection",checkedcontacts.toString());
setResult(RESULT_OK,resultIntent);
finish();