MAD 7 To 10
MAD 7 To 10
MAD 7 To 10
Activity_main.xml :
<ProgressBar
android:id="@+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="200dp"
android:minHeight="50dp"
android:minWidth="200dp"
android:max="100"
android:indeterminate="false"
android:progress="0" />
Practical No : 14
1. Program for above output.
• Activity_main.xml :
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> items = new ArrayList<>();
items.add("Android"); items.add("Java");
items.add("Php"); items.add("Hadoop");
items.add("Sap"); items.add("Python");
2. Write a program to display an image view and a button named a “Change Image”. Once you clicked on
button another image should get displayed.
• Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?> iv.setImageResource(images[i]);
<RelativeLayout i++;
xmlns:android=http://schemas.android.com/a if(i==3)
pk/res/android i=0; } }) ; } }
xmlns:tools="http://schemas.android.com/to
ols"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:id="@+id/img1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img1"
android:layout_centerHorizontal="true"
android:text="Change Image"
android:id="@+id/button"/>
</RelativeLayout>
• MainActivity.java :
package com.example.changeimage;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends
AppCompatActivity { Button b1;
ImageView iv;
boolean flag;
int
images[]={R.drawable.lotus,R.drawable.rose,R
.drawable.sun};
int i=0;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.img1);
b1=(Button) findViewById(R.id.button);
flag=true;
b1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
3. Write a program to display 15 buttons using grid view.
• Activity_main.xml :
<RelativeLayout setContentView(R.layout.activity_main);
xmlns:android="http://schemas.android.com/apk/res/
List<String> buttonLabels = new ArrayList<>();
android"
for (int i = 1; i <= 15; i++) {
xmlns:tools="http://schemas.android.com/tools"
buttonLabels.add("Button " + i); }
android:layout_width="match_parent"
ArrayAdapter<String> adapter = new
android:layout_height="match_parent"
ArrayAdapter<>(this,
tools:context=".MainActivity"> android.R.layout.simple_list_item_1, buttonLabels);
android:id="@+id/gridView" gridView.setAdapter(adapter);
android:layout_width="match_parent" gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
android:layout_height="wrap_content"
@Override
android:numColumns="3"
public void onItemClick(AdapterView<?>
android:verticalSpacing="8dp"
parent, View view, int position, long id) {
android:horizontalSpacing="8dp"
String buttonText =
android:padding="16dp" parent.getItemAtPosition(position).toString();
• MainActivity.java :
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
@Override
<RelativeLayout </RelativeLayout>
xmlns:android="http://schemas.android.com/apk/res/
• MainActivity.java :
android"
package com.example.verticalscrollview;
xmlns:tools="http://schemas.android.com/tools"
import android.os.Bundle;
android:layout_width="match_parent"
import androidx.appcompat.app.AppCompatActivity;
android:layout_height="match_parent"
import android.widget.TextView;
tools:context=".MainActivity">
public class MainActivity extends AppCompatActivity {
<ScrollView
@Override
android:layout_width="match_parent"
protected void onCreate(Bundle
android:layout_height="match_parent">
savedInstanceState) {
<TextView
super.onCreate(savedInstanceState);
android:id="@+id/textView"
setContentView(R.layout.activity_main);
android:layout_width="match_parent"
TextView textView = findViewById(R.id.textView); }}
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="@android:color/black"
• Activity_main.xml :
android:layout_height="wrap_content"
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"/>
</RelativeLayout>
• MainActivity.java :
package com.example.mailtoast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle
savedInstanceState) {
2. Write a program to display 3 checkboxes and 1 button named “order” as shown below .
• Activity_main.xml :
<RelativeLayout android:layout_marginLeft="144dp"
xmlns:android="http://schemas.android.com/apk/res/
android:layout_marginTop="140dp"
android"
android:text="Burger"
xmlns:app="http://schemas.android.com/apk/res-
auto" app:layout_constraintStart_toStartOf="parent"
xmlns:tools="http://schemas.android.com/tools" app:layout_constraintTop_toBottomOf="@+id/checkB
ox2" />
android:layout_width="match_parent"
<Button
android:layout_height="match_parent"
android:id="@+id/button"
tools:context="MainActivity">
android:layout_width="wrap_content"
<CheckBox
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_marginLeft="144dp"
android:layout_width="wrap_content"
android:layout_marginTop="484dp"
android:layout_height="wrap_content"
android:text="Order"
android:layout_marginLeft="144dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="60dp"
app:layout_constraintTop_toBottomOf="@+id/checkB
android:text="Pizza"
ox3" />
app:layout_constraintStart_toStartOf="parent"
</RelativeLayout>
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
• MainActivity.java :
android:id="@+id/checkBox2"
package com.example.order;
android:layout_width="wrap_content"
android:layout_height="wrap_content"
import androidx.appcompat.app.AppCompatActivity;
android:layout_marginLeft="144dp"
import android.os.Bundle;
android:layout_marginTop="100dp"
import android.view.View;
android:text="Coffee"
import android.view.ViewGroup;
app:layout_constraintStart_toStartOf="parent"
import android.widget.Button;
app:layout_constraintTop_toBottomOf="@+id/checkB
import android.widget.CheckBox;
ox" />
import android.widget.TextView;
<CheckBox
import android.widget.Toast;
android:id="@+id/checkBox3"
import android.view.Gravity;
android:layout_width="wrap_content"
import android.view.LayoutInflater;
result.append("\nBurger 120Rs");
totalamount+=120;
}
Toast toast = new
public void addListenerOnButtonClick(){ Toast(getApplicationContext());
pizza=(CheckBox)findViewById(R.id.checkBox); toast.setGravity(Gravity.CENTER_VERTICAL,
100, 100);
coffe=(CheckBox)findViewById(R.id.checkBox2);
toast.setDuration(Toast.LENGTH_LONG);
burger=(CheckBox)findViewById(R.id.checkBox3);
toast.setView(layout);
buttonOrder=(Button)findViewById(R.id.button);
toast.show();
}
buttonOrder.setOnClickListener(new
View.OnClickListener(){ } );
@Override
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="15dp" />
</LinearLayout>