Can someone tell me how to disable a button in Android unless all three editText fields are filled? I'm not sure where to include a conditional statement here and how to place it in the context of the button listener.
Here's my main activity code.
package com.amritayalur.mypowerschool;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;
String url = "";
String str;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
textViewTitle = (TextView) findViewById(R.id.textViewTitle);
textViewDesc = (TextView) findViewById(R.id.textViewDesc);
editTextURL = (EditText) findViewById(R.id.editTextURL);
editTextUser = (EditText) findViewById(R.id.editTextUser);
editTextPass = (EditText) findViewById(R.id.editTextPass);
//Start TextView
textViewTitle.setText("MyPowerSchool");
//button listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
};
});
}
}
You can use the TextChangeListener
EditText ed = //your EditText
ed.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Check if 's' is empty
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Its very Simple, Just update your onClick() method with following code,
#Override
public void onClick(View v)
{
if ( ( !editTextURL.getText().toString().equals("")) && ( !editTextUser.getText().toString().equals("")) && ( !editTextPass.getText().toString().equals("") ) )
{
// TODO Auto-generated method stub
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
}
};
The Above if condition will not disable your Button , but it will allow only if your All EditText are not empty.
How about that:
//button listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!"".equals(editTextURL.getText().toString())
&& !"".equals(editTextUser.getText().toString())
&& !"".equals(editTextPass.getText().toString())){
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
}
};
});
And if you want to disable button:
buttonSubmit.setEnabled(false); //disable button;
Related
I wish to have questions that change to the next question in the sequence onClick but I'm having trouble creating the array of questions and writing the onClick code for that portion.
*Too add more context to the app it will be a questionaire app with a question above an editText field and the users inputs will show up in its respective box at the top of the screen.
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class menu extends Activity {
Button mButton;
EditText mEdit;
int questionNumber = 0;
String [] questions;
int numberOfQuestions = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
questions=new String[numberOfQuestions];
questions[0]="This is first question?";
questions[1]="This is second question?";
final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
mEdit.setText(null);
questionNumber++;
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
i am building an app where if the user enters an "o" or "O" in the start of the edit text then the following should be executed:
replace the entered "o"or "O" with "P" at runtime in the EditText and set the TextView to "ORANGE".
Similarly, if the user enters "g" or "G" then the entered "g"or "G" should be replace with "P" at runtime in EditText and set the TextView to "Green".
and so on....
how can i accomplish this.I have use TextWatcher() but unable to achieve the goal.Help will be appreciated
package com.example.answer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class data extends Activity implements OnClickListener {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Button start, startFor;
EditText sendET;
TextView gotAnswer, result;
int flag = 1;
TextWatcher tt = null;
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
final EditText sendET = (EditText) findViewById(R.id.sendet);
final TextView result = (TextView) findViewById(R.id.tv);
tt = new TextWatcher() {
public void afterTextChanged(Editable s) {
sendET.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt);
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt);
}
};
sendET.addTextChangedListener(tt);
}
private void initialize() {
start = (Button) findViewById(R.id.button1);
sendET = (EditText) findViewById(R.id.sendet);
start.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String bread = sendET.getText().toString();
result = (TextView) findViewById(R.id.tv);
result.setText(bread);
/*
* Bundle basket = new Bundle(); basket.putString("key", bread);
* Intent a = new Intent(data.this, openedclass.class);
* a.putExtras(basket); startActivity(a); break;
*/
}
}
}
if (sendET.getText().toString() == "o")
This is wrong. It's not how you check string equality in Java. Correct usage:
if(sendET.getText().toString().equals("o"))
Also, you remove your listener and add it again. I don't know your app logic, but I guess you don't want addTextChangedListener:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (sendET.getText().toString() == "o") {
flag = 0;
}
sendET.removeTextChangedListener(tt); // REMOVING
if (flag == 0) {
sendET.setText(sendET.getText().toString()
.replaceAll("o", "P"));
result.setText("ORANGE");
}
sendET.addTextChangedListener(tt); // ADDING AGAIN
}
I have two EditText and one TextView in my activity,I want to multiple both edittext value thing is that when user entering values to any of edittext ,Accordingly the textView value should be change..My code is as below:
EDITTEXTS:qty,price
TextView:tv_subtotal
main.java
package com.epe.yehki.ui;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.epe.yehki.util.Const;
import com.epe.yehki.util.Pref;
import com.epe.yehki.util.Utils;
import com.example.yehki.R;
public class PlaceOrderActivity extends ListActivity {
Button confirm, shipping;
RelativeLayout rl_adress;
TextView product_name;
TextView tv_adress;
TextView tv_select_adres;
EditText qty;
EditText price;
TextView tv_subtotal;
TextView tv_total;
ArrayAdapter<String> adapter;
String method;
ImageView back;
Double total, subtotal, min_qty, min_agree_prc, max_agree_prc, min_agree_qty, Max_agree_qty, min_qty_unit_name, max_price, min_price;
Intent i = null;
#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_place_order);
final ListView lv = getListView();
System.out.println("::::::::::::my custome Id inside ::::::::::place order" + Pref.getValue(PlaceOrderActivity.this, Const.PREF_CUSTOMER_ID, ""));
// FINDINLG IDS...!!
qty = (EditText) findViewById(R.id.et_qty);
price = (EditText) findViewById(R.id.et_price);
rl_adress = (RelativeLayout) findViewById(R.id.btn_adress);
product_name = (TextView) findViewById(R.id.tv_product_name);
tv_adress = (TextView) findViewById(R.id.tv_adress);
tv_select_adres = (TextView) findViewById(R.id.tv_select_adress);
tv_total = (TextView) findViewById(R.id.tv_total);
tv_subtotal = (TextView) findViewById(R.id.tv_subtotal);
back = (ImageView) findViewById(R.id.iv_back);
qty.setCursorVisible(false);
price.setCursorVisible(false);
lv.setVisibility(View.GONE);
String[] shipMethods = new String[] { "Sea Freight", "Air Cargo", "Express" };
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shipMethods);
shipping = (Button) findViewById(R.id.btn_shiping_method);
shipping.setText("Select shipping Method");
if (Pref.getValue(PlaceOrderActivity.this, Const.FLAG_ADDRESS, "").equals("")) {
max_agree_prc = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MAX_PRICE));
min_agree_prc = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MIN_PRICE));
Max_agree_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MAX_QTY));
min_agree_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_AGREE_MIN_QTY));
min_qty = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_ORDER_QTY));
min_qty_unit_name = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_ORDER_QTY_UNIT_NAME));
max_price = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MAX_PRICE));
min_price = Double.parseDouble(i.getStringExtra(Const.TAG_PRODUCT_MIN_PRICE));
product_name.setText(getIntent().getStringExtra(Const.TAG_PRODUCT_NAME));
}
qty.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
qty.setCursorVisible(true);
}
});
price.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
price.setCursorVisible(true);
}
});
// SELECT ADDRESS CLICK EVENT..!
rl_adress.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
i = new Intent(PlaceOrderActivity.this, AddressListActivity.class);
startActivity(i);
}
});
shipping.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setListAdapter(adapter);
lv.setVisibility(View.VISIBLE);
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
method = (String) getListAdapter().getItem(position);
shipping.setText(method);
i.putExtra("shipping", method);
lv.setVisibility(View.GONE);
}
});
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Checking for quantity and price...!!!
if (Integer.parseInt(qty.getText().toString()) >= min_qty) {
// check for quantity range
if (Integer.parseInt(price.getText().toString()) >= min_price && Integer.parseInt(price.getText().toString()) <= max_price) {
// check for price range
} else {
Utils.showCustomeAlertValidation(PlaceOrderActivity.this, "Enter Price in Range", "ALERT", "OK");
}
} else {
Utils.showCustomeAlertValidation(PlaceOrderActivity.this, "Enter Quantity in Range", "ALERT", "OK");
}
}
});
}
private void calculate() {
// TODO Auto-generated method stub
double QuantyInt = 1;
double PriceInt = 0;
if (qty != null)
QuantyInt = Double.parseDouble(qty.getText().toString());
if (price != null)
PriceInt = Double.parseDouble(price.getText().toString());
subtotal = (QuantyInt * PriceInt);
String textResult = "Your BMI is " + subtotal;
tv_subtotal.setText(textResult);
tv_total.setText(textResult);
}
}
I want the values of edit text restored when user comes back to my first activity?
Please help me out.
Thanks in advance
this is my first activity code for getting user values in edit text
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
this is my second activity code
public class Second_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
});
}
You don't need neither onSaveInstanceState nor onRestoreInstanceState.
Just call finish in the onClick listener for the button in the second Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
}
This is the second one:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
That way you are resuming the previous Activity instead of starting new one.
If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
private static final int GET_VALUES_REQUEST_ID = 1;
public static final String FIRST_VALUE_ID = "first_value_id";
public static final String SECOND_VALUE_ID = "second_value_id";
private static final float DEFAULT_VALUE = 0;
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivityForResult(intent, GET_VALUES_REQUEST_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GET_VALUES_REQUEST_ID: {
if (Activity.RESULT_OK == resultCode) {
ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
setValues();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void setValues() {
ed1.setText(Float.toString(ed1_val));
ed2.setText(Float.toString(ed2_val));
}
}
The second activity could be something like that:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
This is a very basic example so I just hardcoded some return values - please implement something more meaningful.
Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.
just finish the second activity don't start it via intent.
When you finish the second activity first activity will be automatically resumed,
you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)
Here is an example
Remember it will work when you will not finish your previous activity.Do this in your first activity.
You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.
Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.
I want to make a simple calculator My code is below:
package som.dev.android.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CalcApp extends Activity {
/** Called when the activity is first created. */
Button addValues, subValues, equalsValue;
EditText inputValue;
int inputNum = 0;
public int value1;
public int value2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// initializing Views
inputValue = (EditText) findViewById(R.id.editText1);
addValues = (Button) findViewById(R.id.addBtn);
subValues = (Button) findViewById(R.id.subBtn);
equalsValue = (Button) findViewById(R.id.equalsBtn);
// adding onClick Listeners
addValues.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
pressed = true;
value1 = Integer.parseInt(inputValue.getText().toString());
inputValue.setText("");
}
});
equalsValue.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
value2 = Integer.parseInt(inputValue.getText().toString());
int result = value1+value2;
inputValue.setText(result);// There an exception occurs....
}
});
}
}
But it does not sets result to Edittext in my app and an exception occurs and message comes Unfortunately app is colsed some thing like that so please any one tell me the solution of this problem
use this inputValue.setText(""+result);instead of inputValue.setText(result);// There an exception occurs....
just conversion integer into string
The reason you get an exception is that the EditText thinks you are setting a resource id. You need to convert the integer to a string yourself Integer.toString(result) before using it in the EditText Object. http://developer.android.com/reference/android/widget/TextView.html
change Following Line:-
inputValue.setText(result);
to
inputValue.setText(String.Valueof(result));
then your error is solved.