How do I make my layout clickable?
For example, I divide the screen into 2 layouts, 1 green with 50% of the screen and the other red with the other 50%. How do you make them clickable?
How do I make my layout clickable?
For example, I divide the screen into 2 layouts, 1 green with 50% of the screen and the other red with the other 50%. How do you make them clickable?
Set the XML of your layouts to the tag:
android:clickable=“true”
After that, you only have to treat the click as you treat the click in any other view, like a button, for example.
LinearLayout meuComponente = (LinearLayout) findViewById (R.id.meu_componente);
meuComponente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(this, "hello", Toast.LENGTH_LONG).show();
//Insira qualquer comportamento aqui. (:
}
});
Do not forget to set the attribute of your LinearLayout
:
android:clickable="true"
If you want elements within LinearLayout
not to be clickable, use: android:clickable="false"
for all elements.