Reputation: 41099
I have created a custom view in which I want to implement some onClick
actions.
I created the following:
Custom View XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/leftMenu"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="@drawable/left_menu_background"
android:orientation="vertical" >
<ImageButton
android:id="@+id/left_menu_close_button"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:background="@drawable/left_menu_close_button" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/left_menu_buttons_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/left_menu_data_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_button_transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageButton
android:id="@+id/left_menu_data_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="@drawable/folder" />
<TextView
android:id="@+id/left_menu_data_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Data"
android:textColor="@color/my_white" />
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
<LinearLayout
android:id="@+id/left_menu_settings_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<ImageButton
android:id="@+id/left_menu_settings_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="@drawable/settings" />
<TextView
android:id="@+id/left_menu_settings_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Settings"
android:textColor="@color/my_white" >
</TextView>
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/left_menu_separator" />
</LinearLayout>
</ScrollView>
</LinearLayout>
AppViewLinearLayout:
package com.emildesign.sgreportmanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
public class AppViewLinearLayout extends LinearLayout {
public AppViewLinearLayout(Context context, AttributeSet attrs, int layoutResource)
{
super(context, attrs);
if (isInEditMode())
return;
View view = LayoutInflater.from(context).inflate(layoutResource, null);
addView(view);
}
}
LeftSideMenu:
public class LeftSideMenu extends AppViewLinearLayout
{
private LeftSideMenuClickListener mListener;
public LeftSideMenu(Context context, AttributeSet attrs)
{
super(context, attrs, R.layout.left_menu);
findViewById(R.id.left_menu_data_layout).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_data_image).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_data_text).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_settings_layout).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_settings_image).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_settings_text).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
findViewById(R.id.left_menu_close_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if (mListener != null)
mListener.closeMenuOnClick(v);
}
});
}
public void setListener(LeftSideMenuClickListener listener)
{
mListener = listener;
}
public interface LeftSideMenuClickListener
{
void dataOnClick(View v);
void settingsOnClick(View v);
void closeMenuOnClick(View v);
}
}
In my activity I do this:
public class ReportsTableActivity extends Activity
{
LeftSideMenu leftSideMenu;
static final String TAG = ReportsTableActivity.class.getSimpleName();
private SGRaportManagerAppObj application;
private List<Report> reportsRepository;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
setContentView(R.layout.reports_list_activity_layout);
application = (SGRaportManagerAppObj)getApplication();
reportsRepository = application.reportsRepository.getReportsRepository();
createReportsList();
if (leftSideMenu != null)
{
leftSideMenu.setListener(new LeftSideMenu.LeftSideMenuClickListener() {
@Override
public void settingsOnClick(View v) {
}
@Override
public void dataOnClick(View v) {
}
@Override
public void closeMenuOnClick(View v)
{
Log.d(TAG, "Close left menu button was clicked");
ToggleButton button = (ToggleButton) findViewById(R.id.showLeftMenuButton);
button.setChecked(false);
leftSideMenu.setVisibility(View.GONE);
}
});
}
//rest of the code...
The Problem: for some reason the closeMenuOnClick is never called and the log: "Close left menu button was clicked"
is never presented.
My guess is that the problem happens because I make this check: if (leftSideMenu != null)
and only after I set the Listener. the reason I do that is because my custom view is with viability = gone
when the activity is presented.
Any advice on how to do it right will be very appreciated.
Thanks.
Upvotes: 0
Views: 357
Reputation: 24720
change:
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
setContentView(R.layout.reports_list_activity_layout);
to:
setContentView(R.layout.reports_list_activity_layout);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
also don't create 7 OnClickListener, make one:
OnClickListener mClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener == null) {
return;
}
int id = v.getId();
if (id == R.id.left_menu_data_layout ||
id == R.id.left_menu_data_image ||
id == R.id.left_menu_data_text) {
// skipped three more ids
mListener.settingsOnClick(v);
} else {
mListener.closeMenuOnClick(v);
}
}
}
and register:
// repeat 7 times
findViewById(R.id.xxx).setOnClickListener(mClickListener);
or even better let LeftSideMenu implements OnClickListener and register:
// repeat 7 times
findViewById(R.id.xxx).setOnClickListener(this);
Upvotes: 1
Reputation: 1458
I think you need two swap this two lines. You need to set content view first and only then call findViewById.
setContentView(R.layout.reports_list_activity_layout);
leftSideMenu = (LeftSideMenu) findViewById(R.id.leftSideMenu);
Upvotes: 1