0% found this document useful (0 votes)
5 views18 pages

Examples Android Studio

Examples in Android Studio

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Examples Android Studio

Examples in Android Studio

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Custom background

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.

To create a state list drawable for your button background:

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:

<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>

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

Android CheckBox Example


activity_main.xml

Drag the three checkboxes and one button for the layout. Now the
activity_main.xml file will look like this:

File: activity_main.xml

1. <?xml version="1.0" encoding="utf-8"?>


2. <android.support.constraint.ConstraintLayout xmlns:android="http://
schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.checkbox.MainActivity">
8.
9.
10. <CheckBox
11. android:id="@+id/checkBox"
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:layout_marginLeft="144dp"
15. android:layout_marginTop="68dp"
16. android:text="Pizza"
17. app:layout_constraintStart_toStartOf="parent"
18. app:layout_constraintTop_toTopOf="parent" />
19.
20. <CheckBox
21. android:id="@+id/checkBox2"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:layout_marginLeft="144dp"
25. android:layout_marginTop="28dp"
26. android:text="Coffee"
27. app:layout_constraintStart_toStartOf="parent"
28. app:layout_constraintTop_toBottomOf="@+id/checkBox" />
29.
30. <CheckBox
31. android:id="@+id/checkBox3"
32. android:layout_width="wrap_content"
33. android:layout_height="wrap_content"
34. android:layout_marginLeft="144dp"
35. android:layout_marginTop="28dp"
36. android:text="Burger"
37. app:layout_constraintStart_toStartOf="parent"
38. app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
39.
40. <Button
41. android:id="@+id/button"
42. android:layout_width="wrap_content"
43. android:layout_height="wrap_content"
44. android:layout_marginLeft="144dp"
45. android:layout_marginTop="184dp"
46. android:text="Order"
47. app:layout_constraintStart_toStartOf="parent"
48. app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
49.
50.</android.support.constraint.ConstraintLayout>

Activity class

Let's write the code to check which toggle button is ON/OFF.

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.

Example of Radio Button


In this example, we are going to implement single radio button separately as
well as radio button in RadioGroup.

activity_main.xml
File: activity_main.xml

Play Videox

1. <?xml version="1.0" encoding="utf-8"?>


2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. android:orientation="vertical"
8. tools:context="example.javatpoint.com.radiobutton.MainActivity">
9.
10. <TextView
11. android:id="@+id/textView1"
12. android:layout_width="fill_parent"
13. android:layout_height="wrap_content"
14. android:layout_marginTop="30dp"
15. android:gravity="center_horizontal"
16. android:textSize="22dp"
17. android:text="Single Radio Buttons" />
18.
19.
20.
21. <!-- Default RadioButtons -->
22.
23.
24. <RadioButton
25. android:id="@+id/radioButton1"
26. android:layout_width="fill_parent"
27. android:layout_height="wrap_content"
28. android:layout_gravity="center_horizontal"
29. android:text="Radio Button 1"
30. android:layout_marginTop="20dp"
31.
32. android:textSize="20dp" />
33. <RadioButton
34. android:id="@+id/radioButton2"
35. android:layout_width="fill_parent"
36. android:layout_height="wrap_content"
37. android:text="Radio Button 2"
38. android:layout_marginTop="10dp"
39.
40. android:textSize="20dp" />
41.
42.
43. <View
44. android:layout_width="fill_parent"
45. android:layout_height="1dp"
46. android:layout_marginTop="20dp"
47. android:background="#B8B894" />
48.
49. <TextView
50. android:id="@+id/textView2"
51. android:layout_width="fill_parent"
52. android:layout_height="wrap_content"
53. android:layout_marginTop="30dp"
54. android:gravity="center_horizontal"
55. android:textSize="22dp"
56. android:text="Radio button inside RadioGroup" />
57.
58.
59. <!-- Customized RadioButtons -->
60.
61.
62. <RadioGroup
63. android:layout_width="wrap_content"
64. android:layout_height="wrap_content"
65. android:id="@+id/radioGroup">
66.
67. <RadioButton
68. android:id="@+id/radioMale"
69. android:layout_width="fill_parent"
70. android:layout_height="wrap_content"
71. android:text=" Male"
72. android:layout_marginTop="10dp"
73. android:checked="false"
74. android:textSize="20dp" />
75.
76. <RadioButton
77. android:id="@+id/radioFemale"
78. android:layout_width="fill_parent"
79. android:layout_height="wrap_content"
80. android:text=" Female"
81. android:layout_marginTop="20dp"
82. android:checked="false"
83.
84. android:textSize="20dp" />
85. </RadioGroup>
86.
87. <Button
88. android:layout_width="wrap_content"
89. android:layout_height="wrap_content"
90. android:text="Show Selected"
91. android:id="@+id/button"
92. android:onClick="onclickbuttonMethod"
93. android:layout_gravity="center_horizontal" />
94.
95.
96.</LinearLayout>

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

omments are added in the code to get to know in detail.


 Kotlin
 Java

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;

public class MainActivity extends AppCompatActivity {

// on below line we are creating a variable.

private RadioGroup radioGrp;

private TextView statusTV;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// on below line we are initializing our variables.

radioGrp = findViewById(R.id.idRadioGroup);

statusTV = findViewById(R.id.idTVStatus);
radioGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// Get the selected Radio Button

RadioButton radioButton = group.findViewById(checkedId);

// on below line we are setting text

// for our status text view.

statusTV.setText(radioButton.getText());

});

Layout file

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android "
xmlns:tools="http://schemas.android.com/tools "
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--on below line we are creating
a text for our app-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idTVStatus"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Radio Button in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<!--on below line we are creating a text view-->


<TextView
android:id="@+id/idTVStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idRadioGroup"
android:layout_centerInParent="true"
android:layout_margin="20dp"
android:gravity="center"
android:padding="10dp"
android:text="Status"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<!--on below line we are creating a radio group-->


<RadioGroup
android:id="@+id/idRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">

<!--on below line we are creating a radio buttons-->


<RadioButton
android:id="@+id/idBtnJavaRadio"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Java"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp" />

<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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy