-2

I have an Activity which has a method, I would like to call the entire method including buttons and other findViewById objects such as layouts, I have tried the following but the app crashes with runtime/check_jni.. Anyway below is the code:

ButtonClass:

public class BottomButtons extends MainActivity {

ImageButton iconHideView;
boolean fl;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.class_bottom_buttons);

}


public void bottomButtons() {


    final LinearLayout ll = (LinearLayout)findViewById(R.id.bottomButtons);
    final ScrollView sv = (ScrollView)findViewById(R.id.L_scrollView);

    iconHideView = (ImageButton) findViewById(R.id.icnHideView);
    iconHideView.setImageResource(R.drawable.arrow_down);
    iconHideView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

             if (!fl) {
                    ll.animate().translationY(ll.getHeight()).alpha(0.0f).setDuration(1200).setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    ....

MainActivity Class (And All Other Activities):

public class MainActivity extends Activity {

BottomButtons classBB = new BottomButtons();

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

    ....

    classBB.bottomButtons();

Basically this is a LinearLayout View with buttons which I animate, I have no problem using this method directly inside an activity however I would like to call it (bottomButtons) from all my other activities as well instead of having the same block of code in all my activities.

vk239
  • 1,014
  • 1
  • 12
  • 30
user3560827
  • 632
  • 1
  • 5
  • 22
  • 3
    Each activity has its own layout. You can not interact with the layout of an activity which is not open. You can only interact with the currently visible activity. – Jyotman Singh Feb 15 '16 at 18:01
  • Check this http://stackoverflow.com/questions/19026515/how-to-use-interface-to-communicate-between-two-activities you could use a broadcast receiver or an interface. – AndroidRuntimeException Feb 15 '16 at 18:04
  • 1
    See [Using the same button in other classes](http://stackoverflow.com/a/18964134/1380752) – codeMagic Feb 15 '16 at 18:05

1 Answers1

1

To all experts that thought this question was irrational, thank you very much.

As for my noobies, if you would like to use the same buttons on all activities without copy and paste of same code, I found this solution to work:

public class MainActivity extends BottomButtons {
....
bottomButtons();

Your OTHER activity (and all other activities) should extend BottomButtons then you just call the bottomButtons method in your onCreate of those activities and then you will have access to the buttons including their onClick events.

user3560827
  • 632
  • 1
  • 5
  • 22