Examples Android Studio
Examples Android Studio
If you want to truly redefine the appearance of your button, you can specify a custom
background. Instead of supplying a simple bitmap or color, however, your background
should be a state list resource that changes appearance depending on the button's
current state.
You can define the state list in an XML file that defines three different images or colors
to use for the different button states.
1. Create three bitmaps for the button background that represent the default, pressed, and
focused button states.
To ensure that your images fit buttons of various sizes, create the bitmaps as Nine-
patch bitmaps.
2. Place the bitmaps into the res/drawable/ directory of your project. Be sure each bitmap
is named properly to reflect the button state that they each represent, such
as button_default.9.png, button_pressed.9.png, and button_focused.9.png.
3. Create a new XML file in the res/drawable/ directory (name it something
like button_custom.xml). Insert the following XML:
This defines a single drawable resource, which will change its image based on the
current state of the button.
The first <item> defines the bitmap to use when the button is pressed (activated).
The second <item> defines the bitmap to use when the button is focused (when the
button is highlighted using the trackball or directional pad).
The third <item> defines the bitmap to use when the button is in the default state (it's
neither pressed nor focused).
Note: The order of the <item> elements is important. When this drawable is referenced,
the <item> elements are traversed in-order to determine which one is appropriate for the current
button state. Because the default bitmap is last, it is only applied when the
conditions android:state_pressed and android:state_focused have both evaluated as false.
This XML file now represents a single drawable resource and when referenced by
a Button for its background, the image displayed will change based on these three
states.
4. Then simply apply the drawable XML file as the button background:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
android:background="@drawable/button_custom" />
For more information about this XML syntax, including how to define a disabled,
hovered, or other button states, read about State List Drawable.
II. example
Drag the three checkboxes and one button for the layout. Now the
activity_main.xml file will look like this:
File: activity_main.xml
Activity class
File: MainActivity.java
1. package example.javatpoint.com.checkbox;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.widget.Button;
7. import android.widget.CheckBox;
8. import android.widget.Toast;
9.
10.public class MainActivity extends AppCompatActivity {
11. CheckBox pizza,coffe,burger;
12. Button buttonOrder;
13. @Override
14. protected void onCreate(Bundle savedInstanceState) {
15. super.onCreate(savedInstanceState);
16. setContentView(R.layout.activity_main);
17. addListenerOnButtonClick();
18. }
19. public void addListenerOnButtonClick(){
20. //Getting instance of CheckBoxes and Button from the activty_main.xml file
21. pizza=(CheckBox)findViewById(R.id.checkBox);
22. coffe=(CheckBox)findViewById(R.id.checkBox2);
23. burger=(CheckBox)findViewById(R.id.checkBox3);
24. buttonOrder=(Button)findViewById(R.id.button);
25.
26. //Applying the Listener on the Button click
27. buttonOrder.setOnClickListener(new View.OnClickListener(){
28.
29. @Override
30. public void onClick(View view) {
31. int totalamount=0;
32. StringBuilder result=new StringBuilder();
33. result.append("Selected Items:");
34. if(pizza.isChecked()){
35. result.append("\nPizza 100Rs");
36. totalamount+=100;
37. }
38. if(coffe.isChecked()){
39. result.append("\nCoffe 50Rs");
40. totalamount+=50;
41. }
42. if(burger.isChecked()){
43. result.append("\nBurger 120Rs");
44. totalamount+=120;
45. }
46. result.append("\nTotal: "+totalamount+"Rs");
47. //Displaying the message on the toast
48. Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_
LONG).show();
49. }
50.
51. });
52. }
53.}
Output:
3. RadioButton example
Android RadioButton
RadioButton is a two states button which is either checked or unchecked. If
a single radio button is unchecked, we can click it to make checked radio
button. Once a radio button is checked, it cannot be marked as unchecked
by user.
RadioButton is generally used with RadioGroup. RadioGroup contains several
radio buttons, marking one radio button as checked makes all other radio
buttons as unchecked.
activity_main.xml
File: activity_main.xml
Play Videox
Activity class
File: MainActivity.java
1. package example.javatpoint.com.radiobutton;
2.
3. import android.support.v7.app.AppCompatActivity;
4. import android.os.Bundle;
5. import android.view.View;
6. import android.widget.Button;
7. import android.widget.RadioButton;
8. import android.widget.RadioGroup;
9. import android.widget.Toast;
10.
11.public class MainActivity extends AppCompatActivity {
12. Button button;
13. RadioButton genderradioButton;
14. RadioGroup radioGroup;
15. @Override
16. protected void onCreate(Bundle savedInstanceState) {
17. super.onCreate(savedInstanceState);
18. setContentView(R.layout.activity_main);
19. radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
20. }
21. public void onclickbuttonMethod(View v){
22. int selectedId = radioGroup.getCheckedRadioButtonId();
23. genderradioButton = (RadioButton) findViewById(selectedId);
24. if(selectedId==-1){
25. Toast.makeText(MainActivity.this,"Nothing selected", Toast.LENGTH_SHORT
).show();
26. }
27. else{
28. Toast.makeText(MainActivity.this,genderradioButton.getText(), Toast.LENG
TH_SHORT).show();
29. }
30.
31. }
32.}
Output
Example
package com.gtappdevelopers.kotlingfgproject;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGrp = findViewById(R.id.idRadioGroup);
statusTV = findViewById(R.id.idTVStatus);
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
statusTV.setText(radioButton.getText());
});
Layout file
<RadioButton
android:id="@+id/idBtnKotlinRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Kotlin"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
<RadioButton
android:id="@+id/idBtnFlutterRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Flutter"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />
</RadioGroup>
</RelativeLayout>
#include <stdio.h>
int main()
{ int i,j,n,a[100][100];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",a[i][j]);
int s=0;
for(j=0;j<n;j++)
for (i=0;i<n;i++)
if ()
return 0;
}