[Solved] Android – How to Use a certain class in Main class? [closed]


Lets assume that you have a String in Operation.class that has some value in it which you want to assign to the Text view in MainActivity. Make sure that variable is public.

public String text1 = "this is text for textview";

Now the string above is in Operations.class and you can access the value of variable text that is declared in Operations.class from MainActivity.java like this

Operations obj = new Operations();
            String text = obj.text;

Then assign it to the text view like this

TextView textView = (TextView) findViewById(R.id.id_of_your_text_view);
    textView.setText(text);

You can do same thing with functions and this is one of the fundamentals of programming so I suggest you look into it.

For the simple add function in your case

First of all declare your function with a return type so when you access it you get a value back. I’m going to redefine your function

public int add()
    {
        z = x + y;
        return z;
    }

now when you call the function from your mainactivity like this

Operations obj = new Operations();
            Int value = obj.add();

It will store the value of z inside the variable value. Now if you want to set the text for textView by combining both value and text1 from before you have to first convert the value toString and then you can simply concatenate them in a 3rd variable and use that variable in setText().

4

solved Android – How to Use a certain class in Main class? [closed]