javaandroidinheritanceandroid-activityslidingdrawer

Methods in extended class are not working


I created a menu, where you can swipe up and down. I created this menu as another activity. Now, I need this menu to be added to on other activities.

Following is the code of my menu(SlidingDrawer)

Java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DropDownMenu extends Activity {

    private TextView addInquiry, addEvent, additionalInfo, addToContacts;

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

        //Intializing instance variables
        addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
        addEvent = (TextView)findViewById(R.id.menu_add_event);
        additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
        addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);

        //Register the Listeners
        addInquiry.setOnClickListener(new AddInquiryEvent());


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.drop_down_menu, menu);
        return true;
    }

    //Test Button
    private class AddInquiryEvent implements OnClickListener
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(DropDownMenu.this,NewLead.class);
            startActivity(intent);

        }

    }

}

XML

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

    <SlidingDrawer
        android:id="@+id/SlidingDrawer"
        android:layout_width="wrap_content"
        android:layout_height="250dip"
        android:layout_alignParentBottom="true"
        android:content="@+id/contentLayout"
        android:handle="@+id/slideButton"
        android:orientation="vertical" >

        <Button
            android:id="@+id/slideButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/bottom_bar" >
        </Button>

        <LinearLayout
            android:id="@+id/contentLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip"
            android:background="#cbcbcc" >

            <TextView
                android:id="@+id/menu_add_inquiry"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@string/add_inquiry" 
                android:textColor="#ffffff"
                android:clickable="true"
                android:textAppearance="?android:attr/textAppearanceMedium" />




        </LinearLayout>
    </SlidingDrawer>

</RelativeLayout>

Following is the Code of the other activity

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sales_inqury_main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
     android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SalesInqury" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:text="@string/sales_inqury"
        android:textSize="40sp" />


        <include layout = "@layout/activity_drop_down_menu"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

Java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SalesInqury extends DropDownMenu {

    private ImageView addNewSalesInqury;
    private RelativeLayout salesInquryMainLayout;
    private TextView testEditSales;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_sales_inqury);

        //Intializing instance variables
        addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
        salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
        testEditSales = (TextView)findViewById(R.id.testWord);



    }
}

But, there is an issue. Even thougn I can open the menu in this other activity, I can't click on it's Buttons (TextViews) and navigate to other activities. Why is this?


Solution

  • I think it's because after you've set a contentView in your DropDownMenu activity and bound the listener to the button, in SalesInquiry you set a different contentView. Although this includes the menu-layout, the Views are created anew, so there's no Listener bound to the button anymore.

    A solution would either be to specify the menu callbacks in the layout xml via android:onClick="..." or by having a separate method in DropDownMenu that adds the Listener and that you call from your SalesInquiry class after setting the content view.

    Example 1, specifying callback in XML

    Add android:onClick="onMenuItemClicked" to menu item TextView

    <TextView
            android:id="@+id/menu_add_inquiry"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/add_inquiry" 
            android:textColor="#ffffff"
            android:clickable="true"
            android:onClick="onMenuItemClicked"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    

    Add this method to DropDownMenu class and remove the AddInquiryEvent class and the registering of the listeners.

    public void onMenuItemClicked(View view) {
        switch (view.getId()) {
        case R.id.menu_add_inquiry:
            Intent intent = new Intent(DropDownMenu.this,NewLead.class);
            startActivity(intent);
            break;
        case R.id.menu_other:
            // handle other menu item
            break;
        // ... and so on ...
        default:;
        }
    }
    

    Example 2, bind listeners in a separate method

    In DropDownMenu move the registering of the Listeners to a separate method:

    protected void registerListeners() {
        //Register the Listeners
        findViewById(R.id.menu_add_inquiry).setOnClickListener(new AddInquiryEvent());
    }
    

    Note, that you have to find the view again, as the one kept in the local variable is the wrong one, after SalesInquiry set it's content view.

    Call the method in SalesInquiry after setting the content view.

      setContentView(R.layout.activity_sales_inqury);
      registerListeners();