Sagar Zala
Sagar Zala

Reputation: 45

How to create Button Object which is in another layout xml file in Android

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;
}

Upvotes: 1

Views: 4329

Answers (3)

Piyush
Piyush

Reputation: 18933

You have to create button object in adapter where u r inflating list item layout.

like

  Button b = (Button)yourViewobject.findViewById(R.id.button1);

Now implement onClick event for that

  b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

EDIT: Use this. Make one custom file in drawable folder named mybutton.xml

   <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@drawable/selected" android:state_selected="true"></item>

   <item android:drawable="@drawable/normal"></item>

   </selector>

Now just in your custom layout file set background for your both buttons.

   android:background="@drawable/mybutton" // for Hindi Button

and same as for english button.

And also now remove onClick event method from your adapter.

Need to change..

    <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:clickable = "true"
            android:focusable = "true"
            android:layout_alignTop="@+id/btn_hindi"                    
            android:layout_alignBottom="@+id/ImageView12"
            android:layout_alignLeft="@+id/ImageView12"
            android:background="@drawable/mybutton"
            android:text="ENG"
            android:textColor="@android:color/white" />

Upvotes: 3

Sishin
Sishin

Reputation: 2001

  public class CustomAdapter  extends BaseAdapter
{

private Context mContext;
Cursor cursor;
public CustomAdapter(Context context,Cursor cur) 
{
        super();
        mContext=context;
        cursor=cur;

}

public int getCount() 
{
    // return the number of records in cursor
    return cursor.getCount();
}

// getView method is called for each item of ListView
public View getView(int position,  View view, ViewGroup parent) 
{
                // inflate the layout for each item of listView
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listview_each_item, null);


                                    // fetch the sender number and sms body from cursor
                String Number="123456";

                // get the reference of textViews
                TextView Number=(TextView)view.findViewById(R.id.textViewSMSSender);
                Button btn=(Button)view.findViewById(R.id.button);

                // Set the Sender number and smsBody to respective TextViews 
                Number.setText(senderNumber);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});


                return view;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

}

Upvotes: 0

MohsinSyd
MohsinSyd

Reputation: 175

Do this in your custom adapter

@Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
         ViewHolder viewHolder;
       if (convertView == null) {

        convertView = inflater.inflate(R.layout.list_item, null);
        viewHolder=new ViewHolderforGroup();
           viewHolder.btn = (Button)convertView.findViewById(R.id.btn);
          convertView.setTag(viewHolder)
       }else{
           viewHolder = (ViewHolder)convertView.getTag(viewHolder);
       }

       viewHolder.btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {


                }
            });


    }

    static class ViewHolder{
      Button btn;

    }

Upvotes: 0

Related Questions