user1446632
user1446632

Reputation: 427

How to add imagebuttons to gridview in android

I can not figure out how to add imagebuttons to my gridview. I have to have individual code to execute for each imagebutton, because they do completely different things. How can I do it?

I ended up using a tablelayout instead

Code:

package com.mysoftware.mysoftwareos.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Import views

        //Setup onClickListener for the buttons

        //Setup GridView
    }

    @Override
    public boolean onKeyDown(int KeyCode, KeyEvent event) {
        if ((KeyCode == KeyEvent.KEYCODE_BACK)) {
        //Do nothing to prevent the back button to kill the launcher
        return true;
        }
    return super.onKeyDown(KeyCode, event);
    }

    public void onClick(View src) {
        switch(src.getId()) {
        }
    }
}

Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF8090A0" >

<GridView
    android:id="@+id/appsGridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:numColumns="3" >
</GridView>

</RelativeLayout>

Upvotes: 0

Views: 1857

Answers (1)

toadzky
toadzky

Reputation: 3846

A gridview has an onitemclickedlistener you can use, but if the buttons are supposed to do different things, and you aren't loading from some kind of data store, you probably don't want a gridview anyway. look into the DashboardLayout from Roman Nurik a couple years ago. You can just add the ImageButtons to it either in XML or code. If you add in code, you can assign each button an anonymous onClickListener. If you do it in XML you can set the onClick attribute to a method in your activity.

Upvotes: 1

Related Questions