Mohamad Ziadeh
Mohamad Ziadeh

Reputation: 7

How to define a method called in MainActivity from another class?

I want to define a method that I have in MainActivity in another class that I created. I am having issues figuring this out. Is this even possible? I am not finding anything online about defining a method in another class.

I have included my code and the example of how I want to do it.

My MainActivity Code

package com.example.flashcards;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    DatabseHelper DB = new DatabseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        changeText();
        changeText2();
    }

    private void changeText() {
        final String[] revertText = {"H2O", "What elements does water consist of?"};

        final TextView textChange = (TextView) findViewById(R.id.flashcard1);
        Button change = (Button) findViewById(R.id.answer1);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }

My TextC code(other class)

package com.example.flashcards;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextC extends MainActivity {
    private void changeText2() {
        final String[] revertText = {"2,200° F", "How hot does lava get?"};

        final TextView textChange = (TextView) findViewById(R.id.flashcard2);
        Button change = (Button) findViewById(R.id.answer2);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }
}

My method defined in the MainActivity works, but I cannot seem to get it to work from my other class. Is what I am trying to do possible? I want changeText2() method defined in my TextC class, but declared in my MainActivity since the MainActivity has the onCreate() method.

Upvotes: 0

Views: 57

Answers (2)

www.hybriscx.com
www.hybriscx.com

Reputation: 1129

Try checking OOPs concept for inheritance and overriding. As MainAcitivity is a parent class and TextC child, you get access of parent functions in child and not other way around. what you are trying to do is impossible unless you figure out the exact structure you are looking for.

May be you are looking to declare changeText2() function as public or protected in MainActicity and then override its implementation in child class if needed at all.

Upvotes: 1

Harish Reddy
Harish Reddy

Reputation: 992

TextC class should be your parent class of the MainActivity like

public class MainActivity extends TextC 

meanwhile TextC should be extend to AppCompactActivity and it should be abstact like shown in below.

public abstract class TextC extends AppCompatActivity 

if you do like this then you can access to functions present in TextC and change the name of the TextC and make it as BaseClass or something to make sense

Upvotes: 0

Related Questions