I've just created a ScrollView
that has a LinearLayout
inside. This LinearLayout
has a button on the top, and there is nothing I can do. The button's onClickListener is never called.
I want to detect when a user presses the LinearLayout
and when presses the Button.
Here is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layoutClick(v);
}
});
LinearLayout.LayoutParams headerParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,getDpFromPixel(50));
//bottone per l'header
Button headerButton = new Button(this);
headerButton.setId(findId());
headerButton.setLayoutParams(headerParam);
//headerButton.setBackground(getResources().getColor(R.color.button_backgroud));
//headerButton.setBackground(getResources().getColor(R.color.button_backgroud));
Drawable d = getResources().getDrawable(R.color.button_backgroud);
headerButton.setBackground(d);
headerButton.setText(title);
headerButton.setTextColor(getResources().getColor(R.color.white));
headerButton.setGravity(Gravity.CENTER_VERTICAL);
headerButton.setPadding(70, 0, 0, 0);
headerButton.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
headerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonPressed(v);
System.out.println("button pressed!!");
}
});
layout.addView(headerButton);
The "layoutClick" function is working, the headerButton is never calling the "buttonPressed" function or printing to console..
Try making focusable the button
headerButton.setFocusable(true);
headerButton.setClickable(true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.