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