Note that there are some explanatory texts on larger screens.

plurals
  1. POI want to use a self made Class in android
    primarykey
    data
    text
    <p>I am currently new to Android Development and i am having a difficult time trying to use a self made class in my MainActivity Android class. Let me give you an example; I created a SquareArea class and want to use it in the MainActivity Class</p> <pre><code>public class SquareArea{ private double _length; private double _width; public SquareArea(double length , double width){ _length = length; _width = width; area(); } private double area(){ return _length } } </code></pre> <p>When i instantiate the SquareClass in MainActivity class i want to be able to use the area() method and return when values that are Extracted from(EditText)<br> I want to use the value in order to place it in a text view;however this does not seem to happen.</p> <p>I can do it with methods but i want to use my own classes instead.</p> <p>Please help, I am getting frustrated with this.</p> <pre><code>///Below is my MainActivity Class/// public class MainActivity extends Activity { EditText mEditText1; EditText mEditText2; EditText mEditText3; TextView mTextView; Button mButton; Double value1, value2; SquareArea sq1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText1 = (EditText) findViewById(R.id.editText1); mEditText2 = (EditText) findViewById(R.id.editText2); mEditText3 = (EditText) findViewById(R.id.editText3); mTextView = (TextView) findViewById(R.id.textView1); mEditText1.setBackgroundColor(Color.GREEN); mEditText2.setBackgroundColor(Color.GREEN); mEditText3.setBackgroundColor(Color.RED); mButton = (Button) findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //When the button is clicked, call the calucate method. // calculate(); try { value1 = Double.parseDouble(mEditText1.getText().toString()); value2 = Double.parseDouble(mEditText2.getText().toString()); sq1 = new SquareArea(value1, value2); mTextView.setText(sq1.toString()); } catch (NumberFormatException e) { mTextView.setText("Please use numbers"); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload