Android: Listview inside ScrollView - android

I want to have a layout that can scroll and a listview inside it.
The listview will expand it's height base on how many items in it. Only the ScrollView outside is scrollable.
This is my code:
<ScrollView
android:id="#+id/layout_box"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/layout_height_small"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/layout_margin_medium"
android:layout_marginTop="#dimen/layout_margin_medium"
android:gravity="center"
android:text="#string/list_regist_box_content"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<ListView
android:id="#+id/list_registed_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/btn_add_regist_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="3dp"
android:gravity="center"
android:paddingBottom="#dimen/layout_margin_medium"
android:paddingTop="#dimen/layout_margin_medium"
android:text="#string/add_regist_box"
android:textColor="#0F88FF"
android:textSize="#dimen/text_size_medium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/layout_margin_medium"
android:layout_marginTop="#dimen/layout_margin_large"
android:gravity="center"
android:text="#string/amount"
android:textSize="#dimen/text_size_medium"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/common_row_height"
android:background="#drawable/white_bg_grey_border_bottom"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/list_regist_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_margin_medium"
android:layout_marginRight="#dimen/layout_width_medium"
android:gravity="center_vertical"
android:text="#string/total"
android:textSize="#dimen/text_size_medium" />
</LinearLayout>
</ScrollView>
But the listview is neither expanded nor scrollable.
Please help!

Don't put ListView inside ScrollView - first rule of android clud :)
Instead you can use simple LinearLayout and manage you ListView items inside it.
Or you can add Header/Footer Views to the ListView and using it without scrollview.

Actually, it is possible to put a ListView inside of an ScrollView. In some use cases (e.g. dynamic menus/submenus it's a reasonable solution). However, two caveats apply:
The ListView won't have scroll. In general, nested scrolling is not possible in Android. There are some hacks to make it work (mostly by using requestDisallowInterceptTouchEvent()) but it's hard to make them work correctly in all cases.
As a consequence, you must indicate the exact height the ListView needs to show all items (via its appropriate LayoutParams). Setting WRAP_CONTENT will not work.

I used below lines of code to scroll list item inside scroll view. It's working fine for my requirement, i hope it will help you.
Thanks,
Murali.M
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
//pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

Related

Getting measured height in Android Studio doesn't work properly

I have multiple list views inside a scrollable view, so I used a function that computes the height of a list view and sets it so the list view is not scrollable anymore.
public static void setHeightListView(ListView listView) {
Adapter adapter = listView.getAdapter();
int UNBOUNDED = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = listView.getDividerHeight() * (listView.getCount() - 1);
for (int i = 0; i < adapter.getCount(); ++i) {
View child = adapter.getView(i, null, listView);
child.measure(UNBOUNDED, UNBOUNDED);
height += child.getMeasuredHeight();
}
ViewGroup.LayoutParams listViewParams = listView.getLayoutParams();
listViewParams.height = height;
listView.requestLayout();
}
This function has worked fine so far, but it doesn't compute the correct height for this in getView() in my adapter:
convertView = inflater.inflate(R.layout.activity_frame_no_rounded_corners, null);
activity_frame_no_rounded_corners.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="wrap_content"
android:orientation="horizontal"
android:paddingHorizontal="10dp"
android:focusable="false"
android:background="#drawable/textview_white">
<LinearLayout
android:layout_width="match_parent"
android:focusable="false"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageview_modify"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:focusable="false"
android:gravity="center_vertical"
android:src="#drawable/ic_baseline_remove_circle_24" />
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:focusable="false"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview_key"
android:text="Key"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:focusable="false"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:hint="Vlue"/>
<View
android:id="#+id/vertical_bar_devider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#dcdcdc"/>
<EditText
android:id="#+id/edittext_value"
android:text="Value"
android:textSize="15dp"
android:padding="7dp"
android:textColor="#000000"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:hint="Value"
android:background="#drawable/textview_white"
android:drawableRight="#drawable/ic_baseline_arrow_forward_ios_24_gray"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If I use a different xml then it computes the right height. Also, if I remove the weight from the views it also computes the right height (but I need the weight for alignment, I couldn't find a way around it).
You can see in the Screenshot the different list views that I have (one for the names, one for the dob, one for the grade, one for the email, one for the gmc no, one for the specialities).
For testing purposes, they do not have any margins, so they should be one right after the other.
As you can see, the height of the email list view is computed correctly, but my function fails for the rest of the lists. If I change "GMC Number" to "Email" then the function computes the right height for the gmc no list view as well (idk why, it's just an observation).
I guess my problem has something to do with the weight, but I have no idea how to fix it.
Thank you in advance!

android scroll view on whole activity having listview [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want a specific requirement for a android activity . Creating a forum activity with title, post, comments as list and a comment button .
The Requirements are :
Title on top
post below that
username who posted this post
comments as List view
comment button always shown at the bottom of the activity layout .
Everything defined above as a scroll view
The problem i am facing here is when height of title and post increases the listview and button goes out of the android view .When i tried to put scroll view on whole activity button appears just below the listview not always on bottom.
P.S I am beginner to android.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.trippals.android.SinglePostView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/clubtitle"
android:background="#drawable/abc_list_selector_background_transition_holo_ligh t"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/data"
android:layout_below="#+id/clubtitle"
android:layout_marginTop="15dp"
android:background="#drawable/abc_list_selector_background_transition_holo_light"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/user"
android:textStyle="bold"
android:layout_below="#+id/data"
android:layout_marginTop="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/user"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:weightSum="2"
>
<ListView
android:id="#+id/commentList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.8"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:divider="#android:color/transparent"
android:layout_marginTop="5dp"
android:dividerHeight="10.0sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:orientation="horizontal"
android:weightSum="1"
>
<EditText
android:id="#+id/commentbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="comment"
android:inputType="text"
android:layout_weight=".9"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/ic_play_dark"
android:id="#+id/addbutton"
android:layout_weight=".1"
/>
</LinearLayout>
</LinearLayout>
<ProgressBar
android:id="#+id/pbLoadingpost"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
/>
To use listview inside a scrollview, you need to give fixed height to the listview programmatically at run time i.e item count * item height,
becouse ScrollView need to know the height of every view inside it.
use this code to get height of a listview.
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
Have a look at this page:
http://blog.lovelyhq.com/setting-listview-height-depending-on-the-items/
As Ashim said, if you want to use ListView in ScrollView you have to set the height of the ListView depending on the number of items (you should not have a ListView that scrolls through items when it is child of a ScrollView which should handle the scrolling).
I have faced the same problem. But when I took udacity android design tutorial then I learned how to identify which layouts to be used according your requirement. There is a single video which teach how to identify layout . I promise it will help you lot.
My solution to your problem is :
You keep your buttons which you want to show always at the bottom of the activity out of Scroll view and keep the height of the scroll view to match parent with margin required for your buttons height.

android 2 list view in linear layout height wrap_content

I have two list in a LinearLayout, and I want them to share the space between them according to their content,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/page_vierge"
android:orientation="vertical"
android:padding="5dp">
<ListView
android:id="#+id/list_amis_fb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0.5dp"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center"
android:textSize="20sp"
android:text="Amis "/>
<ListView
android:id="#+id/list_amis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0.5dp"
android:padding="5dp" />
</LinearLayout>
look at the picture
1: if content of if the list 1 is small and list 2 is longer
2: if content of if the list 2 is small and list 1 is longer
3: 2 lists are long
in my code when list 1 is long , list 2 is invisible !!
help.
strong textyou should use ExpandableListview and hide listview group header and always expand group to get same effect that you want... see here
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Have 3 conditions-
1: if the list 1 is small and list 2 is longer
2: if the list 2 is small and list 1 is longer
3: 2 lists are long
play with layout_weight property of LinearLayout children(the ListViews).
you can set it by adding weight proprty, looke below code, i have added in that.you can change weight according to your requirment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/page_vierge"
android:orientation="vertical"
android:weightsum="10"
android:padding="5dp">
<ListView
android:id="#+id/list_amis_fb"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="4"
android:layout_marginTop="10dp"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0.5dp"
android:padding="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:weight="2"
android:layout_gravity="center"
android:textSize="20sp"
android:text="Amis "/>
<ListView
android:id="#+id/list_amis"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="4"
android:layout_marginTop="10dp"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0.5dp"
android:padding="5dp" />
</LinearLayout>
if you want 1st list long then you can set weight of 1st listview to "5",set textview weight to "1" and 2nd listview weight to "4". change weight according to your need.
When you have the adapters that fill the ListViews then you could simply compare the results of the getCount() function and set the layout_height attribute accordingly.
public void onCreate(...)
...
ListView list1 = (ListView)findViewById(R.id.list_amis_fb);
ListView list2 = (ListView)findViewById(R.id.list_amis);
ListAdapter list1_adapter = ... //However you get your data
ListAdapter list2_adapter = ...
int count_difference = list1_adapter.getCount() - list2_adapter.getCount();
if (count_difference < 0){
list1.setLayoutParams(LayoutParams.match_parent, 100);
list2.setLayoutParams(LayoutParams.match_parent, LayoutParams.fill_parent);
}
if (count_difference == 0)
{
...
}

android gridview row dividers / separators

Is there a way to show (horizontal) dividers between rows in a gridview?
I tried putting a small divider-image below every grid item, but this is not a solution, because it won't span the whole row when a row is not completely filled with items.
Is there a way to just add an image between every row? I can only find methods for changing the space between rows.
If you are using custom layout for grid items. Below code will work.
Step 1: Give background color to GridView
This is going to serve as a divider.
Give horizontalSpacing and verticalSpacing as 1dp
backgroundColor will be your divider color.
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e5e5e5"
android:horizontalSpacing="1dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" >
Step 2: Give background color to Custom Grid Item Layout
This is going to serve as a foreground color for GridItems.
In my case I kept it white (#fff)
<?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"
android:gravity="center"
android:background="#fff"
android:padding="15dp"
>
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_launcher_transparent" />
<TextView
android:id="#+id/lable"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textColor="#D0583B"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Result
Note:
If you do not want vertical separator, keep horizontalSpacing = 0dp
If you do not want horizontal separator, keep verticalSpacing = 0dp
I ended up creating a custom gridview, something like this:
https://stackoverflow.com/a/9757501/1310343
using a background image that is exactly as high as one item in my gridview, and has a devider at the bottom.
Works like a charm!
Just wanted to share how I implemented this using the link accepted by OP.
For my case I also needed to control the length of the separators, so I couldn't get around subclassing GridView.
public class HorizontalSeparatorGridView extends GridView {
// Additional methods
#Override
protected void dispatchDraw(Canvas canvas) {
final int count = getChildCount();
for(int i = 0; i < count; i++) {
View child = getChildAt(i);
int bottom = child.getBottom();
int left = child.getLeft();
int right = child.getRight();
Paint paint = new Paint();
paint.setColor(0xffececec);
paint.setStrokeWidth(Math.round(0.5 * density));
int offset = // Some offset
canvas.drawLine(left + offset, bottom, right - offset, bottom, paint);
}
super.dispatchDraw(canvas);
}
I subclassed dispatchDraw as opposed to onDraw just to be safe but I don't think it would matter in this case.
I suggest doing the following:
`
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1sp"
android:layout_marginLeft="7sp"
android:layout_marginRight="7sp"
android:layout_marginTop="7sp"
android:background="#android:color/transparent">
<TextView
android:id="#+id/lblDeposit"
android:layout_width="60sp"
android:layout_height="40sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="0sp"
android:background="#drawable/rounded_top_left_rectangle"
android:gravity="center"
android:paddingLeft="5sp"
android:scaleType="fitXY"
android:text="Deposit"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000">
</TextView>
<TextView
android:id="#+id/lblDepositvalue"
android:layout_width="50sp"
android:layout_height="40sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2sp"
android:layout_marginRight="13sp"
android:background="#drawable/rounded_top_right_rectangle"
android:gravity="center_vertical|center_horizontal"
android:scaleType="fitXY"
android:text="40000/-Rs"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000">
</TextView>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6sp"
android:layout_marginLeft="7sp"
android:layout_marginRight="7sp"
android:layout_marginTop="2sp"
android:background="#android:color/transparent">
<TextView
android:id="#+id/lblPoints"
android:layout_width="60sp"
android:layout_height="40sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="0sp"
android:background="#drawable/rounded_bottom_right_rectangle"
android:gravity="center"
android:paddingLeft="5sp"
android:scaleType="fitXY"
android:text="Points "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000">
</TextView>
<TextView
android:id="#+id/lblPointsValue"
android:layout_width="50sp"
android:layout_height="40sp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2sp"
android:layout_marginRight="13sp"
android:background="#drawable/rounded_bottom_left_rectangle"
android:gravity="center_vertical|center_horizontal"
android:scaleType="fitXY"
android:text="20"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000">
</TextView>
</TableRow>
</TableLayout>`

Create multirow button list dynamically

I want to create a multirow Button list. Something like this :
but I want to do it dynamically(in code). Is there a way to tell layout to do this automatically? Or i have to do this myself usingRelativeLayout.LayoutParams. I can do this by code but I should control so many things and I was wondering if there is another easier way to do this. For example tell layout to add elements in the next row when the current one is full!
You could also do this with LinearLayout and make all the buttons the same size using weight.
As for your question:
i can do this by code but i should control so many things and i was
wondering if there is another easier way to do this. for example tell
layout to add elements in the next row when the current one is full!
This is potentially possible if you measure the screen width and height and use the Functions in in the View class to figure out the specifics of that particular view and its children.
Alternative
But as mentioned in the comments, there are other views that you can use to solve your problem like GridView.
You can also use a table layout ,
Create first row of tabllayout in xml like this
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="#+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I created this xml for four three texts and on button in a single row
refer to the table in onCreate
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
add rows like
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// Here value is the number of rows you want in table
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
for this i took help from here
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
and
its xml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
after some more google search i finally found the best way to do this. it's so clean and simple, using Adapters and grids.
thanks for all the answers
here is a Tutorial: Creating a Custom Adapter for Gridview(ButtonAdapter)

Categories

Resources