It is continuity to the question, android:onClick = "functionName" did not recognize the function name in Fragment subclass. As I exceeded word limit on that, I am asking a new question. I changed my code suggested on forum by different people. Please see the code below
When I used setOnClickListener on button inside the OnCreateView, the app crashing immediately after opening. When I used android:onClick in xml file (Scenario before changing the code) this didn't recognize the method at all.
// my java code for fragment subclass
package com.example.siv.mahalaxmipetroleums;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DipToVol extends Fragment {
Button clearButton11,calcButton11;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
clearButton11 = (Button) getView().findViewById(R.id.clearBtn1);
calcButton11 = (Button) getView().findViewById(R.id.calcBtn1);
calcButton11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int dipHSD = 0, dipMS = 0;
final short radHSD = 1219;
final short lenHSD = 6810;
final short radMS = 999;
final short lenMS = 6804;
double volHSD, volMS,volHSD1, volMS1;
EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
EditText dipM = (EditText) view.findViewById(R.id.dipMS1);
TextView volH = (TextView) view.findViewById(R.id.volHSD1);
TextView volM = (TextView) view.findViewById(R.id.volMS1);
try {
dipHSD = Integer.parseInt(dipH.getText().toString());
volHSD = calVol(radHSD, lenHSD, dipHSD);
volHSD1 = Math.round(volHSD);
volH.setText(volHSD1 + "");
}
catch (NumberFormatException e) {
volH.setText("");
}
try{ dipMS = Integer.parseInt(dipM.getText().toString());
volMS = calVol(radMS, lenMS, dipMS);
volMS1 = Math.round(volMS);
volM.setText(volMS1 + "");
}catch(NumberFormatException e){volM.setText("");}
}
});
clearButton11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText dipH = (EditText) view.findViewById(R.id.dipHSD1);
EditText dipM = (EditText) view.findViewById(R.id.dipMS1);
TextView volH = (TextView) view.findViewById(R.id.volHSD1);
TextView volM = (TextView) view.findViewById(R.id.volMS1);
volH.setText("");
volM.setText("");
dipH.setText("");
dipM.setText("");
}
});
return view;
}
public double calVol(short rad, short len, int height) {
double area;
area = (Math.PI * Math.pow(rad, 2) / 2) - Math.pow(rad, 2) * Math.asin(1 - (height / (double)rad)) - (rad - height) * Math.sqrt(height * (2 * rad - height));
return area*len*0.000001;
}
}