user742030
user742030

Reputation:

How to Add Click Listeners to Inflated XML Buttons?

I am implementing Pakerfeldts' viewflow (Specifically the 'DiffViewFlowExample') in my main activity and I cant figure how to add click listeners to ImageButtons that are in my inflated views.

So how do I add a click listener to the image button w/an @id=regulatoryDocsBtn located in the dashboard_view?

Below are code snippets & I thank you in advance for any help.

Main Activity Class:

public class Home extends Activity {

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

  /**
   * Setup the ViewFlow
   * w/TitleFlow------------------------------------------
   */
   ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow);
   DiffAdapter vfAdapter = new DiffAdapter(this);
   viewFlow.setAdapter(vfAdapter);

   TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
   indicator.setTitleProvider(vfAdapter);
   viewFlow.setFlowIndicator(indicator);
}

DiffAdapter Class:Note:The inflated views are @ the bottom of this class.

public class DiffAdapter extends BaseAdapter implements TitleProvider {

private static final int VIEW1 = 0;
private static final int VIEW2 = 1;
private static final int VIEW_MAX_COUNT = VIEW2 + 1;
private final String[] names = { "DashBoard", "ToolBox" };

private LayoutInflater mInflater;

public DiffAdapter(Context context) {
mInflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override(S)...............

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int view = getItemViewType(position);
if (convertView == null) {
    switch (view) {
    case VIEW1:
    convertView = mInflater.inflate(R.layout.dashboard_view, null);
    break;
    case VIEW2:
    convertView = mInflater.inflate(R.layout.toolbox_view, null);
    break;
    }
}
return convertView;
}

A Snippet of one of the views:dashboard_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#10000000"
    android:stretchColumns="*" >

    <!-- Row 1 -->

    <TableRow android:layout_weight="1" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageButton
                android:id="@+id/regulatoryDocsBtn"
                style="@style/dashBoardimageBtn"
                android:src="@drawable/ic_launcher_regulatory" />

            <TextView
                style="@style/dashBoardTxt"
                android:layout_below="@id/regulatoryDocsBtn"
                android:text="Regulatory Docs" />
        </RelativeLayout>
        .....
        </TableRow>

Upvotes: 0

Views: 3227

Answers (3)

MuraliGanesan
MuraliGanesan

Reputation: 3261

Instead of Button use TextView and write click listener on that TextView.

Upvotes: 0

sabadow
sabadow

Reputation: 5161

You can add the property onClick to the button in the xml, indicating the name of the method to execute.

The method must have a View param.

For example:

In XML layout:

<Button android:id="@+id/testButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="testClick" />

In Activity code:

public void testClick(View v) {
    // The code to be executed on click
}

Hope it helps.

Upvotes: 4

Jaa-c
Jaa-c

Reputation: 5157

You just have to get the button using

ImageButton rdb = (ImageButton) findViewById(R.id.regulatoryDocsBtn);

and add a listener to it:

rdb.setOnClickListener(new RdbOnClickListener());
...

class RdbOnClickListener implements OnClickListener {
    public void onClick(View v) {
         //do what you gotta do
    }
}

or simply:

rdb.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
         //do what you gotta do
    }
});

That should do it. It's all in the example in documentation :)

Upvotes: 1

Related Questions