Fail to Move a View to a TableRow - android

I am trying to inflate a layout resource and move some views in it to a table. I remove the views from their parents and then add them to the table. It works but what I really need is to add them to a TableRow in the table instead. However, when I tried to do that, nothing showed up.
To simplify the example, I just use views in an xml instead of ones generated in code. In the following xml, there are a TableLayout with one TableRow and an ImageButton:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/row_1"></TableRow>
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</RelativeLayout>
If I move the button to the table, it works:
Button button = (Button ) getActivity().findViewById(R.id.button);
TableLayout table = (TableLayout) getActivity().findViewById(R.id.table);
TableRow row = (TableRow) getActivity().findViewById(R.id.row_1);
((ViewGroup)button.getParent()).removeView(button);
table.addView(button);
However, if I move it to the TableRow, the button won't show up.
row.addView(button);
Does anyone have a solution?

Try to add the Button to a row like this:
row.addView(button,new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));

Related

Android - Adding a GridLayout inside a FrameLayout

I am trying to use the Library SwipeLayout (https://github.com/daimajia/AndroidSwipeLayout) to make a table of swipeable rows (that is, to achieve this effect). I want to create the table dynamically (with Java, not xml). The table (if it were to exist as xml) should look like this:
...TableLayout...
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<com.daimajia.swipe.SwipeLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- The first view in SwipeLayout is the delete button hidden behind -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:id="#+id/delete1"
android:gravity="center"
android:padding="16dp"
android:background="#FF0000"
android:textSize="22sp"
android:text="Delete"/>
<GridLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#drawable/bottom_border_light_solid"
android:columnCount="2"
android:padding="10dp">
...TextViews...
</GridLayout>
</com.daimajia.swipe.SwipeLayout>
</TableRow>
.../TableLayout...
My table creation code looks like this:
TableRow row = new TableRow(c);
SwipeLayout swipeLayout = new SwipeLayout(c);
TextView delete = generateDeleteButton(c); //I have confirmed that this function works
GridLayout cell = generateRoutineLayout(c); //I have confirmed that this function works
swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
swipeLayout.addDrag(SwipeLayout.DragEdge.Left, delete);
swipeLayout.addSwipeListener(swipeListener);
cell.setPadding(5, 5, 5, 5);
swipeLayout.addView(delete);
swipeLayout.addView(cell);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
swipeLayout.setLayoutParams(lp);
row.addView(swipeLayout);
TableRow.LayoutParams rl = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
row.setLayoutParams(rl);//TableRow.LayoutParams.WRAP_CONTENT));
row.setPadding(0, 0, 0, 0);
I have confirmed that generateRoutineLayout(c) works properly and generates a correct GridLayout. After closely inspecting the Tree View in Android monitor it is clear that the grid layout IS added to the SwipeLayout and IS properly sized, but it is simply not appearing!
I know that SwipeLayout extends FrameLayout. Is it possible that I can't insert a GridLayout into a FrameLayout?
Thanks
Try having RelativeLayout inside your FrameLayout and then add your GridLayout inside the RelativeLayout.
Hope this helps you.

Add widgets(EditText) to existing xml structure

I've set up an xml layout for an android activity, its consists of a LinearLayout which holds
a ScrollView, a TableLayout with two Buttons.
The ScrollView itself contains an other TableLayout with a default number of TableRows, let's say six. The TableRows are all of the same type - three EditText widgets.
I would like to give the user the option to add or remove TableRows which should then integrate into the TableLayout.
I know how to add Widgets to a layout but not really how to integrate them into an existing xml structure!
So my question is how to specify the layout parent for the addView() method, so that the TableRows and EditTexts would be added/removed as the last ScrollView item.
I would like to use a "counter" method to monitor the number of TableRows and to provide the IDs for the next item.
An actual Row should look like this and each new one should be a copy of it with different IDs
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/MainExercise1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/MainExerciseHint" />
<EditText
android:id="#+id/MainReps1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="#string/MainRepsHint" />
<EditText
android:id="#+id/MainWeight1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="#string/MainWeightHint" />
</TableRow>
// get the viewgroup I suppose it's tablelayout in your case
TableLayout tl =(TableLayout) rootView.findViewById(R.id.your_table_layout);
//create a new tablerow with attributes you want
TableRow r = new TableRow(getActivity());
//create edittext as much as you want
EditText e1 = new EditText(getActivity());
//add edittext to tablerow
r.addView(e1);
//add tablerow to tablelayout
tl.addView(r);

Android TableLayout CustomRow

I am trying to make a day-calendar and for that I am using a tablelayout and I then want to define a custom row(layout xml) containing TextViews to be able to access data here is and example of my table layout:
<TableLayout
android:id="#+id/appointmentListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/unitNul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"/>
<CustomRow><\CustomRow> //this is where I imagined I would have to put my custom element
</LinearLayout>
</TableRow>
I would then proceed to put in appointments based on their time of the day in the correct rows (via. switch logic)
My questions are:
How do I add a Custom element to each tablerow?
Is this the best way to do so?
side note:
I have tried to make a custom view element, but this seems to be a bit too dificult for me to handle. (this is also an option if you know a good guide explaining this)
I have already tried: http://developer.android.com/guide/topics/ui/custom-components.html
create an Xml like this::
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/tablerows"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:stretchColumns="0,1" >
</TableLayout>
</LinearLayout>
</LinearLayout>
then in activity class dynamically add rows and the other views like this;;
TableRow row;
TextView t1, t2;
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1, getResources().getDisplayMetrics());
row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
t1 = new TextView(context);
t1.setTypeface(null, 1);
t1.setTextSize(15);
t1.setWidth(50 * dip);
t1.setPadding(20*dip, 0, 0, 0);
row.addView(t1);
then in ur table(here "myTable") add the row like this::
myTable.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

tablelayout not visible in my custom dialog

I have been trying to get some data to show up in my dialog.
In the dialog I have a checkbox and two buttons that show up, so I know it is loading my layout file.
I am not certain what else to do, so why would the background on my dialog be completely transparent, and more importantly, why can't I see anything in the two views I have experimented with?
Here is my entire layout 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" >
<CheckBox
android:id="#+id/show_all_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Include books read" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/books_by_author_select_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Books" />
<Button
android:id="#+id/books_by_author_cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
<TableLayout android:id="#+id/books_by_author_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
<TableRow android:id="#+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textfield 1-1"></TextView>
<CheckBox android:id="#+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
</LinearLayout>
I have also tried this with a ListView but the same results. Directly below the two buttons the dialog is transparent.
The TableLayout has nine children when it finishes being initialized, and since it wasn't showing up, I then added the TableRow in the xml above, originally that block wasn't there.
this.mContext = context;
setContentView(R.layout.books_by_author);
final TableLayout view = (TableLayout) findViewById(R.id.books_by_author_list);
for(int position = 0; position < list.size(); position++) {
TableLayout table = (TableLayout) findViewById(R.id.books_by_author_list);
// create a new TableRow
TableRow row = new TableRow(mContext);
// create a new TextView
TextView t = new TextView(mContext);
// set the text to "text xx"
t.setText(list.get(position).mTitle);
// create a CheckBox
CheckBox c = new CheckBox(mContext);
// add the TextView and the CheckBox to the new TableRow
row.addView(t);
row.addView(c);
// add the TableRow to the TableLayout
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
view.invalidate();
I have tried this with a ListView and just using an ArrayAdapter, and I have created a custom adapter extending ArrayAdapter and BaseAdapter.
I have also explicitly set the background of the ListView and TableLayout to be Color.YELLOW, and tried setting other colors, but nothing helps.
TableLayout doesn't work in many cases. Moreover, while running your app, TableLayout creates problem.
Always prefer using LinearLayout or FrameLayout.
Linearlayout can fit at any place where you are using TableLayout.
Solution: The problem was that the LinearLayout containing the buttons has fill_parent instead of wrap_content for the layout_height attribute.

How to get two edit text boxes side by side through java code?

Problem solved
now i want to define one edit text to left and other to right...
i tried doing that but didnt work.. when i do fill_parent.. it just show one edit text.. on one line where it shows both of them side by side when i do wrap_content..
now what i want to do is.. have both edit text boxes define certain size and position left and right.. which i tried implementing didnt work??
You need to change the layout from vertical to horizontal in your layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
...
You can also position elements within the layout using the gravity setting. So to position the button in the middle you could use:
<Button android:layout_width="wrap_content"
android:gravity="center"
...
Update: to put two buttons in each row, side by side, simply nest the LinearLayout elements, e.g.
<LinearLayout android:orientation="vertical" ...>
<LinearLayout android:orientation="horizontal" ...>
<Button ... />
<Button ... />
</LinearLayout>
<LinearLayout android:orientation="horizontal" ...>
<Button ... />
<Button ... />
</LinearLayout>
</LinearLayout>
Just create a new linear layout, add the EditTexts to that, then add the new linear layout to your existing layout.
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
EditText a = new EditText(PlusbuttonActivity.this);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
addViewToRoot(l);
}
Basically, you're just reproducing the same structure you would need to produce the layout in your XML.

Categories

Resources