Articles → ANDROID → Button control in android

Button control in android






What is button control?





Example





Click to Enlarge



public void ClickEvent(View view) {
    Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
}



Click event using setOnClickListener




Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(EditTextDemo.this, "Button Clicked", Toast.LENGTH_LONG).show();
    }
});




public class EditTextDemo extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text_demo);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show();
    }
}



Output



Click to Enlarge


ImageButton in android







Click to Enlarge




Click to Enlarge




Click to Enlarge


Setting both image and text on the button




AttributeDescription
android:drawableBottomThe image to be drawn below the text.
android:drawableEndThe image to be drawn to the end the text.
android:drawableLeftThe image to be drawn to the left the text.
android:drawableRightThe image to be drawn to the right the text.
android:drawableStartThe image to be drawn to the start of the text.



Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, November 20, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250