Reputation: 31
How can make something like this?
?
For my aplication I want some list of image button and if I've press one button I want to do something. I've really search on all google to find that and I've found some example but all of them are to complex and have so more details
Can anyone suggest me one tutorial or example about how to make a SIMPLE list of image button?
Upvotes: 1
Views: 3174
Reputation: 1068
Well a simple way to make a list of buttons is creating an adapter. You would use this by doing something list ButtonListAdapter buttonListAdapter = new ButtonListAdapter(context, List<"of whatever you send in">);.
And then with the list list.setAdapter(buttonListAdapter);
class ButtonListAdapater extends BaseAdapter
{
Context mContext;
private LayoutInflater mInflater;
List<"whatever you want to pass in such as images or textfiels or classes"> mDatas;
public ButtonListAdapater (Context context, List<"whatever you want to pass in such as images or textfiels or classes"> results)
{
mContext = context;
mDatas = results;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount()
{
return mDatas.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
"whatever you want to pass in such as images or textfiels or classes" data = mDatas.get(position);
if (convertView == null) {
"whatever layout you made, on click effect can be in layout on in code"
convertView = mInflater.inflate(R.layout.data_list_item, null);
holder = new ViewHolder();
holder.tvButton = (TextView) convertView.findViewById(R.id.textViewButton);
holder.lbButton = (ImageButton) convertView.findViewById(R.id.Button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvButton.setText(data.getData());
"With the button here it depends on what you want to do and how, since its perfectly fine to put an onclicklistener here or in the layout"
holder.llButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, OTherActivity.class);
startActivity(intent)
}
});
return convertView;
}
static class ViewHolder
{
TextView tvButton;
ImageButton lbButton;
}
And data_list_item layout xml can be something simple such as
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/R.id.Button/>
</LinearLayout>
Upvotes: 4