Articles → ANDROID → Button control in android

Button control in android






What is button control?





Example




Picture showing the button control to be added in the android screen in the design mode

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


Picture showing the output of the button on the android screen

Click to Enlarge


ImageButton in android






Picture showing the resource window when the image button is dragged and dropped on the design mode

Click to Enlarge



Picture showing the image button in the component tree

Click to Enlarge



Picture showing the output of the image button on the android screen

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