MAD 7 To 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Practical No : 13

1. Write a program to show following output.

Activity_main.xml :

<?xml version="1.0" encoding="utf-8"?> public class MainActivity extends


<RelativeLayout AppCompatActivity {
xmlns:android="http://schemas private ProgressBar pgsBar;
.android.com/apk/res/android" private int i = 0;
android:layout_width="match_parent" private TextView txtView;
android:layout_height="match_parent"> private Handler hdlr = new Handler();
<ProgressBar @Override
android:id="@+id/pBar" protected void onCreate(Bundle
savedInstanceState) {
style="?android:attr/progressBarStyleHorizontal"
super.onCreate(savedInstanceState);
android:layout_width="wrap_content" setContentView(R.layout.activity_main);
android:layout_height="wrap_content" pgsBar = (ProgressBar)
android:layout_marginLeft="100dp" findViewById(R.id.pBar);
android:layout_marginTop="200dp" txtView = (TextView)
android:minHeight="50dp" findViewById(R.id.tView);
android:minWidth="200dp" Button btn =
android:max="100" (Button)findViewById(R.id.btnShow);
android:indeterminate="false" btn.setOnClickListener(new
android:progress="0" /> View.OnClickListener() {
<TextView @Override
android:id="@+id/tView" public void onClick(View v) {
android:layout_width="wrap_content" i = pgsBar.getProgress();
android:layout_height="wrap_content" new Thread(new Runnable() {
android:layout_alignLeft="@+id/pBar" public void run() {
android:layout_below="@+id/pBar" /> while (i < 100) {
<Button i += 1;
android:id="@+id/btnShow" hdlr.post(new Runnable() {
android:layout_width="wrap_content" public void run() {
android:layout_height="wrap_content" pgsBar.setProgress(i);
android:layout_marginLeft="130dp" txtView.setText(i+"/"+pgsBar.getMax());
android:layout_marginTop="20dp" try {
android:text="DOWNLOAD FILE" to show the progress slowly.
android:layout_below="@+id/tView"/> Thread.sleep(100);
</RelativeLayout> } catch (InterruptedException
e.printStackTrace();
• MainActivity.java : } }} }).start(); }}0;}}
package com.example.progressbar;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;
2. Write a program to display circular progress
bar.
• Activity_main.xml :
Only change in the XML file . remove the
style of progress bar to get circular progress
bar

<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 :

<?xml version="1.0" encoding="utf-8"?> items.add("Ajax"); items.add("C++");


<RelativeLayout items.add("Ruby"); items.add("Rails");
xmlns:android="http://schemas.android.com/apk/res/
items.add("React"); items.add("Javascript");
android"xmlns:tools="http://schemas.android.com/to
ols" items.add("c#"); items.add("advanced java");
android:layout_width="match_parent" items.add("angular JS"); items.add("html");
android:layout_height="match_parent" ArrayAdapter<String> adapter = new
tools:context=".MainActivity"> ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
<ListView
ListView listView = findViewById(R.id.listView);
android:id="@+id/listView"
listView.setAdapter(adapter);
android:layout_width="match_parent"
listView.setOnItemClickListener(new
android:layout_height="match_parent" /> AdapterView.OnItemClickListener() {
</RelativeLayout> @Override
• MainActivity.java : public void onItemClick(AdapterView<?>
parent, View view, int position, long id) {
package com.example.listandtoast;
String selectedItem =
import android.os.Bundle; parent.getItemAtPosition(position).toString();
import android.view.View; Toast.makeText(MainActivity.this,
import android.widget.AdapterView; selectedItem, Toast.LENGTH_SHORT).show(); }}); } }

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 :

<?xml version="1.0" encoding="utf-8"?> super.onCreate(savedInstanceState);

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

<GridView GridView gridView = findViewById(R.id.gridView);

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();

android:gravity="center"/> Toast.makeText(MainActivity.this, buttonText,


Toast.LENGTH_SHORT).show(); } });
</RelativeLayout>

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

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle


savedInstanceState) {
4. Write a program to display a text view using vertical scroll view.
• Activity_main.xml :

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

<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"

android:text="Lorem Ipsum is simply dummy

text of the printing and typesetting industry.\n

Lorem Ipsum has been the industry's standard

dummy text ever since the 1500s, when an

unknown printer took a galley of type and

scrambled it to make a type specimen book.\n

It has survived not only five centuries,

but also the leap into electronic typesetting,

remaining essentially unchanged.\n

It was popularised in the 1960s with the

release of Letraset sheets containing

Lorem Ipsum passages,\n

and more recently with desktop publishing

software like Aldus PageMaker including

versions of Lorem Ipsum\n" />


Practical No : 15
1. Write a program to display above toast message.

• Activity_main.xml :

<?xml version="1.0" encoding="utf-8"?> super.onCreate(savedInstanceState);


<RelativeLayout setContentView(R.layout.activity_main);
xmlns:android="http://schemas.android.com/ Button button = findViewById(R.id.btn);
apk/res/android" button.setOnClickListener(new
xmlns:app="http://schemas.android.com/apk/ View.OnClickListener() {
res-auto" @Override
xmlns:tools="http://schemas.android.com/to public void onClick(View v) {
ols" Toast.makeText(MainActivity.this,
android:layout_width="match_parent" "Message for you : \n You have got Mail!",
android:layout_height="match_parent" Toast.LENGTH_SHORT).show();
tools:context=".MainActivity"> }
});
<TextView }
android:layout_width="wrap_content" }

android:layout_height="wrap_content"

android:text="Hello World! toast Example " />

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

public class MainActivity extends


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 :

<?xml version="1.0" encoding="utf-8"?> android:layout_height="wrap_content"

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

public class MainActivity extends AppCompatActivity { }

CheckBox pizza,coffe,burger; result.append("\nTotal: "+totalamount+"Rs");

Button buttonOrder; LayoutInflater inflater = getLayoutInflater();

@Override View layout =


inflater.inflate(R.layout.custom_toast, (ViewGroup)
protected void onCreate(Bundle
findViewById(R.id.custom_toast_layout));
savedInstanceState) {
TextView tv = (TextView)
super.onCreate(savedInstanceState);
layout.findViewById(R.id.txt);
setContentView(R.layout.activity_main);
tv.setText(result.toString());
addListenerOnButtonClick();

}
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

public void onClick(View view) { • Custom_toast.xml

int totalamount=0; <?xml version="1.0" encoding="utf-8"?>

StringBuilder result=new StringBuilder(); <LinearLayout


xmlns:android="http://schemas.android.com/apk/res/
result.append("Selected Items:");
android"
if(pizza.isChecked()){ android:id="@+id/custom_toast_layout"
result.append("\nPizza 100Rs"); android:orientation="horizontal"
totalamount+=100; android:layout_width="match_parent"
} android:layout_height="match_parent"
if(coffe.isChecked()){
android:paddingLeft="10dp"
result.append("\nCoffe 50Rs"); android:paddingRight="10dp"
totalamount+=50;
>
}

if(burger.isChecked()){ <TextView android:id="@+id/txt"


android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:textStyle="bold"

android:textSize="15dp" />

</LinearLayout>

You might also like