Not Complete
|
ListView is a view group that displays a list of scrollable items. The
list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database
query and converts each item result into a view that's placed into the list.
divider
Drawable or color to draw between list
items.
May be a color value, in the form of "#rgb
", "#argb
", "#rrggbb
", or "#aarrggbb
".
code - android:divider="#888888"
dividerHeight
Height of the divider. Will use the intrinsic height of
the divider if this is not specified.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).code - android:dividerHeight="14sp"
Text
Alignment
Set Text Left, Right, Centre and more in EditText
code - android:textAlignment="center"
Background
Set the background color as a image, color.
code - android:background="@drawable/abc"
Text
Direction
Set the direction of the text
rtl for Right to Left direction.
code - android:textDirection="rtl"
Visibility
show or hide the edittext
Must be 'Visible', 'Invisivble' or 'Gone'.
code - android:visibility="invisible"
Margin
Specifies extra space on the bottom side of this view.
This space is outside this view's bounds. Margin values should be positive.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).Code - android:layout_margin="46dp"
XML Attributes
|
|||||||||||
Attribute Name
|
Related Method
|
Description
|
|||||||||
Specifies extra
space on the bottom side of this view.
|
|||||||||||
Specifies extra
space on the end side of this view.
|
|||||||||||
Specifies extra
space on the left side of this view.
|
|||||||||||
Specifies extra
space on the right side of this view.
|
|||||||||||
Specifies extra
space on the start side of this view.
|
|||||||||||
Specifies extra
space on the top side of this view.
|
Padding
Sets the padding, in pixels, of all four edges. Padding
is defined as space between the edges of the view and the view's content. A
views size will include it's padding. If a background is provided, the
padding will initially be set to that (0 if the drawable does not have
padding). Explicitly setting a padding value will override the corresponding
padding found in the background.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).code - android:padding="16dp"
XML Attributes
|
|||||||||||
Attribute Name
|
Related Method
|
Description
|
|||||||||
onClick
Name of the method in this View's context to invoke when
the view is clicked. This name must correspond to a public method that takes
exactly one parameter of type View. For instance, if you specify
android:onClick="sayHello"
, you must declare a public void
sayHello(View v)
method
of your context (typically, your Activity).
Must be a string value, using '\\;' to escape characters
such as '\\n' or '\\uxxxx' for a unicode character.
code - android:onClick="method name"
rotation
rotation of the view, in degrees.
Must be a floating point value, such as "
1.2
".code - android:rotation="12"
Attribute - Description
android:rotationX - rotation
of the view around the x axis, in degrees.
android:rotationY - rotation
of the view around the y axis, in degrees.
Save
Enabled
If false, no state will be saved for this view when it is
being frozen. The default is true, allowing the view to be saved (however it
also must have an ID assigned to it for its state to be saved). Setting this to
false only disables the state for this view, not for its children which may
still be saved.
Must be a boolean value, either "
true
" or "false
".code - android:saveEnabled="false"
Sound
Effect Enabled
Boolean that controls whether a view should have sound
effects enabled for events such as clicking and touching.
Must be a boolean value, either "
true
" or "false
".code - android:soundEffectsEnabled="true"
Tag
Supply a tag for this view containing a String, to be
retrieved later with View.getTag() or searched for with View.findViewWithTag(). It is generally
preferable to use IDs (through the android:id attribute) instead of tags
because they are faster and allow for compile-time type checking.
Must be a string value, using '\\;' to escape characters
such as '\\n' or '\\uxxxx' for a unicode character.
code - android:tag="tag name here"
ForeGround
Defines the drawable to draw over the
content. This can be used as an overlay. The foreground drawable participates
in the padding of the content if the gravity is set to fill.
May be a color value, in the form of
"
#rgb
", "#argb
", "#rrggbb
", or "#aarrggbb
" or imagecode - android:foreground="@drawable/abc"
foreGround Gravity
Defines the gravity to apply to the foreground drawable.
The gravity defaults to fill.
Must be one or more (separated by '|') of the following
constant values.
Constant
|
Value
|
Description
|
top
|
0x30
|
Push object to the top of its container, not changing its size.
|
bottom
|
0x50
|
Push object to the bottom of its container, not changing
its size.
|
left
|
0x03
|
Push object to the left of its container, not changing its
size.
|
right
|
0x05
|
Push object to the right of its container, not changing
its size.
|
center_vertical
|
0x10
|
Place object in the vertical center of its container, not
changing its size.
|
fill_vertical
|
0x70
|
Grow the vertical size of the object if needed so it
completely fills its container.
|
center_horizontal
|
0x01
|
Place object in the horizontal center of its container,
not changing its size.
|
fill_horizontal
|
0x07
|
Grow the horizontal size of the object if needed so it
completely fills its container.
|
center
|
0x11
|
Place the object in the center of its container in both
the vertical and horizontal axis, not changing its size.
|
fill
|
0x77
|
Grow the horizontal and vertical size of the object if
needed so it completely fills its container.
|
clip_vertical
|
0x80
|
Additional option that can be set to have the top and/or
bottom edges of the child clipped to its container's bounds. The clip will be
based on the vertical gravity: a top gravity will clip the bottom edge, a
bottom gravity will clip the top edge, and neither will clip both edges.
|
clip_horizontal
|
0x08
|
Additional option that can be set to have the left and/or right
edges of the child clipped to its container's bounds. The clip will be based
on the horizontal gravity: a left gravity will clip the right edge, a right
gravity will clip the left edge, and neither will clip both edges.
|
foregroundTint
Tint to apply to the foreground. if your foreground is
image then overwrite with tint color.
Must be a color value, in the form of "
#rgb
", "#argb
", "#rrggbb
", or "#aarrggbb
".android:foregroundTint="#12e7ff"
foregroundTintMode
Blending mode used to apply the foreground
tint. awsm
Must be one of the following
constant values.
Constant
|
Value
|
Description
|
src_over
|
3
|
The tint is drawn on top of the drawable. [Sa + (1 -
Sa)*Da, Rc = Sc + (1 - Sa)*Dc]
|
src_in
|
5
|
The tint is masked by the alpha channel of the drawable.
The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
|
src_atop
|
9
|
The tint is drawn above the drawable, but with the
drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
|
multiply
|
14
|
Multiplies the color and alpha channels of the drawable
with those of the tint. [Sa * Da, Sc * Dc]
|
screen
|
15
|
[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
|
add
|
16
|
Combines the tint and drawable color and alpha channels,
clamping the result to valid color values. Saturate(S + D)
|
code - android:foregroundTintMode="add"
//-------------------------------
Scroll bar start here
ScrollIndicators
Defines which scroll indicators
should be displayed when the view can be scrolled. Multiple values may be
combined using logical OR, for example "top|bottom".
Must be one or more (separated by
'|') of the following constant values.
Constant
|
Value
|
Description
|
none
|
0x00
|
No scroll indicators are displayed.
|
top
|
0x01
|
Displays top scroll indicator when view can be scrolled
up.
|
bottom
|
0x02
|
Displays bottom scroll indicator when vew can be scrolled
down.
|
left
|
0x04
|
Displays left scroll indicator when vew can be scrolled
left.
|
right
|
0x08
|
Displays right scroll indicator when vew can be scrolled
right.
|
start
|
0x10
|
Displays right scroll indicator when vew can be scrolled
in the start direction.
|
end
|
0x20
|
Displays right scroll indicator when vew can be scrolled
in the end direction.
|
code - android:scrollIndicators="bottom"
ScrollX
The initial horizontal scroll offset, in pixels.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).code - android:scrollX="14sp"
ScrollY
The initial vertical scroll offset, in pixels.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).
code - android:scrollY="14sp"
scrollbarAlwaysDrawHorizontalTrack
Defines whether the horizontal scrollbar track should
always be drawn.
Must be a boolean value, either "
true
" or "false
".android:scrollbarAlwaysDrawHorizontalTrack="true"
Below code for vertical android:scrollbarAlwaysDrawVerticalTrack="true"
scrollbarDefaultDelayBeforeFade
Defines the delay in milliseconds that a scrollbar waits
before fade out.
Must be an integer value, such as "
100
".code - android:scrollbarDefaultDelayBeforeFade="100"
scrollbarFadeDuration
Defines the delay in milliseconds that a scrollbar takes
to fade out.
Must be an integer value, such as "
100
".code - android:scrollbarFadeDuration="100"
scrollbarSize
Sets the width of vertical scrollbars and height of
horizontal scrollbars.
Must be a dimension value, which is a floating point
number appended with a unit such as "
14.5sp
". Available
units are: px (pixels), dp (density-independent pixels), sp (scaled pixels
based on preferred font size), in (inches), mm (millimeters).code - android:scrollbarSize="100dp"
scrollbarStyle
Controls the scrollbar style and
position. The scrollbars can be overlaid or inset. When inset, they add to the
padding of the view. And the scrollbars can be drawn inside the padding area or
on the edge of the view. For example, if a view has a background drawable and
you want to draw the scrollbars inside the padding specified by the drawable,
you can use insideOverlay or insideInset. If you want them to appear at the
edge of the view, ignoring the padding, then you can use outsideOverlay or
outsideInset.
Must be one of the following
constant values.
Constant
|
Value
|
Description
|
insideOverlay
|
0x0
|
Inside the padding and overlaid
|
insideInset
|
0x01000000
|
Inside the padding and inset
|
outsideOverlay
|
0x02000000
|
Edge of the view and overlaid
|
outsideInset
|
0x03000000
|
Edge of the view and inset
|
scrollbarThumbHorizontal
Defines the horizontal scrollbar thumb
drawable.
android:scrollbarThumbHorizontal="@drawable/abc"
scrollbarThumbVertical
Defines the vertical scrollbar thumb
drawable.
android:scrollbarThumbVertical="@drawable/abc"
scrollbarTrackHorizontal
Defines the horizontal scrollbar track drawable.
code - android:scrollbarTrackHorizontal="@drawable/abc"
scrollbarTrackVertical
Defines the vertical scrollbar track
drawable.
code - android:scrollbarTrackVertical="@drawable/abc"
scrollbars
Defines which scrollbars should be
displayed on scrolling or not.
Must be one or more (separated by
'|') of the following constant values.
Constant
|
Value
|
Description
|
none
|
0x00000000
|
No scrollbar is displayed.
|
horizontal
|
0x00000100
|
Displays horizontal scrollbar only.
|
vertical
|
0x00000200
|
Displays vertical scrollbar only.
|
code - android:scrollbars="horizontal"
--------------------------------------
Java Code
ArrayAdapter
It is use to store array of
data.
ArrayAdapter arrayAdapter=new ArrayAdapter(context,android.Choose
List,array);
An adapter is something that knows about the
list items and how to represent or draw each list item on the screen.
Above syntax makes use of ArrayAdapter and its constructor
takes 3 parameters having information about the list items
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
First argument is Context to access system
services and resources ( you need layout inflater to create list item view )
Second argument defines the layout of the list that defines how the list item
appears in listview. Here layout android.R.layout.simple_list_item_1 which is defined by framework is used.
Third
argument is the information
about the list item, typically this information is used to create view for the
list item.
Finally created Adapter is given to the ListView
myListView.setAdapter(aa);
Now ListView calls the functions of Adapter to get the views of list item and populates in
the container.
If the list items are
changed ( here todo list ) Adapter can let the ListView know about it by
calling notifyDataSetChanged.
aa.notifyDataSetChanged();
listView.setAdapter(arrayAdapter);
Program
ListView
with ArrayAdapter
MainActivity.java
public class MainActivity extends AppCompatActivity { ListView listView; String[] ar={"India","Japan","Tokyo"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.list1); ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,ar); listView.setAdapter(arrayAdapter); } }
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="sichlearning.example.com.testone.MainActivity"> <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" > </ListView> </LinearLayout>
ListView
with ArrayAdapter
MainActivity.java
public class MainActivity extends AppCompatActivity { ListView listView; String[] ar={"India","America","China"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.list1); ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,ar); listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(),"List Position :"+position,Toast.LENGTH_SHORT).show(); } }); } }
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="sichlearning.example.com.testone.MainActivity"> <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#12ff66" > </ListView> </LinearLayout>
ListView
with text and image
MainActivity.java
public class MainActivity extends AppCompatActivity { ListView listView; String[] web = { "Google Plus", "Twitter", "Windows", "Bing", } ; Integer[] imageId = { R.drawable.abc, R.drawable.b_default, R.drawable.b_focus, R.drawable.b_press, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.list1); CustomList adapter = new CustomList(MainActivity.this, web, imageId); listView=(ListView)findViewById(R.id.list1); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); } }); } }
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="sichlearning.example.com.testone.MainActivity"> <ListView android:id="@+id/list1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAlignment="center" > </ListView> </LinearLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String> { private final Activity context; private final String[] web; private final Integer[] imageId; public CustomList(Activity context, String[] web, Integer[] imageId) { super(context, R.layout.list_single, web); this.context = context; this.web = web; this.imageId = imageId; } @Override public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView= inflater.inflate(R.layout.list_single, null, true); TextView txtTitle = (TextView) rowView.findViewById(R.id.txt); ImageView imageView = (ImageView) rowView.findViewById(R.id.img); txtTitle.setText(web[position]); imageView.setImageResource(imageId[position]); return rowView; } }
list_single.xml
<?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="horizontal" > <ImageView android:id="@+id/img" android:layout_gravity="center" android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/abc" /> <TextView android:id="@+id/txt" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="TextView" /> </LinearLayout>
ListView
with text and image
No comments:
Post a Comment