Practical 14
IX. Practical related questions
   1. List all attributes of ImageView
       android:adjustViewBounds
       android:baseline
       android:baselineAlignBotton
       android:cropToPadding
       android:maxHeight
       android:maxWidth
       android:scaleType
   2. Write steps to add following string array to grid view
      String example[] = new String{“A”, “B”,”C”,”D”,”E”};
      ArrayAdapter a = new
      ArrayAdapter(this,R.layout.activity_grid_view,example);
      GridView g = (GridView) findViewById(R.id.gList);
      g.setAdapter(a);
   3. Describe android:strechMode attribute of Grid view in detail
      Defines how many columns to show. May be an integer value, such as
      “100” or auto_fit which means display as many columns as possible to
      fill the available space
X.Exercise
   1. Write a program to show the following output:
package com.example.myapplication14_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
   String prgms[] =
{"Android","Java","Php","Hadoop","Sap","Python","Ajax","C++","R
uby","Rails"};
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ArrayAdapter a = new
ArrayAdapter(this,R.layout.activity_listview,prgms);
      ListView l = (ListView) findViewById(R.id.lists);
      l.setAdapter(a);
   }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <ListView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/lists"/>
</LinearLayout>
/layout/activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="match_parent"
   android:layout_width="match_parent"
   android:padding="10dp"
   android:textSize="16dp"
   android:textStyle="bold">
</TextView>
2. Write a program to display an image using ImageView and a Button named
as “Change image”. Once you click on button another image should get
displayed
package com.example.myapplication14_2;
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 implements
View.OnClickListener {
  Button b1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1=(Button) findViewById(R.id.myButton);
    b1.setOnClickListener(this);
  }
  public void onClick(View v) {
    ImageView im = (ImageView) findViewById(R.id.myImage);
       im.setImageResource(R.drawable.img_green);
  }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">
<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/myImage"
  android:src="@drawable/img_red"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myButton"
    android:text="Change Image"/>
</LinearLayout>
3. write a program to display 15 buttons using GridView
package com.example.myapplication14_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
String list[] =
{"b1","b2","b3","b4","b5","b6","b7","b8","b9","b10","b11","b12","b
13","b14","b15"};
GridView v;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     v = (GridView) findViewById(R.id.myGrid);
     ArrayAdapter ad = new ArrayAdapter(this,R.layout.activity_gridview,list);
     v.setAdapter(ad);
   }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="match_parent"
   android:layout_width="match_parent"
   android:orientation="vertical">
   <GridView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/myGrid"
     android:numColumns="3"/>
</LinearLayout>
/layout/activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content">
</Button>
4. Write a program to display a text view using vertical scroll view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/sc_view">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="My Text"/>
  </ScrollView>
</LinearLayout>
package com.example.myapplication14_4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    }
}
Practical 15
IX. Practical related questions
   1. List all predefined constants to specify the positioning of the toast. Which
       method is used to change the position of a toast on the screen.
       setGravity(int gravity, int x, int y) values-Gravity.TOP,Gravity.LEFT
   2. List two constants of Toast
       LENGTH_LONG
       LENGTH_SHORT
X. Exercise
1. write a program to display the following toast message
package com.example.myapplication15_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
  Button b1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.b1);
    b1.setOnClickListener(this);
  }
  public void onClick(View v) {
    StringBuffer str = new StringBuffer("Message for you:\n");
    str.append("You have got mail");
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
  }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello world, Toast Example"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id = "@+id/b1"
    android:text="Show Toast"/>
</LinearLayout>
  2. Write a program to display three checkbox and one button named order as
     shown below. Once the user click on button it should toast different
     selected checkboxes along with items individual and total price
package com.example.myapplication15_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
  Button b1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button)findViewById(R.id.b1);
    b1.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    StringBuffer str = new StringBuffer("Selected items:\n");
    CheckBox p,c,b;
    int total = 0;
    p = (CheckBox) findViewById(R.id.pizza);
    c = (CheckBox) findViewById(R.id.coffee);
    b = (CheckBox) findViewById(R.id.burger);
    if(p.isChecked()) {
       str.append("Pizza : "+250);
       total=total+250;
    }
    if(c.isChecked()) {
       str.append("\nCoffee : "+100);
       total = total+100;
    }
    if(b.isChecked()) {
       str.append("\nBurger : "+150);
       total = total+150;
    }
    str.append("\nTotal : "+total);
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
  }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">
  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id = "@+id/pizza"
    android:text="Pizza"/>
  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/coffee"
    android:text="Coffee"/>
  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/burger"
    android:text="Burger"/>
  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id = "@+id/b1"
    android:text="Order"/>
</LinearLayout>
Practical 16
IX. Practical related questions
   1. Write an xml TimePicker tag
       <TimePicker android:layout_width=”wrap_content”
       android:layout_height=”wrap_content”
       android:id=”myTimePicker”
       android:timePickerMode=”spinner”/>
   2. List and explain all methods of TimePicker class
       void autofill(AutofillValue) - Automatically fills the content of this view
       with the value
       Integer getCurrentHour()returns the current hour
       Integer getCurrentMinute()-returns the current minute
       int getHour()-Returns the currently selected hour using 24-hour time.
       int getMinute()-Returns the currently selected minute
       boolean is24HourView()
       boolean isEnabled()-Returns the enabled status for this view
       void setCurrentHour(Integer)
       void setCurrentMinute(Integer)
       void setHour()-Sets the currently selected hour using 24-hour time
       void setIs24HourView(Boolean)- Sets whether this widget displays time
       in 24-hour mode or 12-hour mode with an AM/PM picker.
   3. List and explain any five methods of DatePicker class
  getDayOfMonth() - This method gets the selected day of month
  getMonth() - This method gets the selected month
   getYear() - This method gets the selected year
  updateDate(int year, int month, int dayOfMonth) - This method updates the
current date
   getFirstDayOfWeek()-This Method returns first day of the week
X. Exercise
1. write program to display the following output
package com.example.myapplication16_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="AM/PM"/>
  <TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myTimePicker1"
    android:timePickerMode="spinner"/>
</LinearLayout>
ii.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:text="24 Hours"/>
    <TimePicker
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/myTimePicker1"
      android:timePickerMode="spinner"/>
</LinearLayout>
package com.example.myapplication16_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
TimePicker t1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1 = (TimePicker) findViewById(R.id.myTimePicker1);
    t1.setIs24HourView(true);
    }
}
iii.
package com.example.myapplication16_1_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       TimePicker t1 = (TimePicker) findViewById(R.id.myTimePicker1);
       t.setIs24HourView(true);
  }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical">
  <TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myTimePicker1"
    android:timePickerMode="clock"/>
</LinearLayout>
2.Write program to display following output. Select date and time.
package com.example.myapplication16_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TimePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
  Button b1,b2;
  EditText d,t;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    d = (EditText) findViewById(R.id.selectDate);
    t = (EditText) findViewById(R.id.selectTime);
    b1 = (Button) findViewById(R.id.dateButton);
    b2 = (Button) findViewById(R.id.timeButton);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat dfmt = new SimpleDateFormat("dd-MM-yyyy");
    String str = dfmt.format(c.getTime());
    SimpleDateFormat dfmt1 = new SimpleDateFormat("hh:mm:ss");
    String str1 = dfmt1.format(c.getTime());
    switch(v.getId()) {
            case R.id.dateButton:
            d.setText(str);
            break;
            case R.id.timeButton:
            t.setText(str1);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  >
  <EditText
    android:id="@+id/selectDate"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="110dp"
     />
  <EditText
    android:id="@+id/selectTime"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/selectDate"
    />
    <Button
      android:id="@+id/dateButton"
      android:layout_width="120dp"
      android:layout_height="wrap_content"
      android:layout_marginTop="100dp"
      android:layout_toRightOf="@+id/selectDate"
      android:text="Select Date" />
    <Button
      android:layout_width="120dp"
      android:id="@+id/timeButton"
      android:layout_below="@+id/dateButton"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/selectTime"
    android:text="Select Time" />
</RelativeLayout>