Unit-4 Using Selection Widgets and Debugging
Unit-4 Using Selection Widgets and Debugging
Unit-4
Using Selection widgets and Debugging
Using ListView:
ListView is used to display a list of vertically scrolling items, allowing users to select one or more
of them.
Ex: <ListView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:entries=“@array/fruits”
android:choiceMode=“singleChoice”
android:drawSelectionOnTop=“false”
android:transcriptMode=“normal”/>
Attribute Description
Entries Used to refer an array resource for displaying options in the ListView
choiceMode Used to define the number of items that are selectable from the ListView
1) none: Doesn’t allow selection of any option from the ListView
2) singleChoice: Allows selection of a single option from the ListView
3) multipleChoice: Allows selection of multiple options from the ListView
drawSelectorOnTop When set to “true” ,the selector is drawn over the selected item.
• Whether we are creating a ListView by extending the Activity class or the ListActivity class, the
ListView can be populated by one of the following two methods:
ListView is populated through string resources to display a list of items for the user to select from.
Open the string resource file /res/values/string.xml and add a string array structure called Fruits that list
various fruits we want to display via the ListView control.
<resources>
<string name=“app_name”>ListViewApp</string>
<string-array name=“fruits”>
<item>Apple</item>
<item>Mango</item>
<item>Banana</item> </string-array>
</resources>
activity_list_view_app.xml
<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:id=“@+id/frtlist”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:entries=“@array/fruits”/>
<TextView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/txtv”/>
</LinearLayout>
ListViewAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.ListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_app);
fruitsList.setOnItemClickListener(new OnItemClickListener()
@Override
});
Second they convert individual elements of data into specific Views to be displayed inside the
selection widget.
The ArrayAdapter is one of the adapters provided by Android that provides data sources to
selection widgets.
activity_list_view_app.xml
<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:id=“@+id/frtlist”
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
<TextView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id=“@+id/txtv”/>
</LinearLayout>
ListViewAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.ListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_app);
Dept of CSE, C.B.I.T,PDTR Page 5
Unit-4
ListView fruitlist=(ListView)findViewById(R.id.frtlist)
fruitlist.setAdapter(arrayAdpt);
fruitlist.setOnItemClickListener(new onItemClickListener()
@Override
});
The spinner is akin to drop-down list that display a list of items and allows the user to select the
desired item.
2) Through ArrayAdapter
<Spinner
android:id=@+id/sp
android:layout_width=“match_parent”
Android:layout_height=“match_parent”
android:prompt=“@string/choose_msg”
android:entries=“@array/fruits”/>
android:prompt:- attribute is used for displaying the text what need to display defaulty.
strings.xml
<resources>
<string name=“app_name”>SpinnerApp</string>
</resources>
arrays.xml:
<resources>
<string-array name=“fruits”>
<item>Apple</item>
<item>Banana</item>
<item>Orange</item>
<item>Grapes</item>
</string-array>
</resources>
activity_spinner_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<Spinner
android:id=“@+id/sp”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:prompt=“@string/choose_msg”
android:entries=“@array/fruits”/>
<TextView
android:id=“@+id/selectedopt”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</LinearLayout>
SpinnerAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_app);
Spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
Dept of CSE, C.B.I.T,PDTR Page 9
Unit-4
tv.setText(“ “);
});
2) Through ArrayAdapter:
activity_spinner_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<Spinner
android:id=“@+id/sp”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:prompt=“@string/choose_msg”/>
<TextView
android:id=“@+id/selectedopt”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</LinearLayout>
SpinnerAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_app);
Spin.setAdapter(arrayAdpt);
Spin.setOnItemSelectedListener(new OnItemSelectedListener()
tv.setText(“ “);
});
AutoCompleteTextView:
To implement the auto complete facility, we create an ArrayAdapter and set it to display items
or suggestions from data source.
Activity_auto_complete_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<AutoCompleteTextView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
Android:id=“@+id/actv”/>
</LinearLayout>
AutoCompleteAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_app);
AutoCompleteTextView prduct=(AutoCompleteTextView)findViewById(R.id.actv);
prduct.setThreshold(2);
prduct.setAdapter(arradpt);
setThreshold(): method is used to indicate the minimum number of characters that a user must enter
before the suggestions are displayed.
The GridView control is a ViewGroup used to display text and image data in the form of a
rectangular, scrollable grid.
To display data in the grid, we first define GridView control in the XML layout, and then bind the
data that we want to be displayed to it using the ArrayAdapter.
1) android:columnWidth
This specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.
2) android:gravity
Specifies the gravity within each cell. Possible values are top, bottom, left, right, center, center_vertical,
center_horizontal etc.
Defines the amount of spacing between items in the grid. This could be in px, dp, sp, in, or mm.
4) android:numColumns
Defines how many columns to show. May be an integer value, such as "100“. Or we can give the value as
auto_fit can gives the no of columns based on available space.
Ex: activity_grid_view_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:id=“@+id/tv”
android:text=“Select a fruit:”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<GridView
android:id=“@+id/grid”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:verticalSpacing=“2dip”
android:horizontalSpacing=“5dip”
android:numColumns=“auto_fit”
android:gravity=“center”/>
</LinearLayout>
GridViewAppActvity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.GridView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view_app);
GridView g=(GridView)findViewById(R.id.grid);
g.setAdapter(arrayAdpt);
g.setOnItemClickListener(this);
selectedop.setText(“ “);
To display content in GridView we use Adapters, which provide the content to display to the
controls.
For displaying through GridView first we need to add images to the res/drawable folder.
Assume we have added 4 images called one.jpeg,two.jpeg,three.jpeg,four.jpeg . Lets create
the GridIamgeApp.
activity_grid_image_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<TextView
android:id=“@+id/tv”
android:text=“Select an image:”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<GridView
android:id=“@+id/grid”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:verticalSpacing=“2dip”
android:horizontalSpacing=“5dip”
android:numColumns=“auto_fit”
android:columnWidth=“100dip”
android:gravity=“center”/>
</LinearLayout>
GridImageAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.view.ViewGroup;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_image_app);
TextView selectedop=(TextView)findViewById(R.id.tv);
GridView g=(GridView)findViewById(R.id.grid);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(this);
int p=position+1;
Public ImageAdapter(Context c)
Contxt=c;
return images.length;
return position;
return position;
iv.setImageResource(images[position]);
iv.setLayoutParams(new GridView.LayoutParams(100,120));
Dept of CSE, C.B.I.T,PDTR Page 19
Unit-4
return imageView;
The height and width of the image are set by setting the GridView.LayoutParams()
method.
ViewPager control can be used for displaying data which may be text , image etc., in the form of
pages with horizontal swiping behavior i.e., flipped from left to right.
The ViewPager needs a data adapter for define and loading the data for each page.
Used for creating and instantiating the page and adding it to the container
2)destroyItem(): used for removing the page from container of specified position.
3)isViewFromObject(): Determines whether the specified page is associated with specified with
specified key object.
4)getCount(): Defines the size of the paging range , that is the count of the number of the pages.
activity_view_pager_app.xml
<LinearLayout
Xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<TextView
android:id=“@+id/tv”
android:text=“Image Gallery”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<android.support.v4.ViewPager>
android:id=“@+id/viewpager”
android:layout_width=“match_parent”
android:layout_height=“100dip”/>
</LinearLayout>
ViewPagerAppActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.view.ViewPager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager_app);
TextView selectedop=(TextView)findViewById(R.id.tv);
ViewPager vpg=(ViewPager)findViewById(R.id.viewpager);
iv.setImageResource(images[position]);
((ViewPager) container).addView(view,0);
return view;
@Override
return imges.length;
@Override
Dept of CSE, C.B.I.T,PDTR Page 22
Unit-4
@Override
When we run DDMS, it automatically connects to the attached android device or any running
emulator.
c. Generates LogCat, which displays the log messages about the state of the application.
In the upper left pane of DDMS window, we see a Devices tab that display the list of devices
connected.
4) Network Statistics: Helps us in getting information regarding network usage of our application
Debugging Applications:
When the application switches to Debug perspective on the screen you will see options like
callback stack, console, code view, and variables.
1)Debug Pane: The debug pane displays debug session information in a tree hierarchy.
iv. Disconnect: Disconnects the debugger from the selected debug target.
2) Expressions Pane: This pane is used for compute expressions with different variables of the
application.
Note: The variables what are used in the expression must exists in the application.
3) Breakpoints Pane: The breakpoints pane displays all the inserted breakpoints in the application.
This pane helps in enabling , disabling, skipping and removing breakpoints, and also helps in
suspending the breakpoint on specific condition.
4) Variables Pane: The Variables Pane displays values of the variables associated with the stack frame
selected in the Debug pane.
Dialogs are used for display small information and also used for guide the users.
iii. CharacterPickerDialog: A dialog that enables you to select an accented character associated
with a regular character source.
iv. DatePickerDialog: A dialog that enables you to set and select a date with a DatePicker control.
vi. TimePickerDialog: A dialog that enables you set and select a time with a TimePicker Control.
Alert Diaolug:
3) setMessage(): Used for adding the message which you want to display on the dialog box.
Ex: activity_alert_dialog_app.xml
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<Button
android:id=“@+id/clck_btn”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</LinearLayout>
AlertDialogAppActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.app.AlertDialog;
import android.content.DialogInterface;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog_app);
Button b= (Button)this.FindViewById(R.id.clck_btn)
b.setOnClickListener(this);
@Override
ad.setTitle(“Alert Window”);
ad.setIcon(“R.drawable.one”);
ad.setMessage(“This is alert”);
{
Dept of CSE, C.B.I.T,PDTR Page 27
Unit-4
return;
});
ad.show();
DatePickerDialog:
Ex: activity_date_picker_app.xml
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<Button
android:id=“@+id/dt_btn”
android:text=“set Date”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<TextView
android:id=“@+id/dtvw”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
</LinearLayout>
Dept of CSE, C.B.I.T,PDTR Page 28
Unit-4
DatePickerAppActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.app.DateDialog;
import android.util.Calendar;
import android.view.View;
import androi.widget.DatePicker;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_picker_app);
TextView tv=(TextView)findViewById(R.id.dtvw);
yr=c.get(Calendar.YEAR);
mon=c.get(Calendar.MONTH);
dy=c.get(Calendar.DAY_OF_MONTH);
db.setOnClickListener(new OnClickListener()
new DatePickerDialog(DatePickerAppActivity.this,dateListener,yr,mon,dy).show();
});
yr=year;
mon=month;
dy=day;
}; }
TimePickerDialog:
TimePickerDialog allows us to set or select time through the built-in android TimePicker View.
Ex: activity_time_picker_app.xml
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<Button
android:id=“@+id/tm_btn”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<TextView
android:id=“@+id/tmvw”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/></LinearLayout>
TimePickerAppActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
Dept of CSE, C.B.I.T,PDTR Page 31
Unit-4
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.Calendar;
import android.util.Calendar;
import android.view.View;
import androi.widget.TimePicker;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_picker_app);
TextView tv=(TextView)findViewById(R.id.dtvw);
h=c.get(Calendar.HOUR_OF_DAY);
m=c.get(Calendar.MINUTE);
tb.setOnClickListener(new OnClickListener()
}
Dept of CSE, C.B.I.T,PDTR Page 32
Unit-4
});
h=hour;
m=min;
}; }
Activity_date_time_picker_app.xml
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<Button
android:id=“@+id/tm_btn”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<Button
android:id=“@+id/dt_btn”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/>
<TextView
android:id=“@+id/dtmvw”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”/></LinearLayout>
DateTimePickerAppActivity.java
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import java.util.Calendar;
import android.util.Calendar;
import android.view.View;
import androi.widget.TimePicker;
import androi.widget.DatePicker;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_time_picker_app);
TextView dtv=(TextView)findViewById(R.id.dtmvw);
h=c.get(Calendar.HOUR_OF_DAY);
m=c.get(Calendar.MINUTE);
yr=c.get(Calendar.YEAR);
mon=c.get(Calendar.MONTH);
dy=c.get(Calendar.DAY_OF_MONTH);
db.setOnClickListener(new OnClickListener()
new DatePickerDialog(DatePickerAppActivity.this,dateListener,yr,mon,dy).show();
});
tb.setOnClickListener(new OnClickListener()
{
Dept of CSE, C.B.I.T,PDTR Page 35
Unit-4
});
yr=year;
mon=month;
dy=day;
};
h=hour;
m=min;
};
Fragments:
A Fragment is a combination of an activity and a layout. It contains a set of views that makeup
an independent user interface.
Fragments can be dynamically adjust to various views like landscape , potrait and even can
based on target device.
A fragment is like a sub activity with its own lifecycle and view hierarchy.
The life cycle of a fragment includes several callback methods. Those are:
7) onPause(): called when the fragment is visible but does not have focus.
Dept of CSE, C.B.I.T,PDTR Page 37
Unit-4
11) onDetach(): called when the fragment is detached from the activity.
There are two ways for creating Fragments one is by using <fragment> tag in the layout file of
the application but this can be static.
For creating, adding and replacing fragments dynamically we use FragmentManager in activity
(java)file.
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=FragmentManager.beginTransaction();
FragmentTransaction ft=FragmentTransaction.beginTransaction();
ft.commit();
Here Fragment1Activity is user defined java class of the fragment, which is also used to load the
UI of the fragment from XML file.
fragment_container is the ID of the container that exists in the layout file where we want to
put our fragment
A ListFragment is a fragment that contains a built-in ListView that can be set to display items
from a specified data source so we can directly add a java class file that extends ListFragment
class. In this java class file, we write code to define the items to be displayed through the
ListView of the ListFragment.
Fragment1Activity.java
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
@Override
Super.onCreate(savedInstanceState);
setListAdapter(arradpt);
@Override
TextView tv=(TextView)getActivity.findViewById(R.id.tv);
activity_list_frag_app.xml
<LinearLayout
xmlns:android=http://schemas.android.com/apk/res/android
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:Orientation=“vertical”>
<fragment
android:name=“com.androidunleashed.listfragapp.Fragment1Activity”
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:id=“@+id/fragment1”/>
</LinearLayout>
ListFragAppActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_frag_app);
@Override
getMenuInflater.inflate(R.menu.activity_list_frag_app,menu);
return true;