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);
Related
I am very new to android app development.In the below code, I am using TableLayout to place my contents but getting some extra spaces in table row. Please suggest how to remove it.
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DataStage 8.5"
android:id="#+id/textViewDS8.5"
android:layout_column="0"
android:textStyle="bold|italic"
android:textColor="#090909"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
In place of using TableRow you can use gridlayout
GridLayout (not GridView) how to stretch all children evenly
this is almost the similar or you can use linear layout and set its orientation horizantal and set the weight of the text view 0.5 in this way both the text will cover half the screen and no free space
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));
I am programmatically adding a TableLayout to a parent LinearLayout in Android. The table has one row with two columns; an EditText and a Button. I am also specifying that the EditText can be stretched to fill the width while the Button remains constant in size (kind of like an input-append component). Everything is fine until I populate a long text as the value for the EditText, which seems to make it stretch so that the whole value can be displayed (rather than being ellipsized). This of course means the button is pushed off the right edge, making it unusable.
The added dynamic layout:
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<EditText
android:id="#+id/added_text"
android:padding="8dp"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_gravity="center_vertical"
android:text=""
androin:scrollHorizontally="true"
android:ellipsize="end"
android:enabled="false"/>
<ImageButton
android:id="#+id/added_button"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="8dp"
android:layout_gravity="center_vertical"
android:src="#drawable/minus"/>
</TableRow>
</TableLayout>
The code that populates the EditText and adds the table:
LinearLayout parent = (LinearLayout) findViewById(R.id.parent); // LinearLayout
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dynamic = li.inflate(R.layout.dynamic, null);
EditText et = (EditText) dynamic.findViewById(R.id.added_text);
et.setText("Some very long text...");
parent.addView(dynamic, 1);
What I want is simple; just to ellipsize the EditText value so that the added view honors the parent width.
Looks like adding the following to the EditText above solved it.
android:layout_weight="1"
Found it here actually:
Android: Stop EditText from going outside of tablerow
I got this screen that have some static and some dynamic views to be created.
I want infinity number of views to be created and i think dynamic is the way forward
Now all i need is to create the top column with image buttons , the edittext with clientname and the spinner with cash in the xml (this is the layout i have currently) now i want if a user clicks the cart icon the views with product name, quantity and price to be generated dynamically and then the bottom views are also generated via xml
like this
As you can see there're two row of product,quantity and price. Currently this is done via xml can someboy help out how can i generate and arrange the row with quantity and price and product name in between an existing xml layout like this?
NB: the main layout is relative layout and the layout that contains the row to be generated dynamically is this
<LinearLayout
android:id="#+id/laygroup0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lvpaymode"
android:gravity="center"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoproduct0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:drawableLeft="#drawable/ic_shopping_cart_black_24dp"
android:ems="10"
android:hint="#string/strproductname"
android:padding="10dp"
android:singleLine="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/autoquantity0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_local_mall_black_24dp"
android:ems="10"
android:hint="#string/strquantity"
android:inputType="numberDecimal"
android:padding="10dp"
android:singleLine="true" >
</EditText>
<EditText
android:id="#+id/autoprice0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_attach_money_black_24dp"
android:ems="10"
android:hint="#string/strprice"
android:inputType="numberDecimal"
android:padding="10dp"
android:singleLine="true" >
</EditText>
</LinearLayout>
</LinearLayout>
I can not give complete code bc I'm writing from my phone, but I can give you more pointers.
Create the parent layout in xml in the main layout. It will be much easyer if its a LinearLayout (any other layout it will be much harder to deal with, more care and more code).
Then use findViewById to reference the parent it in java code:
LinearLayout parent = findViewById(R.layout....);
You will use the parent to add in it the child views.
Next set up a button with an onClick event hooked up. In the onClick event you will inflate the child layout: View child = LayoutInflater.from(context).inflate(R.layout.childxml);
Next use TextView tv = child.findViewById(... to find all the views contained in it etc. Set up other button events here if you have buttons.
At last you can add the child to the parent: parent.addView(child);
If the parent its a linear layout with the orientation set to vertical all the views will arrange nicely one below the other.
If you add many views you risk extending more than the phone's screen allows it and it will not be scrollable. You can overcome this by wrapping the parent linear layout with a ScrollView in xml. Now it will work nicely.
Hope it helps.
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.