Question

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?

Was it helpful?

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();

OTHER TIPS

I'm not sure. but can you please try with extending the DropDownMenu-Activity instead of Activity.

 public class NewActivity extends DropDownMenu {
 // override which you want to override 
}

And make sure DropDownMenu in such a way that you can extends what you want to implements. Hope you have got my point.

Consistency Issue (Recommendation)

There is a HUGE misnomer in your class name, you should NOT call an Activity DropDownMenu. This is confusing, you should instead call it DropDownActivity or something like that. Also do you really need to use an Activity? You do realize a View can still listen to the click of a button right? Not only an Activity can listen to button clicks. If you encapsulate all of this in a View and have a dedicated listener for that view, then you wouldn't have any need for another Activity.

Possible Solution

It's likely related to the activity/component you have registered to listen to the buttons is not correct. Validate your code so that you can at least get the correct response of the onClick.

The problem is that you are attaching the listener to addInquiry in the DropDownMenu:

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

This listener is not attached to the TextView in the extended class, since the setContentView is called and layout is refreshed.

Possible solution, if you are not using the DropDownMenu activity on its own then simply (remove setContentView):

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

Not the best solution but would work.

Update: Better Solution (1) Let the subclass decide which layout to show. This layout must contain the menu (which is true in your case).

public class DropDownMenu extends Activity {

    private TextView addInquiry, addEvent, additionalInfo, addToContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _setContentView();

        //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());
    }

    protected void _setContentView() {
        setContentView(R.layout.activity_drop_down_menu);
    }
    ...
    ...
}

@override the _setContentView method so that we do not set the view twice.

public class SalesInqury extends DropDownMenu {

    ...
    ...

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

        //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);
    }

    @Override
    protected void _setContentView() {
        // need to make sure this layout contains the menu (otherwise NullPointerException may arise)
        setContentView(R.layout.activity_sales_inqury); 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top