I have been looking everywhere but i can't find the answer, i'm just a beginner, thanks in advance for all answers.
I want to put two editText's in activity_main.xml and when the app is open on the phone the user can write some numbers in those two editTexts and then click on Button, and the sum of these two editTexts to output in textView.
try this on button submit.
`btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(validation()){
int result = Integer.parseInt(edObjOne.getText().toString().trim())+
Integer.parseInt(edObjTwo.getText().toString().trim());
textViewObj.setText(String.valueOf(result));
}
}
});`
private boolean validation() {
if (edObjOne.getText().toString().trim().equals("")) {
Toast.makeText(this, "Field one empty", Toast.LENGTH_SHORT).show();
return false;
}
if (edObjTwo.getText().toString().trim().equals("")) {
Toast.makeText(this, "Field two empty", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
set edittext property number & android:digits="1234567890" in xml layout.
.
Related
I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}
Iam new to programming... still a beginner in Android...
I have a Login Activity, when I clikc submit it goes to another Activity.
I have an EditBox which it enters.
I want to detect if the EditBox is empty
If it is empty I want to avoid clicking a button which I am clicking using the
etNumber.setOnFocusChangeListener(new View.OnFocusChangeListener()
This will save the data to a csv file...
The idea is that any empty editText will not be saved... and thus have empty lines in the csv...
There are many ways to do it. But for the above problem statement the below code should work.
<Your EditText>.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!editText.getText().toString().trim().isEmpty()){
//Your code
}
}
});
EditText editText = (EditText) findViewById(R.id.edittext);
if(TextUtils.isEmpty(editText.getText().toString())) {
Toast.makeText(MainActivity.this, "EditText is Empty", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "EditText is Not Empty", Toast.LENGTH_LONG).show();
}
If you are using Kotlin (which you should) then below 1 line statement uses the kotlin inline extension to check for the emptiness,
editText.text.isNotEmpty().apply {
//do something
}
here editText is id of your edit text , which you can get with below code,
val editText= findViewById(R.id.et_message) as EditText
I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});
First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}
I'm in a situation where I have four radiobutton, next button and one question. now my issue is that if the user clicks next button without selecting any option it should show a toast message and if user clicks any one of the radio button, it should be checked whether it is right or wrong. I have partially implemented but I cant get the exact solution. any one help me with this.
Thanks in advance
I have posted my code below
Next.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
count++;
if(viewFlipper.getDisplayedChild()==0)
{
id1 = rag1.getCheckedRadioButtonId();
rab1=(RadioButton)findViewById(id1);
//System.out.println("1nd question answer"+rab1.getText());
if (rag1.getCheckedRadioButtonId()!=R.id.btn1_1||rag1.getCheckedRadioButtonId()!=R.id.btn1_2||rag1.getCheckedRadioButtonId()!=R.id.btn1_3||rag1.getCheckedRadioButtonId()!=R.id.btn1_4||rag1.getCheckedRadioButtonId()!=R.id.btn1_5&&rag1.getCheckedRadioButtonId()==-1)
{
Toast.makeText(getApplicationContext(), "Please enter the answer", Toast.LENGTH_SHORT).show();
}
if((rag1.getCheckedRadioButtonId()!=-1)&&rab1.getText().equals(c_alcorrectanswer.get(0)))
{
System.out.println("1nd question answer"+rab1.getText());
}
else
{
Toast.makeText(getApplicationContext(), "Please enter the correct answer", Toast.LENGTH_SHORT).show();
viewFlipper.stopFlipping();
}
}
You should check within one condition only. Whether any of your RadioButton of your RadioGroup is selected or not.
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
if(radioButtonID > 0) //return -1 if none of radiobutton is selected from radiogroup
{
//move to next
}
else
{
// show toast. Please select any option.
}
As per your code snippet:
if(viewFlipper.getDisplayedChild()==0)
{
id1 = rag1.getCheckedRadioButtonId();
if(id1 < 0) //return -1 if none of radio button is selected
{
//Toast. Please select any answer
}
}
developer.android.com documentation reference
I have five Edit Text in my application . I also have two buttons called "Next" and "Previous". Now I want to select the next and previous edit text fields when i click the corresponding buttons form my view dynamically. Is there any way to do this.
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusDownId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Next");
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = getCurrentFocus().getNextFocusUpId();
if(id != View.NO_ID) {
findViewById(id).requestFocus();
System.out.println("Back");
}
}
});
This is the XML where you have to set the focus order
<EditText
android:id="#+id/et1"
android:nextFocusDown="#+id/et2"
android:nextFocusUp="#+id/et2"
....../>
<EditText
android:id="#+id/et2"
android:nextFocusDown="#+id/et1"
android:nextFocusUp="#+id/et1"
...../>
Edit
If you are creating view dynamic then you should use below methods to set the next focus
setNextFocusDownId(id)
setNextFocusUpId(id);
i think this may help you,
http://kahdev.wordpress.com/2008/06/29/changing-button-text-in-android/
Make use of
android:nextFocusLeft
android:nextFocusRight
android:nextFocusUp
android:nextFocusDown
in your editText's attributes in your layout.xml.
e.g. android:nextFocusDown="#id/myNextEditText"
For more details about how to use it please follow this link.
Try -
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next :
if(editText1.hasFocus()){
editText2.requestFocus();
}else if(editText2.hasFocus()){
editText3.requestFocus();
}
break;
case R.id.btn_previous :
if(editText2.hasFocus()){
editText1.requestFocus();
}else if(editText3.hasFocus()){
editText2.requestFocus();
}
break;
}
}