Hello I have create an Android application, in that I add some buttons and images in ListView using CustomAdapter.
My Files Structure is:
layout
[1] album ->List View
[2] album_list -> Button
So how I create Button Object ?
My Code is:
album.xml
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0" >
</ListView>
</GridLayout>
album_list.xml
<RelativeLayout
android:id="@+id/RelativeLayout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/btn_hindi"
android:layout_alignBottom="@+id/ImageView12"
android:layout_alignLeft="@+id/ImageView12"
android:background="@drawable/normal"
android:text="ENG"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/tv_albumname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_go"
android:layout_alignTop="@+id/iv_album_image"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="Album Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_hindi"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginTop="7dp"
android:layout_marginRight="17dp"
android:layout_alignTop="@+id/ImageView12"
android:layout_alignLeft="@+id/btn_eng"
android:background="@drawable/normal"
android:text="HINDI"
android:textColor="@android:color/white" />
</RelativeLayout>
Album.java
Album_List_Custom_Adapter adapter =
new Album_List_Custom_Adapter(getApplicationContext(), albumList);
lv.setAdapter(adapter);
Album_List_Custom_Adapter.java
private Context context;
ArrayList<HashMap<String, String>> listAlbum;
ViewHolder vholder;
private OnClickListener listener;
public Album_List_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList)
{
this.context = context;
this.listAlbum=albumList;
}
@Override
public int getCount()
{
return listAlbum.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)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi=convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.home_list_model, null);
vholder = new ViewHolder();
vholder.hindi=(Button)vi.findViewById(R.id.btn_hindi);
vholder.eng=(Button)vi.findViewById(R.id.btn_eng);
vholder.tv_album_name = (TextView) vi.findViewById(R.id.tv_albumname);
vi.setTag(vholder);
}
else
{
vholder = (ViewHolder) (vi.getTag());
}
vholder.hindi.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
vholder.hindi.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.hindi.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.eng.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.hindi.setBackgroundResource(R.drawable.normal);
}
});
vholder.tv_album_name.setText(listAlbum.get(position).get("album"));
return vi;
}
static class ViewHolder
{
TextView tv_album_name;
Button eng, hindi;
}