Dynamic TableLayout Android - android

I am creating a android application where i have to show contents of sqlite in Android app.
So i decide to use TableLayout here.But the main point here is that columns and rows from sqlite database is not previously decided,so i need to create that type of layout that deal with dynamic columns and rows.
So here what i am doing is that i have created a layouts sublayout.xml, cells.xml ,mainlayout.xml.
sublayout.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"
android:id="#+id/subLayout_" >
</LinearLayout>
In this layout i am dynamically adding cells using cells.xml.
cells.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" >
<TextView
android:id="#+id/cell_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell"
android:textSize="25dp"/>
</LinearLayout>
And when my one row gets ready i simply added it to mainlayout.xml.
mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/tableLayout_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</TableRow>
</TableLayout>
But i am getting nullpointer exception here.
Here is my class code.
LinearLayout layout,subLayout_;
layout=(LinearLayout)findViewById(R.id.tableLayout_);
subLayout_=(LinearLayout)findViewById(R.id.subLayout_);
for (int i = 0; i < columns.length; i++) {
Log.e("Column name", ""+columns[i]);
View v=inflater.inflate(R.layout.cells, null);
TextView tv=(TextView)v.findViewById(R.id.cell_);
tv.setText(columns[i]);
subLayout_.addView(v);
}
layout.addView(subLayout_);
Please help me what i am doing wrong here ?

Try this
layout = (LinearLayout) findViewById(R.id.tableLayout_);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.sublayout, null);
subLayout_ = (LinearLayout) vi.findViewById(R.id.subLayout_);
for (int i = 0; i < columns.length; i++) {
Log.e("Column name", "" + columns[i]);
ViewGroup v = (ViewGroup) inflater.inflate(R.layout.cells, null);
TextView tv = (TextView) v.findViewById(R.id.cell_);
tv.setText(columns[i]);
subLayout_.addView(v);
}
layout.addView(subLayout_);

You could try this approach here.
Design your xml like this:
<TableLayout>
<Tablerow>
<Add your views here dynamically>
</TableRow>
</TableLayout>
Create the textviews dynamically and go on adding it to the TableRow and finally add the TableRow to the TableLayout.

Related

Inflating View into LinearLayout and editing cmponents in this view

I am trying to add multiple LinearLayouts into one declared in xml. Everyone has 3 textView which will be edited in code. My problem is, when i am inflating xml layout to View object in code, all margins are ignored. My second question:
How can i dynamically set ids to textViews and then edit text in it?
LinearLayout xml which is inflating:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/pointsAwaiting"
android:layout_marginTop="40dp"
android:background="#drawable/background_blue"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:textColor="#FFFFFF"
android:textSize="15dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
</LinearLayout>
He is inflating into this piece of code:
<
ScrollView 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:background="#drawable/background"
tools:context="pl.com.qiteq.zielonomocni_rework.HistoryActivity">
<LinearLayout
android:id="#+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="#+id/historyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
And finnaly java code: (loop counter is for example)
LinearLayout mainView = (LinearLayout) findViewById(R.id.historyView);
for (int i=0; i<=2; i++){
View layout = View.inflate(this, R.layout.history_bar, null);
mainView.addView(layout);
}
The reason all your margins are being ignored is that you are passing null into your inflater here:
View layout = View.inflate(this, R.layout.history_bar, null);
When you do this all the LayoutParams of your view are thrown away. You should replace it with:
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
To set text in the TextViews you don't need to set a different id for each one, just give each one an id. Then in your loop you can do something like:
List<TextView> textViews = new ArrayList<>();
LayoutInflater inflater = LayoutInflater.from(this);
for (int i=0; i<=2; i++){
View layout = inflater.inflate(this, R.layout.history_bar, mainView, false);
mainView.addView(layout);
TextView textView = (TextView) layout.findViewById(R.id.text1);
textViews.add(textView);
}
You would then have a reference to each of your TextViews in the List

How to update the textview dynamically created textview in childlayout?

LinearLayout lay1 = (LinearLayout) findViewById(R.id.lay1);
for(int i=0;i<list1.size();i++) {
View child = lay1.getChildAt(i);
LinearLayout lay3 = (LinearLayout) child.findViewById(R.id.lay3);
for (j = 0; j <list2.size(); j++) {
View child1 = lay3.getChildAt(j);
// View hiddenInfo11 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3 , false);
final TextView name = (TextView) child1.findViewById(R.id.name);
name.setText("new");
}
}
XML Layout:
<LinearLayout
android:id="#+id/lay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/lay2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#color/white"
android:orientation="horizontal">
</LinearLayout>
add_item.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">
<LinearLayout
android:id="#+id/lay3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
add_inner_item.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"
>
<TextView android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Added View Dynamically using the following method.
for(int i=0;i<list.size();i++) {
View hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.add_item, lay, false);
LinearLayout lay3 = (LinearLayout) hiddenInfo.findViewById(R.id.lay3);
lay1.addView(hiddenInfo);
for (j = 0; j < list2.size(); j++) {
View hiddenInfo1 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3, false);
final TextView name = (TextView) hiddenInfo1.findViewById(R.id.name);
name.setText(list2.get(j).get("Name"));
lay3.addView(hiddenInfo1);
}
I have one Layout with creating dynamically and I want to update the child textview from outside of the button click view.I have tried this code.But nothing is changed.Can anyone give suggestion to this sample.
You are looking for "lay2" inside the "lay1" container. According to your XML layout, they are not nested, so you won't be able to find it this way. Change it to this:
LinearLayout lay2 = (LinearLayout) findViewById(R.id.lay2);
You should get the actual lay2 container. I'm not sure whether the text views are there though since I have seen your code for the views' creation.
EDIT:
I've not specified a parent - you should use the Activity for this. Something like:
LinearLayout lay2 = (LinearLayout) MyActivity.this.findViewById(R.id.lay2);

Add view to another one programmatically inside Fragment

Something gone wrong on my code. I have an XML representing layout of my fragment (fragment_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and another xml file representing a single view that shod go inside fragment layout (element.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="number"
android:text="0" />
</RelativeLayout>
and this is how i fill Fragment layout inside onCreateView method:
View view = inflater.inflate(R.layout.fragment_layout, container,
false);
some_elements = getActivity().getResources().getStringArray(
R.array.some_array);
LinearLayout root = (LinearLayout) view.findViewById(R.id.root_layout);
for (int i = 0; i < some_elements.length; i++) {
View element = inflater.inflate(
R.layout.element, container, false);
TextView tv= (TextView) element.findViewById(R.id.tv);
prodotto.setText(prodotti[i]);
EditText et= (EditText) element.findViewById(R.id.et);
root.addView(element);
}
return view;
Now happen that only first element of "some_elements" array is showed inside fragment (element contains 10 elements). why other elements is not showed? what's wrong?
The problem is android:layout_height="match_parent", in your RelativeLayout. It should be wrap_content

Rotating view causes dynamically added checkboxes to have the same text

Using a LayoutInflater, I am dynamically generating multiple TableRows, each which contains a checkbox. Everything worked great until you rotated the display. Each CheckBox gets the same text and checked state as the very last CheckBox created.
I found that if I assigned a unique ID to each row and to each checkbox it works correctly (each checkbox keeps its unique text and checked state).
If inflating the same layout multiple times, Do I need to assign a unique ID to every View inside the inflated layout? That sure could be a pain if you have an involved layout that is being inflated.
Here is my code that works:
main activity
public class PrototypeActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableLayout tablesLayout = (TableLayout)findViewById(R.id.TableLayout1);
tablesLayout.removeAllViews();
String[] values = new String[] { "Item 1", "Item 2", "Item 3"};
for (int i = 0; i < values.length; i++) {
View view = inflater.inflate(R.layout.row_layout, null, false);
view.setId(i);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.tableCollectCheckBox);
checkBox.setId(values.length+i);
checkBox.setText(values[i]);
tablesLayout.addView(view);
}
tablesLayout.invalidate();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal">
<CheckBox
android:id="#+id/tableCollectCheckBox"
android:text="DataTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</TableRow>
You should call checkBox.setFreezesText(true).

How do you add dynamically a view from xml file, I want to add TableRow in RelativeLayout

I am trying to add table row dynamically in my activity. The table row is in the relative layout. It looks fine but don't know where I am going wrong. Below is my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
for(int i = 1; i <3; i++)
RLayout.addView(tableRow); //My code is crashing here
}
And the main.xml as follows
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableRow
android:id="#+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
>
</TextView>
</TableRow>
</RelativeLayout>
Please Help.
It's crashing because that TableRow is already in the layout. If you want to add some dynamically you have to create it programmatically, that is:
// PSEUDOCODE
TableRow newRow = new TableRow(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(/*....*/);
newRow.setLayoutParams(lp);
relLayout.add(newRow);
Actually TableRow should be used inside TableLayout.
If you want to use something more than once, you can use the inflate technique. You need to create an xml layout that includes the only part that you want to repeat (so your TableRow and its children), and then:
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.your_layout, null);
Now inflated contains the layout you specified. Instead of null, you might want to put there the layout to which attach the inflated one. Every time you need a new element like that, you would have to inflate it the same way.
(You should always report the error you get when it crashes)
------ EDIT -----
ok now i see, this is your code:
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
for(int i = 1; i <4; i++) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflated = inflater.inflate(R.layout.main, tableRow);
}
this way you're inflating your whole layout inside the original TableRow.
You should have a row.xml layout like this, together with the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
/>
</TableRow>
and then inflate it like this:
RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
for(int i = 1; i <4; i++) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.row, RLayout);
}
see if it works.

Categories

Resources