I want to do the following: when a CheckBox is checked a TextView appears then the user can put whatever he needs, but the problem is I can't get the text written in the EditText to put it in a String variable, because it takes the initial value which is "nothing" when it enters the if statement. Any help?
i am new at this website , here is the part of the code i am talking about
SendSMS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
sendText.setVisibility(0);
smstext = sendText.getText().toString();
} else if (isChecked == false) {
sendText.setVisibility(8);
smstext = "";
}
}
});
here.. after checking the sendsms to true edittext (sendText) will appear
.
.
.
.
.
.
here when the user enter any text i need to put it into a string (sms) to be stored in the data base ,, sorry for the mistakes
break;
case R.id.SaveImage:
String x = EventName.getText().toString();
if (x.contentEquals("")) {
Toast.makeText(getApplicationContext(), "Enter a Title",
Toast.LENGTH_SHORT).show();
} else {
Intent i = new Intent(getApplicationContext(), Swipe.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "Event saved",
Toast.LENGTH_SHORT).show();
String typename = type;
String name = EventName.getText().toString();
String location = EventLocation.getText().toString();
String dateFrom = DateFrom.getText().toString();
String dateTo = EbDate.getText().toString();
String timeFrom = mPickTime.getText().toString();
String timeTo = EbTime.getText().toString();
String duration = durationresult.getText().toString();
String alarm = alarmresult.getText().toString();
String repeat = repeatresult.getText().toString();
String audios = Audios;
String sms = smstext;
String call = calltext;
mySQLiteAdapter.insert(name, location, dateFrom, dateTo,
timeFrom, timeTo, duration, alarm, repeat, typename,
audios, sms, call);
updateList();
// reset form
EventName.setText(null);
EventLocation.setText(null);
break;
}
}
}
It appears that the only time you are setting smstext is when the value of the checkbox changes. The user can only enter text into that textbox when it is visible, so when the value of the checkbox changes, logically the textbox is empty and smstext will always be null.
Have you tried something like this:
case R.id.SaveImage:
...
String sms = sendText.getText().toString();
...
Related
I made a code where user put value between some range and my code generate random number for them. Randomization working properly but when fields are blank my app is crash how should I fix it.
randNum.java
Button generateNum = findViewById(R.id.generate_number);
generateNum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = findViewById(R.id.fromNum);
String sTextFromET = et.getText().toString();
int fNum = Integer.valueOf(sTextFromET);
EditText et1 = findViewById(R.id.toNum);
String sTextFromET1 = et1.getText().toString();
int sNum = Integer.valueOf(sTextFromET1);
TextView ans = findViewById(R.id.ans);
// if(sNum == null || fNum == null){
//
// ans.setText(getString(R.string.enterNumError));
//
// }
// else
if(sNum < fNum){
ans.setText(getString(R.string.max_min_error));
}else {
final int random = new Random().nextInt((sNum - fNum) + 1) + fNum;
String ras = Integer.toString(random);
ans.setText(ras);
}
}
});
I try to use null but it is not working.
You need to put validation first on button click. (For checking if user has entered nothing or just spaces in any of edittexts).
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strNum1 = edtl.getText().toString().trim();
strNum2 = edt2.getText().toString().trim();
if (strNum1.length() == 0)
{
showAlert("Please enter Num 1");
}
else if (strNum2.length() == 0)
{
showAlert("Please enter Num 2");
}
else
{
int numvalue1 = Integer.parseInt(strNum1);
int numvalue2 = Integer.parseInt(strNum2);
generateNum (numvalue1, numvalue2); //Call your function for generation of random number here
//do your stuff here
}
}
});
Hope this helps you understand the validation of forms for empty input fields.
P.S: I would recommend you put inputType attribute for your EditTexts if you have not added it already in xml file like:
android:inputType="number"
So you can avoid exception at Integer.parseInt if user enters any alphabet or symbol.
You need to handle NumberFormatException thrown by Integer.valueOf() function
try {
EditText et = findViewById(R.id.fromNum);
String sTextFromET = et.getText().toString();
int fNum = Integer.valueOf(sTextFromET);
EditText et1 = findViewById(R.id.toNum);
String sTextFromET1 = et1.getText().toString();
int sNum = Integer.valueOf(sTextFromET1);
TextView ans = findViewById(R.id.ans);
if(sNum < fNum){
ans.setText(getString(R.string.max_min_error));
}else {
final int random = new Random().nextInt((sNum - fNum) + 1) + fNum;
String ras = Integer.toString(random);
ans.setText(ras);
}
}catch(NumberFormatException e){
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
I have created a small application to check whether the password entered by user is valid or not. it is being able to check , but its not displaying the toast and as soon as i click the button , it shows "Unfortunately , your app has stopped working". I am using my device for deployment. Please help me find out , why the toast is not working. I have used a command which sets the value of variable a,b,c in the edit text field , to check whether it is coming correct. And yes it was coming correct. So the problem lies in the toast as per i think.
public class second extends AppCompatActivity {
public EditText fname ;
public EditText lname ;
public EditText email ;
public EditText pass ;
public EditText blood;
public EditText cpass;
public EditText add ;
public EditText mob ;
public Toast t ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
fname = (EditText) findViewById(R.id.fname);
lname = (EditText) findViewById(R.id.lname);
email = (EditText) findViewById(R.id.email);
pass = (EditText) findViewById(R.id.pass);
add = (EditText) findViewById(R.id.add);
cpass = (EditText) findViewById(R.id.cpass);
mob = (EditText) findViewById(R.id.mob);
blood = (EditText) findViewById(R.id.blood);
Button sign = (Button) findViewById(R.id.sign);
sign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sfname = fname.getText().toString();
String spass = pass.getText().toString();
String scpass = cpass.getText().toString();
validate(spass, scpass);
}
});
}
public void validate(String spass ,String scpass){
int a =0;
int b =0;
int c =0;
t = new Toast(this);
int len = spass.length();
for(int i =0;i<len;i++){
char d = spass.charAt(i);
if(d>=48 && d<=57){
a++;
}
if(d>=65 && d<=90){
b++;
}
if(d>=33 && d<=47){
c++;
}
}
email.setText(a+" "+b+" "+c);
if(a==0 || b==0 || c==0){
t.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
t.show();
} else {
if(spass.equals(scpass)){
t.makeText(this,"login succesful",Toast.LENGTH_SHORT);
t.show();
} else {
t.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();
}
}
}
}
try this
Toast toast = Toast.makeText(context, text, duration);
toast.show()
or
Toast.makeText(context, text, duration).show();
From the Docs, it says
Toast (Context context) Construct an empty Toast object. You must call
setView(View) before you can call show().
So when you create Toast object from its constructor, it is considered that you are trying to create Custom Toast
If you are not creating any, then use like:
public Toast t; // Global variable
Now inside your validate method:
t = Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG);
t.show();
I have made some changes to you method , try using this one .
You can find more details on https://stackoverflow.com/a/21963343
public void validate(String spass ,String scpass){
int a =0;
int b =0;
int c =0;
// Toast t = new Toast(this);
int len = spass.length();
for(int i =0;i<len;i++){
char d = spass.charAt(i);
if(d>=48 && d<=57){
a++;
}
if(d>=65 && d<=90){
b++;
}
if(d>=33 && d<=47){
c++;
}
}
email.setText(a+" "+b+" "+c);
if(a==0 || b==0 || c==0){
/*
* updated
* */
Toast.makeText(this, "Password should contain atleast one special character , one capital letter and one number", Toast.LENGTH_LONG).show();
} else {
if(spass.equals(scpass)){
/*Updated*/
Toast.makeText(this,"login succesful",Toast.LENGTH_SHORT).show();
} else {
/*Updated*/
Toast.makeText(this,"passwords dont match",Toast.LENGTH_SHORT).show();
}
}
}
I implemented Sharing intent button in my application. I have strings in textviews, but it is not always filled with any text. So, I would like to check it is empty or not. If a string is empty, sharing intent should ignore/remove/get ride of this string and go to next string. I tried to do this programmatically, but without success. How can I do this? I need some helpo:
public void buttonClick2(View view) {
TextView textView1 = (TextView)findViewById(R.id.word);
TextView textView2 = (TextView)findViewById(R.id.definition);
TextView textView3 = (TextView)findViewById(R.id.definition2);
TextView textView4 = (TextView)findViewById(R.id.definition3);
boolean hasDefinition2 = false;
if (textView3 !=null){
String text3 = textView3.getText().toString();
if(text3 !=null && !text3.isEmpty()){
hasDefinition2 = true;
}
}
String def2 ="";
if(hasDefinition2){
def2 +="\n Definition 2: "+textView3.getText().toString();
}
boolean hasDefinition3 = false;
if (textView4 !=null){
String text4 = textView4.getText().toString();
if(text4 !=null && !text4.isEmpty()){
hasDefinition3 = true;
}
}
String def3 ="";
if(hasDefinition3){
def3 +="\n Definition 3: "+textView4.getText().toString();
}
Intent shareIntent1 = new Intent(android.content.Intent.ACTION_SEND);
shareIntent1.setAction(android.content.Intent.ACTION_SEND);
shareIntent1.setType("text/plain");
shareIntent1.putExtra(android.content.Intent.EXTRA_SUBJECT, "Word"); //<=adding blank quotes
shareIntent1.putExtra(Intent.EXTRA_TEXT, textView1.getText().toString()+
"\n"+Definition: +textView2.getText().toString()+
"\n"+def2+
"\n"+def3+
"\n"+"\n"+"#2016 Dictionary"+
"\n"+"Visit us:");
startActivity(Intent.createChooser(shareIntent1, "Share"));
}
you can try the following to check if textView3/4 is empty or not: -
String extra =textView1.getText().toString()+
"\n"+Definition: +textView2.getText().toString();
if (textView3 !=null){
String text = textView3.getText().toString();
if(text !=null && !text.isEmpty()){
extra +="\n Definition 2:"+ textView3.getText().toString();
}
}
if (textView4 !=null){
String text = textView4.getText().toString();
if(text !=null && !text.isEmpty()){
extra +="\n Definition 3:"+ textView4.getText().toString();
}
}
extra += "\n"+"\n"+"#2016 Dictionary"+
"\n"+"Visit us:"
shareIntent.putExtra(Intent.EXTRA_TEXT, extra);
startActivity( ... )
also do consider using StringBuilder instead of String for the extra.
I m using this framework, I already trying many times for making this problem, but I cant do it. I already asking on stackoverflow but no one cant help me. Actually I m tried.
I m using this framework : https://github.com/kikoso/Swipeable-Cards
And I m using SimpleCardStackAdapter like this :
for (int i = 0; i < user.length(); i++) {
final JSONObject c = user.getJSONObject(i);
// Storing JSON item in a Variable
String id = c.getString(user_id);
String name = c.getString(username);
final String email = c.getString(text);
String image1 = c.getString(imageUrl);
String range1 = c.getString(range);
String msgId = c.getString(postId);
// adapter.add(new CardModel(name, email, image1));
//Set JSON Data in TextView
Log.i("image1image1image1image1", image1);
// CardModel cardModel = new CardModel(" cardModel", " CardModel", r.getDrawable(R.drawable.picture1));
card = new CardModel(name, email, image1);
card.setOnClickListener(new CardModel.OnClickListener() {
#Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
// Intent no = new Intent(HomeListview.this, YayDetailActivity.class);
/// startActivity(no);
}
});
card.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
#Override
public void onLike(CardModel card) {
Log.i("Swipeable Cards", "I dislike the card");
}
#Override
public void onDislike(CardModel card) {
Log.i("Swipeable Cards", "I like the card");
// new sendNewYay().execute(sharedToken, card.getTitle());
Toast.makeText(getApplicationContext(), card.getDescription(), Toast.LENGTH_SHORT).show();
}
});
// I m added adapter
adapter.add(card);
mCardContainer.setAdapter(adapter);
}
At the onDislike method, I need to get item name.
in this line : new sendNewYay().execute(sharedToken, name);
I send the item name, But it dont work.
1.How can I get the item name, in this method?
2.I have two button, one of them for onLike method, another one for onDislike Method. Ho can I triggered this two method with my button?
Thank you.
Decleare two variable global as string
String itemname;
try {
JSONArray c = new JSONArray(user.toString());
for (int i = 0 ; i < c.length();i++) {
String id = c.getString(user_id);
String name = c.getString(username);
final String email = c.getString(text);
String image1 = c.getString(imageUrl);
String range1 = c.getString(range);
String msgId = c.getString(postId);
System.out.println("Position : " + "" + i + ""+ c.getString(i));
itemname = name.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Final itemname is " + itemname);
I'm developing an app for android, and currently the only thing it does is calculate grades. There's two input boxes, one that takes the current grade, and another that takes a possible exam grade, and then it tells you the final grade. However, when there is no value in at least one of those boxes, it crashes. I tried to make an if statement to detect if the final value was null, but that didn't work. Here's my code:
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText grade = (EditText) findViewById(R.id.grade1);
EditText exam = (EditText) findViewById(R.id.grade2);
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
double finalResult = (Double.parseDouble(stringGrade) * .8) + (Double.parseDouble(stringExam) * 0.2);
String finalResultString = String.valueOf(finalResult);
if (finalResultString == null){
finalResultString = "0";
} else {
intent.putExtra(EXTRA_MESSAGE, finalResultString);
startActivity(intent);
}}
and here's the code that renders it:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView1 = new TextView(this);
textView1.setTextSize(30);
textView1.setText("Your final would be " + message);
setContentView(textView1);
Just for reference, the .8 and .2 is because the current grade is weighted at 80%, and the exam is weighted at 20%. How can I make it so it won't crash when nothing is put into the boxes?
usernameEditText.getText().toString(); would not return null. It will return an empty string. What you can do before calculating finalResult is to check stringGrade and stingExam are non-empty and numerical values and if one is not then stop the operation. i.e.
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
if (stringGrade.isEmpty() || stringExam.isEmpty()) {
Toast.makeText(this, "Invalid values", Toast.LENGTH_SHORT);
return;
}
try this:
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText grade = (EditText) findViewById(R.id.grade1);
EditText exam = (EditText) findViewById(R.id.grade2);
String stringGrade = (grade.getText().toString());
String stringExam = (exam.getText().toString());
if(stringGrade.equals("") ){
stringGrade = "0";
}
if(stringExam.equals("") ){
stringExam= "0";
}
double finalResult = (Double.parseDouble(stringGrade) * .8) + (Double.parseDouble(stringExam) * 0.2);
String finalResultString = String.valueOf(finalResult);
if (finalResultString == null){
finalResultString = "0";
} else {
intent.putExtra(EXTRA_MESSAGE, finalResultString);
startActivity(intent);
}}