How do I get my “calculate button” in a Fragment of a Tabbed Activity working












0















I'm creating an application with three tabs and three fragments created for the three tabs which represent a bmi calculator a length converter and weight converter. non of the calculate buttons work in any of the fragments



Ive tried setting onclick from the xml and also overriding the onclick method in fragment.java file



@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_fragment_1, container, false);

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

height = height.findViewById(R.id.height);
weight = weight.findViewById(R.id.weight);
result = result.findViewById(R.id.result);

calc = calc.findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();

@Override
public void onClick(View arg0) {

// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
float heightValue = Float.parseFloat(heightStr);
float weightValue = Float.parseFloat(weightStr);
float bmi = weightValue / (heightValue * heightValue);

displayBMI(bmi);
} else {
result.setText(getString(R.string.Enter_height));


}

}


private void displayBMI(float bmi) {


String bmiLabel;


if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.very_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal_weight);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.over_weight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.first_class_obese);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.second_class_obese);
} else {
bmiLabel = getString(R.string.third_class_obese);
}

bmiLabel = bmi + "n" + bmiLabel;
result.setText(bmiLabel);

}
})

;
}


I expect the bmi to be calculated when the button: "@id calc" is clicked from the values put in the editText field for height and weight










share|improve this question

























  • what is the problem you are facing?

    – Ajay Chauhan
    Dec 29 '18 at 10:52











  • the button does nothing when clicked

    – daniel_c
    Dec 29 '18 at 11:30











  • try adding some logs so you can know if the code inside onclick is getting executed or not.

    – Ajay Chauhan
    Dec 29 '18 at 11:32











  • thank you..please I'm not very familiar adding logs..it would be fine if you help me through

    – daniel_c
    Dec 29 '18 at 15:38
















0















I'm creating an application with three tabs and three fragments created for the three tabs which represent a bmi calculator a length converter and weight converter. non of the calculate buttons work in any of the fragments



Ive tried setting onclick from the xml and also overriding the onclick method in fragment.java file



@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_fragment_1, container, false);

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

height = height.findViewById(R.id.height);
weight = weight.findViewById(R.id.weight);
result = result.findViewById(R.id.result);

calc = calc.findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();

@Override
public void onClick(View arg0) {

// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
float heightValue = Float.parseFloat(heightStr);
float weightValue = Float.parseFloat(weightStr);
float bmi = weightValue / (heightValue * heightValue);

displayBMI(bmi);
} else {
result.setText(getString(R.string.Enter_height));


}

}


private void displayBMI(float bmi) {


String bmiLabel;


if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.very_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal_weight);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.over_weight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.first_class_obese);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.second_class_obese);
} else {
bmiLabel = getString(R.string.third_class_obese);
}

bmiLabel = bmi + "n" + bmiLabel;
result.setText(bmiLabel);

}
})

;
}


I expect the bmi to be calculated when the button: "@id calc" is clicked from the values put in the editText field for height and weight










share|improve this question

























  • what is the problem you are facing?

    – Ajay Chauhan
    Dec 29 '18 at 10:52











  • the button does nothing when clicked

    – daniel_c
    Dec 29 '18 at 11:30











  • try adding some logs so you can know if the code inside onclick is getting executed or not.

    – Ajay Chauhan
    Dec 29 '18 at 11:32











  • thank you..please I'm not very familiar adding logs..it would be fine if you help me through

    – daniel_c
    Dec 29 '18 at 15:38














0












0








0








I'm creating an application with three tabs and three fragments created for the three tabs which represent a bmi calculator a length converter and weight converter. non of the calculate buttons work in any of the fragments



Ive tried setting onclick from the xml and also overriding the onclick method in fragment.java file



@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_fragment_1, container, false);

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

height = height.findViewById(R.id.height);
weight = weight.findViewById(R.id.weight);
result = result.findViewById(R.id.result);

calc = calc.findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();

@Override
public void onClick(View arg0) {

// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
float heightValue = Float.parseFloat(heightStr);
float weightValue = Float.parseFloat(weightStr);
float bmi = weightValue / (heightValue * heightValue);

displayBMI(bmi);
} else {
result.setText(getString(R.string.Enter_height));


}

}


private void displayBMI(float bmi) {


String bmiLabel;


if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.very_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal_weight);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.over_weight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.first_class_obese);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.second_class_obese);
} else {
bmiLabel = getString(R.string.third_class_obese);
}

bmiLabel = bmi + "n" + bmiLabel;
result.setText(bmiLabel);

}
})

;
}


I expect the bmi to be calculated when the button: "@id calc" is clicked from the values put in the editText field for height and weight










share|improve this question
















I'm creating an application with three tabs and three fragments created for the three tabs which represent a bmi calculator a length converter and weight converter. non of the calculate buttons work in any of the fragments



Ive tried setting onclick from the xml and also overriding the onclick method in fragment.java file



@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_fragment_1, container, false);

}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

height = height.findViewById(R.id.height);
weight = weight.findViewById(R.id.weight);
result = result.findViewById(R.id.result);

calc = calc.findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
String heightStr = height.getText().toString();
String weightStr = weight.getText().toString();

@Override
public void onClick(View arg0) {

// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
float heightValue = Float.parseFloat(heightStr);
float weightValue = Float.parseFloat(weightStr);
float bmi = weightValue / (heightValue * heightValue);

displayBMI(bmi);
} else {
result.setText(getString(R.string.Enter_height));


}

}


private void displayBMI(float bmi) {


String bmiLabel;


if (Float.compare(bmi, 15f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.very_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal_weight);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.over_weight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.first_class_obese);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.second_class_obese);
} else {
bmiLabel = getString(R.string.third_class_obese);
}

bmiLabel = bmi + "n" + bmiLabel;
result.setText(bmiLabel);

}
})

;
}


I expect the bmi to be calculated when the button: "@id calc" is clicked from the values put in the editText field for height and weight







java android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 19:28









vizsatiz

516316




516316










asked Dec 29 '18 at 10:49









daniel_cdaniel_c

13




13













  • what is the problem you are facing?

    – Ajay Chauhan
    Dec 29 '18 at 10:52











  • the button does nothing when clicked

    – daniel_c
    Dec 29 '18 at 11:30











  • try adding some logs so you can know if the code inside onclick is getting executed or not.

    – Ajay Chauhan
    Dec 29 '18 at 11:32











  • thank you..please I'm not very familiar adding logs..it would be fine if you help me through

    – daniel_c
    Dec 29 '18 at 15:38



















  • what is the problem you are facing?

    – Ajay Chauhan
    Dec 29 '18 at 10:52











  • the button does nothing when clicked

    – daniel_c
    Dec 29 '18 at 11:30











  • try adding some logs so you can know if the code inside onclick is getting executed or not.

    – Ajay Chauhan
    Dec 29 '18 at 11:32











  • thank you..please I'm not very familiar adding logs..it would be fine if you help me through

    – daniel_c
    Dec 29 '18 at 15:38

















what is the problem you are facing?

– Ajay Chauhan
Dec 29 '18 at 10:52





what is the problem you are facing?

– Ajay Chauhan
Dec 29 '18 at 10:52













the button does nothing when clicked

– daniel_c
Dec 29 '18 at 11:30





the button does nothing when clicked

– daniel_c
Dec 29 '18 at 11:30













try adding some logs so you can know if the code inside onclick is getting executed or not.

– Ajay Chauhan
Dec 29 '18 at 11:32





try adding some logs so you can know if the code inside onclick is getting executed or not.

– Ajay Chauhan
Dec 29 '18 at 11:32













thank you..please I'm not very familiar adding logs..it would be fine if you help me through

– daniel_c
Dec 29 '18 at 15:38





thank you..please I'm not very familiar adding logs..it would be fine if you help me through

– daniel_c
Dec 29 '18 at 15:38












2 Answers
2






active

oldest

votes


















0














try adding onClick method in Xml on button click.



android:onClick="onButtonClick"


Inside your fragment create the same method as you mentioned in your XML.



public void onButtonClick(View view){
//add your code here.
}





share|improve this answer


























  • thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

    – daniel_c
    Dec 29 '18 at 17:07











  • no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

    – Ajay Chauhan
    Dec 29 '18 at 17:58













  • it shows error message variable "onButtonClick" never used

    – daniel_c
    Dec 29 '18 at 18:43











  • i just edited my code

    – Ajay Chauhan
    Dec 29 '18 at 19:46











  • onButtonClick should be a function,why its showing variable to you.

    – Ajay Chauhan
    Dec 29 '18 at 19:46



















0














Your code has some errors. I have fixed them while I did not change your code's logic.



Since details provided by you aren't enough, I will have to assume a few things here,




  • Variable result variable is of type TextView in your Fragment's XML file and has the id as result.

  • Variables height, weight is of type EditText in your Fragment's XML file (as you have mentioned) and have id as height and weight.

  • Variable calc is of type Button in your Fragment's XML file and has the id as calc.


  • What is Objects in your application? I cannot see its declaration or where is it initialized. Unless we know what it is, without initializing this code won't work



    String heightStr;
    String weightStr;

    EditText height;
    EditText weight;
    TextView result;

    Button calc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

    height = (EditText) view.findViewById(R.id.height);
    weight = (EditText) view.findViewById(R.id.weight);

    result = (TextView) view.findViewById(R.id.result);

    calc = (Button) view.findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // here you set what you want to do when user clicks your button,
    // e.g. launch a new activity

    heightStr = height.getText().toString();
    weightStr = weight.getText().toString();

    if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
    float heightValue = Float.parseFloat(heightStr);
    float weightValue = Float.parseFloat(weightStr);
    float bmi = weightValue / (heightValue * heightValue);
    displayBMI(bmi);
    } else {
    result.setText(getString(R.string.Enter_height));
    }
    }
    });
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }


    private void displayBMI(float bmi) {

    String bmiLabel;

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.very_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal_weight);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.over_weight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.first_class_obese);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.second_class_obese);
    } else {
    bmiLabel = getString(R.string.third_class_obese);
    }

    bmiLabel = bmi + "n" + bmiLabel;
    result.setText(bmiLabel);

    }







share|improve this answer


























  • thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

    – daniel_c
    Dec 29 '18 at 15:47













  • Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

    – Heeth21
    Dec 29 '18 at 15:59













  • thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

    – daniel_c
    Dec 29 '18 at 17:31











  • It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

    – Heeth21
    Dec 30 '18 at 2:57













  • I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

    – daniel_c
    Dec 31 '18 at 10:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968845%2fhow-do-i-get-my-calculate-button-in-a-fragment-of-a-tabbed-activity-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














try adding onClick method in Xml on button click.



android:onClick="onButtonClick"


Inside your fragment create the same method as you mentioned in your XML.



public void onButtonClick(View view){
//add your code here.
}





share|improve this answer


























  • thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

    – daniel_c
    Dec 29 '18 at 17:07











  • no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

    – Ajay Chauhan
    Dec 29 '18 at 17:58













  • it shows error message variable "onButtonClick" never used

    – daniel_c
    Dec 29 '18 at 18:43











  • i just edited my code

    – Ajay Chauhan
    Dec 29 '18 at 19:46











  • onButtonClick should be a function,why its showing variable to you.

    – Ajay Chauhan
    Dec 29 '18 at 19:46
















0














try adding onClick method in Xml on button click.



android:onClick="onButtonClick"


Inside your fragment create the same method as you mentioned in your XML.



public void onButtonClick(View view){
//add your code here.
}





share|improve this answer


























  • thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

    – daniel_c
    Dec 29 '18 at 17:07











  • no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

    – Ajay Chauhan
    Dec 29 '18 at 17:58













  • it shows error message variable "onButtonClick" never used

    – daniel_c
    Dec 29 '18 at 18:43











  • i just edited my code

    – Ajay Chauhan
    Dec 29 '18 at 19:46











  • onButtonClick should be a function,why its showing variable to you.

    – Ajay Chauhan
    Dec 29 '18 at 19:46














0












0








0







try adding onClick method in Xml on button click.



android:onClick="onButtonClick"


Inside your fragment create the same method as you mentioned in your XML.



public void onButtonClick(View view){
//add your code here.
}





share|improve this answer















try adding onClick method in Xml on button click.



android:onClick="onButtonClick"


Inside your fragment create the same method as you mentioned in your XML.



public void onButtonClick(View view){
//add your code here.
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 29 '18 at 19:45

























answered Dec 29 '18 at 16:07









Ajay ChauhanAjay Chauhan

217215




217215













  • thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

    – daniel_c
    Dec 29 '18 at 17:07











  • no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

    – Ajay Chauhan
    Dec 29 '18 at 17:58













  • it shows error message variable "onButtonClick" never used

    – daniel_c
    Dec 29 '18 at 18:43











  • i just edited my code

    – Ajay Chauhan
    Dec 29 '18 at 19:46











  • onButtonClick should be a function,why its showing variable to you.

    – Ajay Chauhan
    Dec 29 '18 at 19:46



















  • thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

    – daniel_c
    Dec 29 '18 at 17:07











  • no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

    – Ajay Chauhan
    Dec 29 '18 at 17:58













  • it shows error message variable "onButtonClick" never used

    – daniel_c
    Dec 29 '18 at 18:43











  • i just edited my code

    – Ajay Chauhan
    Dec 29 '18 at 19:46











  • onButtonClick should be a function,why its showing variable to you.

    – Ajay Chauhan
    Dec 29 '18 at 19:46

















thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

– daniel_c
Dec 29 '18 at 17:07





thank you ..would I need to add onclicklistener and override onClick method inside this method public void onButtonClick(View){ //add your code here. }

– daniel_c
Dec 29 '18 at 17:07













no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

– Ajay Chauhan
Dec 29 '18 at 17:58







no if you are doing it in the xml you dont need to add click listener.please accept the answer if it solves your problem.

– Ajay Chauhan
Dec 29 '18 at 17:58















it shows error message variable "onButtonClick" never used

– daniel_c
Dec 29 '18 at 18:43





it shows error message variable "onButtonClick" never used

– daniel_c
Dec 29 '18 at 18:43













i just edited my code

– Ajay Chauhan
Dec 29 '18 at 19:46





i just edited my code

– Ajay Chauhan
Dec 29 '18 at 19:46













onButtonClick should be a function,why its showing variable to you.

– Ajay Chauhan
Dec 29 '18 at 19:46





onButtonClick should be a function,why its showing variable to you.

– Ajay Chauhan
Dec 29 '18 at 19:46













0














Your code has some errors. I have fixed them while I did not change your code's logic.



Since details provided by you aren't enough, I will have to assume a few things here,




  • Variable result variable is of type TextView in your Fragment's XML file and has the id as result.

  • Variables height, weight is of type EditText in your Fragment's XML file (as you have mentioned) and have id as height and weight.

  • Variable calc is of type Button in your Fragment's XML file and has the id as calc.


  • What is Objects in your application? I cannot see its declaration or where is it initialized. Unless we know what it is, without initializing this code won't work



    String heightStr;
    String weightStr;

    EditText height;
    EditText weight;
    TextView result;

    Button calc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

    height = (EditText) view.findViewById(R.id.height);
    weight = (EditText) view.findViewById(R.id.weight);

    result = (TextView) view.findViewById(R.id.result);

    calc = (Button) view.findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // here you set what you want to do when user clicks your button,
    // e.g. launch a new activity

    heightStr = height.getText().toString();
    weightStr = weight.getText().toString();

    if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
    float heightValue = Float.parseFloat(heightStr);
    float weightValue = Float.parseFloat(weightStr);
    float bmi = weightValue / (heightValue * heightValue);
    displayBMI(bmi);
    } else {
    result.setText(getString(R.string.Enter_height));
    }
    }
    });
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }


    private void displayBMI(float bmi) {

    String bmiLabel;

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.very_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal_weight);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.over_weight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.first_class_obese);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.second_class_obese);
    } else {
    bmiLabel = getString(R.string.third_class_obese);
    }

    bmiLabel = bmi + "n" + bmiLabel;
    result.setText(bmiLabel);

    }







share|improve this answer


























  • thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

    – daniel_c
    Dec 29 '18 at 15:47













  • Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

    – Heeth21
    Dec 29 '18 at 15:59













  • thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

    – daniel_c
    Dec 29 '18 at 17:31











  • It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

    – Heeth21
    Dec 30 '18 at 2:57













  • I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

    – daniel_c
    Dec 31 '18 at 10:52
















0














Your code has some errors. I have fixed them while I did not change your code's logic.



Since details provided by you aren't enough, I will have to assume a few things here,




  • Variable result variable is of type TextView in your Fragment's XML file and has the id as result.

  • Variables height, weight is of type EditText in your Fragment's XML file (as you have mentioned) and have id as height and weight.

  • Variable calc is of type Button in your Fragment's XML file and has the id as calc.


  • What is Objects in your application? I cannot see its declaration or where is it initialized. Unless we know what it is, without initializing this code won't work



    String heightStr;
    String weightStr;

    EditText height;
    EditText weight;
    TextView result;

    Button calc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

    height = (EditText) view.findViewById(R.id.height);
    weight = (EditText) view.findViewById(R.id.weight);

    result = (TextView) view.findViewById(R.id.result);

    calc = (Button) view.findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // here you set what you want to do when user clicks your button,
    // e.g. launch a new activity

    heightStr = height.getText().toString();
    weightStr = weight.getText().toString();

    if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
    float heightValue = Float.parseFloat(heightStr);
    float weightValue = Float.parseFloat(weightStr);
    float bmi = weightValue / (heightValue * heightValue);
    displayBMI(bmi);
    } else {
    result.setText(getString(R.string.Enter_height));
    }
    }
    });
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }


    private void displayBMI(float bmi) {

    String bmiLabel;

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.very_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal_weight);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.over_weight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.first_class_obese);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.second_class_obese);
    } else {
    bmiLabel = getString(R.string.third_class_obese);
    }

    bmiLabel = bmi + "n" + bmiLabel;
    result.setText(bmiLabel);

    }







share|improve this answer


























  • thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

    – daniel_c
    Dec 29 '18 at 15:47













  • Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

    – Heeth21
    Dec 29 '18 at 15:59













  • thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

    – daniel_c
    Dec 29 '18 at 17:31











  • It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

    – Heeth21
    Dec 30 '18 at 2:57













  • I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

    – daniel_c
    Dec 31 '18 at 10:52














0












0








0







Your code has some errors. I have fixed them while I did not change your code's logic.



Since details provided by you aren't enough, I will have to assume a few things here,




  • Variable result variable is of type TextView in your Fragment's XML file and has the id as result.

  • Variables height, weight is of type EditText in your Fragment's XML file (as you have mentioned) and have id as height and weight.

  • Variable calc is of type Button in your Fragment's XML file and has the id as calc.


  • What is Objects in your application? I cannot see its declaration or where is it initialized. Unless we know what it is, without initializing this code won't work



    String heightStr;
    String weightStr;

    EditText height;
    EditText weight;
    TextView result;

    Button calc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

    height = (EditText) view.findViewById(R.id.height);
    weight = (EditText) view.findViewById(R.id.weight);

    result = (TextView) view.findViewById(R.id.result);

    calc = (Button) view.findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // here you set what you want to do when user clicks your button,
    // e.g. launch a new activity

    heightStr = height.getText().toString();
    weightStr = weight.getText().toString();

    if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
    float heightValue = Float.parseFloat(heightStr);
    float weightValue = Float.parseFloat(weightStr);
    float bmi = weightValue / (heightValue * heightValue);
    displayBMI(bmi);
    } else {
    result.setText(getString(R.string.Enter_height));
    }
    }
    });
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }


    private void displayBMI(float bmi) {

    String bmiLabel;

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.very_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal_weight);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.over_weight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.first_class_obese);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.second_class_obese);
    } else {
    bmiLabel = getString(R.string.third_class_obese);
    }

    bmiLabel = bmi + "n" + bmiLabel;
    result.setText(bmiLabel);

    }







share|improve this answer















Your code has some errors. I have fixed them while I did not change your code's logic.



Since details provided by you aren't enough, I will have to assume a few things here,




  • Variable result variable is of type TextView in your Fragment's XML file and has the id as result.

  • Variables height, weight is of type EditText in your Fragment's XML file (as you have mentioned) and have id as height and weight.

  • Variable calc is of type Button in your Fragment's XML file and has the id as calc.


  • What is Objects in your application? I cannot see its declaration or where is it initialized. Unless we know what it is, without initializing this code won't work



    String heightStr;
    String weightStr;

    EditText height;
    EditText weight;
    TextView result;

    Button calc;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_fragment_1, container, false);

    height = (EditText) view.findViewById(R.id.height);
    weight = (EditText) view.findViewById(R.id.weight);

    result = (TextView) view.findViewById(R.id.result);

    calc = (Button) view.findViewById(R.id.calc);

    calc.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // here you set what you want to do when user clicks your button,
    // e.g. launch a new activity

    heightStr = height.getText().toString();
    weightStr = weight.getText().toString();

    if (Objects.equals(heightStr, "") || !Objects.equals(weightStr, "")) {
    float heightValue = Float.parseFloat(heightStr);
    float weightValue = Float.parseFloat(weightStr);
    float bmi = weightValue / (heightValue * heightValue);
    displayBMI(bmi);
    } else {
    result.setText(getString(R.string.Enter_height));
    }
    }
    });
    return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }


    private void displayBMI(float bmi) {

    String bmiLabel;

    if (Float.compare(bmi, 15f) <= 0) {
    bmiLabel = getString(R.string.severely_underweight);
    } else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
    bmiLabel = getString(R.string.very_underweight);
    } else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
    bmiLabel = getString(R.string.underweight);
    } else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
    bmiLabel = getString(R.string.normal_weight);
    } else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
    bmiLabel = getString(R.string.over_weight);
    } else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
    bmiLabel = getString(R.string.first_class_obese);
    } else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
    bmiLabel = getString(R.string.second_class_obese);
    } else {
    bmiLabel = getString(R.string.third_class_obese);
    }

    bmiLabel = bmi + "n" + bmiLabel;
    result.setText(bmiLabel);

    }








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 9:54

























answered Dec 29 '18 at 11:51









Heeth21Heeth21

388




388













  • thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

    – daniel_c
    Dec 29 '18 at 15:47













  • Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

    – Heeth21
    Dec 29 '18 at 15:59













  • thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

    – daniel_c
    Dec 29 '18 at 17:31











  • It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

    – Heeth21
    Dec 30 '18 at 2:57













  • I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

    – daniel_c
    Dec 31 '18 at 10:52



















  • thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

    – daniel_c
    Dec 29 '18 at 15:47













  • Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

    – Heeth21
    Dec 29 '18 at 15:59













  • thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

    – daniel_c
    Dec 29 '18 at 17:31











  • It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

    – Heeth21
    Dec 30 '18 at 2:57













  • I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

    – daniel_c
    Dec 31 '18 at 10:52

















thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

– daniel_c
Dec 29 '18 at 15:47







thank you I added this line: height = (EditText) findViewById(R.id.height); then it gives error:cannot resolve method findViewById(int)..I had to add method qualifier which is the id before findViewById(R.id.height); I had to remove EditText from here because it shows cast to be redundant. height = (EditText) findViewById(R.id.height); weight = (EditText) findViewById(R.id.weight); this block of code heightStr = height.getText(); weightStr = weight.getText(); shows incompatible types. Required: java.lang.String Found: android.text.Editable. please more help needed

– daniel_c
Dec 29 '18 at 15:47















Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

– Heeth21
Dec 29 '18 at 15:59







Sorry, I have edited it. use heightStr = height.getText().toString(); and weightStr = weight.getText().toString(); cast is redundant simply means it isn't required. Also I don't know if method qualifier was necesarry, been a long time since I used EditText and as far as I remember I never needed it, will have to check for it again.

– Heeth21
Dec 29 '18 at 15:59















thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

– daniel_c
Dec 29 '18 at 17:31





thank you ..I have used your correction but the button still dosen't work. please more solutions if you have

– daniel_c
Dec 29 '18 at 17:31













It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

– Heeth21
Dec 30 '18 at 2:57







It wouldn't work. What is Objects in your code??? It does not seem to be initialized nor can I see the line where some value is assigned to it. How can you compare to a null value?? Also what is result?? Is it a TextView or something else?

– Heeth21
Dec 30 '18 at 2:57















I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

– daniel_c
Dec 31 '18 at 10:52





I have edited the code and removed that line from the code and it still does not work. result is a TextView for output

– daniel_c
Dec 31 '18 at 10:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968845%2fhow-do-i-get-my-calculate-button-in-a-fragment-of-a-tabbed-activity-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas