0

Apologize for this question. it might be bogus. but I need help as I m searching for the answer from back two days. I cant find any solution. How to connect any individual class to MainActivity.java Here. For instance. I have custom java class (strnum1.java) containing onclick to return number to textview. I want to refer this to MainActivity.java so as to run the program.

Strnum1.java (Custom class)

package com.marsh.calculator.calculator;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.view.View.OnClickListener;
 import android.widget.TextView;

 public class Strnum1 extends Activity implements View.OnClickListener {

   View rootview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.btnOne);

        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btnsin:
                ((TextView)findViewById(R.id.txtScreen)).setText("1");
                break;
        }
    }
 }

Any help on how could I solve this part of code.

Bala Raja
  • 627
  • 6
  • 20
Marsh
  • 35
  • 6
  • 2
    what do you mean by "connect"? Why do you need an Activity just to handle a button press? – Carlo Moretti May 05 '16 at 07:52
  • connect in the sense to refer the "custom class" to Main Activity. so that when the program runs it should retrieve the individual class activities to main activity. correct me if I m wrong. hope you get it. – Marsh May 05 '16 at 08:00
  • do I need to write anything inside Main Activity in context to individual class?. – Marsh May 05 '16 at 08:01
  • Sorry I just don't get it. You have your MainActivity with its activity_main layout. In that layout you have a button and you want to do something when it's pressed. Then just add the listener inside your MainActivity, why do you need a "custom class"? If you want to handle some extra logic in a separate class, that's fine, but that class doesn't need to be an activity. – Carlo Moretti May 05 '16 at 08:23
  • okay. I made some changes. will be glad if you could justify if it is right. – Marsh May 05 '16 at 08:31
  • In the code you posted, change the class name `Strnum1` to `MainActivity` and you're done! – Carlo Moretti May 05 '16 at 08:33

1 Answers1

0
package com.marsh.calculator.calculator;

 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bzero = (Button) findViewById(R.id.btnOne);
        Button bone = (Button) findViewById(R.id.btnZero);
        Button btwo = (Button) findViewById(R.id.btnTwo);
       Button bthree = (Button) findViewById(R.id.btnThree);
        Button bfour = (Button) findViewById(R.id.btnFour);
        Button bfive = (Button) findViewById(R.id.btnFive);
        Button bsix = (Button) findViewById(R.id.btnSix);
        Button bseven = (Button) findViewById(R.id.btnSeven);
        Button beight = (Button) findViewById(R.id.btnEight);
        Button bnine = (Button) findViewById(R.id.btnNine);
        bzero.setOnClickListener(this);
        bone.setOnClickListener(this);
        btwo.setOnClickListener(this);
        bthree.setOnClickListener(this);
        bfour.setOnClickListener(this);
        bfive.setOnClickListener(this);
        bsix.setOnClickListener(this);
        bseven.setOnClickListener(this);
        beight.setOnClickListener(this);
        bnine.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnZero:
                ((TextView) findViewById(R.id.txtScreen)).setText("0");
                break;
            case R.id.btnOne:
                ((TextView) findViewById(R.id.txtScreen)).setText("1");
                break;
            case R.id.btnTwo:
                ((TextView) findViewById(R.id.txtScreen)).setText("2");
                break;
            case R.id.btnThree:
                ((TextView) findViewById(R.id.txtScreen)).setText("3");
                break;
            case R.id.btnFour:
                ((TextView) findViewById(R.id.txtScreen)).setText("4");
                break;
            case R.id.btnFive:
                ((TextView) findViewById(R.id.txtScreen)).setText("5");
                break;
            case R.id.btnSix:
                ((TextView) findViewById(R.id.txtScreen)).setText("6");
                break;
            case R.id.btnSeven:
                ((TextView) findViewById(R.id.txtScreen)).setText("7");
                break;
            case R.id.btnEight:
                ((TextView) findViewById(R.id.txtScreen)).setText("8");
                break;
            case R.id.btnNine:
                ((TextView) findViewById(R.id.txtScreen)).setText("9");
                break;
        }
    }
  }
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
Marsh
  • 35
  • 6
  • I made changes to actual Main Activity. – Marsh May 05 '16 at 08:40
  • one more query. could I do the same for operators such as "sin", "cos", "rad", "hyp", etc. etc. or should I do it some other way. maybe other class something. please suggest me. – Marsh May 05 '16 at 08:43