lundi 4 février 2013

Adding headers button to a ListView

Here is an example showing how to add headers button to a ListView

Button btn1 = new Button(MyActivity.this);
btn1.setText("button1");
btn1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn1.setGravity(Gravity.CENTER);
btn1.setOnClickListener(new OnClickListener() {   
 @Override
 public void onClick(View v) {
  // do something
 }
});
Button btn2 = new Button(MyActivity.this);
btn2.setText("button2");
btn2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn2.setGravity(Gravity.CENTER);
btn2.setOnClickListener(new OnClickListener() {   
 @Override
 public void onClick(View v) {
  // do something
 }
});
        
LinearLayout layout = new LinearLayout(this);  
layout.setOrientation(LinearLayout.HORIZONTAL);  
layout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
layout.setGravity(Gravity.CENTER);  
layout.addView(btn1);  
layout.addView(btn2);
myList.addHeaderView(layout);//or getListView().addHeaderView(layout);