Shahbaz Pothiawala
Shahbaz Pothiawala

Reputation: 1215

Inheritance in Android

I have two Activities in my app : Menu Activity and Main Activity.

Here is MenuActivity xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MenuActivity" >

    <LinearLayout 
       android:orientation="horizontal"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="10dp"
       android:gravity="center"
       android:background="#000">

        <TextView
            android:text="menu 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/menu1"
            android:textColor="#fff"
            android:padding="5dp"
         />

         <TextView
            android:text="menu 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/menu2"
            android:textColor="#fff"
            android:padding="5dp"
         />

    </LinearLayout>

</RelativeLayout>

This is MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity" >

        <include 
            layout="@layout/activity_menu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />

</RelativeLayout>

Now the OnClick Listener of menu1 and menu2 is defined in the MenuActivity.java.

public class MenuActivity extends Activity {

      TextView menu1, menu2;

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

           menu1 = (TextView)findViewById(R.id.menu1);
           menu2 = (TextView)findViewById(R.id.menu2);

           menu1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                     // TODO Auto-generated method stub
                     Toast.makeText(getApplicationContext(), "menu 1 clicked", 5000).show();
                }
           });

           menu2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                      // TODO Auto-generated method stub
                      Toast.makeText(getApplicationContext(), "menu 2 clicked", 5000).show();
                }
           });

}

How do I inherit the OnClick listener in MainActivity. In short, my code in MenuActivity has to be used in various other activities, how do I use it? Please support with a piece of code and I have done plenty of searching but did not find anything relevant. I am new to Android, any help would be appreciated.

Upvotes: 4

Views: 9437

Answers (3)

vilpe89
vilpe89

Reputation: 4702

Make a base class that you use in every activity where you use those menu buttons:

public class BaseActivity extends Activity implements OnClickListener {

    @Override
    protected void onClick(View view) {
        switch(view.getId()) {
        case R.id.menu1:
            Toast.makeText(this, "Menu1 clicked.", Toast.LENGTH_SHORT).show();
        break;

        case R.id.menu2:
            Toast.makeText(this, "Menu2 clicked.", Toast.LENGTH_SHORT).show();
        break;
        }
    }
}

And then do this in your activies:

public class MainActivity extends BaseActivity {

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

        findViewById(R.id.menu1).setOnClickListener(this);
        findViewById(R.id.menu2).setOnClickListener(this);
    }
}

And in other activity you can do the same:

public class SecondActivity extends BaseActivity {

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

        findViewById(R.id.menu1).setOnClickListener(this);
        findViewById(R.id.menu2).setOnClickListener(this);
    }
}

Just remember to include that menu layout in your activitys layout so you dont get null pointers.

Upvotes: 5

You don't inherit OnClickListener you implement it!
Each activity implements its own onClick() which gives more flexibility to your code.
EDIT
after reviewing your code you can simplify things which I believe it's what you want, you can implement OnClickListner to your activity and write one onClick method for all clickables in your activity here you can do this

public class MenuActivity extends Activity implements OnClickListener
{
    TextView menu1, menu2;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        menu1 = (TextView)findViewById(R.id.menu1);
        menu2 = (TextView)findViewById(R.id.menu2);
        menu1.setOnClickListener(this);
        menu2.setOnClickListener(this);
    }
    @override
    onClick( View view )
    {
        if (view == menu1)
             Toast.makeText(getApplicationContext(), "menu 1 clicked", Toast.LONG).show();
        if (view == menu2)
             Toast.makeText(getApplicationContext(), "menu 2 clicked", Toast.LONG).show();
    }
}

And it's better not to reference textviews if you won't use them

public class MenuActivity extends Activity implements OnClickListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        findViewById(R.id.menu1).setOnClickListener(this);
        findViewById(R.id.menu2).setOnClickListener(this);

    }
    @override
    onClick( View view )
    {
        if (view.getId() == R.id.menu1)
             Toast.makeText(getApplicationContext(), "menu 1 clicked", Toast.LONG).show();
        if (view.getId() == R.id.menu2)
             Toast.makeText(getApplicationContext(), "menu 2 clicked", Toast.LONG).show();
    }
}

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

You cannot inherit the click listeners of Menu Activity in Main activity. Each activity has its own click listeners. Although you can make a general method to perform some task and call that method from Both Activities but cannot inherit it like this way.

Upvotes: 0

Related Questions