Android -Adding a button in android studio example
- Every Button is styled using the system's default button background,which is often
different from one device to another and from one version of the platform to another.
If you're not satisfied with the default button style and want to customize it to match
the design of your application, then you can replace the button's background image with a
state list drawable. A state list drawable is a drawable resource defined in XML that changes
its image based on the current state of the button. Once you've defined a state list drawable
in XML, you can apply it to your Button with the android:background attribute.
Step 1: create a button and Text view in first activity
- button=(Button)findViewById(R.id.button);
Step 2: Add on click listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//todo code;
}
});
Step 3: Inside on click create a statement
txv1.setText("Hello world!!");
Complete coding will be
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
TextView txv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
txv1=(TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txv1.setText("Hello world!!");
}
});
}
}
Output:
Leave a Comment