I want to display a TableLayout within a ScrollView. The Table has 9 TableRows but the ScrollView starts displaying parts of the 4th row and I can't sroll up to see the rows above.
The XML-Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/uyi_choosepi_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:keepScreenOn="true" >
<MyGallery
android:id="#+id/uyi_gallery_originals"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<TableLayout
android:id="#+id/uyi_choosepi_buttontable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="0,1" >
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hilfe"
android:onClick="showHelp"
android:drawableLeft="#android:drawable/ic_menu_help" />
<Button
android:id="#+id/uyi_choosepi_savepw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fertig"
android:clickable="false"
android:enabled="false"
android:onClick="savePassword"
android:drawableLeft="#android:drawable/ic_menu_save"/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/uyi_choosepi_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/uyi_gallery_originals"
android:layout_above="#id/uyi_choosepi_buttontable">
<TableLayout
android:id="#+id/uyi_choosepi_tablelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical"
android:shrinkColumns="0,1,2" />
</ScrollView>
</RelativeLayout>
The TableRows in the uyi_choosepi_tablelayout where added programmatically as follows:
table = (TableLayout) findViewById(R.id.uyi_choosepi_tablelayout);
for (int i = 0; i < UYIMainActivity.PASS_LENGTH; i++) {
TableRow row = new TableRow(table.getContext());
originalViews[i] = new ImageView(row.getContext());
originalViews[i].setAdjustViewBounds(true);
originalViews[i].setScaleType(ScaleType.FIT_XY);
originalViews[i].setPadding(3, 3, 3, 3);
originalViews[i].setImageResource(R.drawable.oempty);
distortedViews[i] = new ImageView(row.getContext());
distortedViews[i].setAdjustViewBounds(true);
distortedViews[i].setScaleType(ScaleType.FIT_XY);
distortedViews[i].setPadding(3, 3, 3, 3);
distortedViews[i].setImageResource(R.drawable.wempty);
ImageView arrow = new ImageView(row.getContext());
arrow.setAdjustViewBounds(true);
arrow.setScaleType(ScaleType.FIT_XY);
arrow.setMaxWidth(80);
arrow.setPadding(3, 3, 3, 3);
arrow.setImageResource(R.drawable.arrow_active);
row.addView(originalViews[i]);
row.addView(arrow);
row.addView(distortedViews[i]);
table.addView(row);
}
I hope you can give me a hint!
Try using a GridView instead that may get you further towards your goal. I saw [this blog post][1] recently, and I would have approached it more along these lines.
Edit: I meant to paste this tutorial sorry, http://android-coding.blogspot.com/2011/09/custom-gridview-ii-with-imageview-and.html
I solved my problem by surrounding the TableLayout with a LinearLayout
<ScrollView
android:id="#+id/uyi_choosepi_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/uyi_gallery_originals"
android:layout_above="#id/uyi_choosepi_buttontable">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/uyi_choosepi_tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:shrinkColumns="0,1,2" />
</LinearLayout>
</ScrollView>
Related
I'm trying to set programmatically a ToggleButton inside of a Gridview. All seems that is working, but I can't set some properties like the margins between the togglebuttons that I'm adding in a loop for and this property: toogleBtn.setAllCaps(false); don't work.
Thanks in advance
This is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/screen"
android:id="#+id/ly_general"
android:fitsSystemWindows="true"
android:background="#color/colorPrimary">
<TextView
android:text="#string/your_interest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="#dimen/text_size_22"
android:id="#+id/text_init_session"
android:layout_marginTop="#dimen/margin_26"
android:layout_marginLeft="#dimen/padding_20"
android:layout_below="#+id/image"/>
<TextView
android:text="#string/select_categories"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_init_session"
android:layout_alignLeft="#+id/text_init_session"
android:layout_alignStart="#+id/text_init_session"
android:layout_marginTop="22dp"
android:id="#+id/textViewExplanation" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
style="#style/screen"
android:id="#+id/scroll"
android:layout_below="#+id/textViewExplanation">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lyCategory"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:columnCount="3"
android:layout_marginTop="#dimen/padding_40">
</GridLayout>
</ScrollView>
</RelativeLayout>
This is my code in java class:
for (int i = 0; i < arrayList.size(); i++) {
final ToggleButton toogleBtn = new ToggleButton (RegisterThreeActivityNew.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 20, 20, 20);
toogleBtn.setLayoutParams(params);
toogleBtn.setWidth(140);
toogleBtn.setHeight(20);
toogleBtn.setTextSize(12);
toogleBtn.setAllCaps(false);
toogleBtn.setText(arrayList.get(i).getName());
toogleBtn.setTextOff(arrayList.get(i).getName());
toogleBtn.setTextOn(arrayList.get(i).getName());
toogleBtn.setTextColor(ResourcesCompat.getColor(getResources(), R.color.white, null));
toogleBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_category, null));
layoutCategories.addView(toogleBtn);
}
In your xml file inside toggle button set these properties
android:minWidth="10dp" // use what ever minimum value you need
android:minHeight="10dp"
if you dont decrease the minwidth in xml then how much ever you try to decrease its width & height it won't work.I suffered with this problem for 2 days
I'm having a few issues getting the Two Way Gridview library (originally by Jess Andres https://github.com/jess-anders/two-way-gridview ) to work in a LinearLayout.
Jess' example code just has the gridview in the layout xml by itself, however I need to add additional elements around the gridview hence have put the element and the gridview inside a LinearLayout.
The issue is that I'm getting the following error...
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to two_way_gridview_gradle.TwoWayAbsListView$LayoutParams
at two_way_gridview_gradle.TwoWayGridView$HorizontalGridBuilder.onMeasure(TwoWayGridView.java:2872)
at two_way_gridview_gradle.TwoWayGridView.onMeasure(TwoWayGridView.java:286)
I understand that the issue is that the inflator is attempting to assign parameters from TwoWayAbsList to the LinearLayout parameters, but I haven't a clue as to how to force it to use only the TwoWayAbsListView parameters
Here's my layout file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/caller_text"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:lines="2"
android:textSize="17sp"
android:text="First text block"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
<two_way_gridview_gradle.TwoWayGridView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E8E8E8"
app:cacheColorHint="#E8E8E8"
app:columnWidth="80dp"
app:rowHeight="80dp"
app:gravity="center"
app:horizontalSpacing="16dp"
app:verticalSpacing="16dp"
app:numColumns="auto_fit"
app:numRows="auto_fit"
app:scrollDirectionLandscape="horizontal"
app:scrollDirectionPortrait="horizontal"
app:stretchMode="spacingWidthUniform"
/>
<TextView
android:id="#+id/cancel_text"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:lines="2"
android:textSize="17sp"
android:text="Second text block"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
</LinearLayout>
And here's how I'm inflating the view in the activity onCreate() method....
setContentView(R.layout.activity_meetingcaller);
TwoWayGridView gridView = (TwoWayGridView) findViewById(R.id.gridview);
// Instance of ImageAdapter Class
gridView.setAdapter(new GridImageAdapter(this));
I have been messing around to implement Two-way gridview, but finally implemented it my way:
<ScrollView
android:id="#+id/mainscrolView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<HorizontalScrollView
android:id="#+id/rclv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
I add the children of TableLayout programmatically. Like:
TableLayout tv = findViewById(R.id.table);
tv.removeAllViewsInLayout();
TableRow tr = new TableRow(AndroidDatabaseManager.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView b3 = new TextView(AndroidDatabaseManager.this);
b3.setText("Child");
tr.addView(b3);
tv.addView(tr);
final View vline = new View(AndroidDatabaseManager.this);
vline.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
If you want to add border to cells. There's a little workaround for it.
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)
I have a LinearLayout with a TextView and TableLayout embedded in it. here is the format of my xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txtHeader"
android:layout_width="match_parent"
android:text="Header" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
</LinearLayout>
I am adding rows dynamically to the table. Everytime, I get an update, I need to clear the table and write new information (number of rows are variables). So, I am trying to do clear the table as a first step whenever I have to update.
ll = (LinearLayout)findViewById(R.id.wlayout);
tb = (TableLayout)findViewById(R.id.wtable);
private void on Update(){
tb.removeAllViewsInLayout();
Create table dynamically..
}
But it is removing my text header also. I have also tried ll.removeView(tb) where ll is the LinearLayout. Can any one please help me. I am new to Android. Thank you.
You have not given id to TableLayout tag.Please give id and then try to clear it.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+#id/wtable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Column 1 -->
<TextView
android:id="#+id/row1Item1"
android:text="Column1" />
<!-- Column 2 -->
<TextView
android:id="#+id/row1Item2"
android:text="Column2" />
</TableRow>
</TableLayout>
And then try this..
tb = (TableLayout)findViewById(R.id.wtable);
tb.removeAllViewsInLayout();
I have a layout where I put i rows dynamically in a tablelayout that is inside a scrollview. Everything is working smoothly except that I don't can get the checkbox text to wordwrap (See xml below). The text is also set at runtime. I have searched the internet and has made a lot of trying to set different properties with no luck so far.
Is this task possible to achieve?
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
CheckBox extends TextView, so I assume you can wrap its text like they solve the issue in this post :
Android TextView Text not getting wrapped
If you cannot get it to work like that, you can maybe also try, using match_parent in the checkbox.
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
hope it helps.
use table row inside the Linear layout instead of table layout..
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/bes_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/bes_kommentar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bes_checkbox" />
</RelativeLayout>
</TableRow>
</LinearLayout>
I have made dynamic layouts with checkboxes in the past and used this approach:
// Create new view to add layout to
sv = new ScrollView(this);
// Create new layout to add check boxes to
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add layout to view
sv.addView(ll);
// Create 6 check boxes
int itemSize = 6;
for(int j=0; j<itemSize; j++){
CheckBox cb = new CheckBox(this);
cb.setText("number " + j);
ll.addView(cb);
}
// Finalize view
this.setContentView(sv);
Is this what you are looking for?