Dynamically added tablerow only last row not visible - android

I have created a tablelayout with the header row added in the XML file and the remaining content rows filled in dynamically.
For some reason, the last row is not visible.
I looked through other related posts and everything seems okay in my code.
Appreciate if someone could take a look and point out the problem.
XML code:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.bluechipsys.mcpackagemanager.PMActivity"
android:id="#+id/view_layout">
<ProgressBar
android:id="#+id/installProgressBar"
android:indeterminate="true"
style="#android:style/Widget.Holo.ProgressBar.Large"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
<ScrollView
android:id="#+id/textAreaScroller"
android:layout_above="#+id/btn_reload"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:singleLine="false">
<TextView
android:text="#string/msg_default"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="#+id/viewLog"
android:maxLines="1000"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:singleLine="false"
android:background="#drawable/border" />
</ScrollView>
<TextView
android:id="#+id/versionInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tableLayoutScroller"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/tableLayoutScroller"
android:text="#string/tv_versioninfo"
android:textStyle="bold" />
<ScrollView
android:id="#+id/tableLayoutScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:fadeScrollbars="false"
android:scrollbars="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:id="#+id/tbl_packagelist"
android:orientation="vertical"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="false"
android:layout_centerHorizontal="false"
android:layout_centerInParent="false"
android:layout_marginTop="30dp"
android:layout_alignParentStart="#+id/textAreaScroller"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller"
android:layout_above="#+id/textAreaScroller"
android:scrollHorizontally="false"
android:stretchColumns="1,2,3,4,5"
android:shrinkColumns="2"
android:background="#drawable/border_table">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:background="#android:drawable/list_selector_background">
<CheckBox
android:id="#+id/cbSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/title_cell_shape"
android:onClick="onCheckboxClicked" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tvState"
android:background="#drawable/title_cell_shape"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_package"
android:id="#+id/tvPackage"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_instver"
android:id="#+id/tvInstver"
android:singleLine="false"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:textAlignment="center"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_latestver"
android:id="#+id/tvLatestver"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/tv_size"
android:id="#+id/tvSize"
android:background="#drawable/title_cell_shape"
android:textColor="#000"
android:textStyle="bold"
android:gravity="fill"
android:paddingLeft="2dp" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reload"
android:id="#+id/btn_reload"
android:background="#android:color/transparent"
android:drawableTop="#drawable/reload"
android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_apply"
android:id="#+id/btn_apply"
android:background="#android:color/transparent"
android:drawableTop="#drawable/apply"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_close"
android:id="#+id/btn_close"
android:background="#android:color/transparent"
android:drawableTop="#drawable/close"
android:layout_alignTop="#+id/btn_reload"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textAreaScroller"
android:layout_alignEnd="#+id/textAreaScroller" />
</RelativeLayout>
Java Code:
private void clearTableRows() {
Logger.d(TAG, "Clearing table rows");
TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
int rowCount = main_table.getChildCount();
main_table.removeViews(1, rowCount - 1); // Leave header row intact
}
// Create table rows and add to the GUI
private void createTableRows() {
Logger.d(TAG, "Creating table rows");
final TableLayout main_table = (TableLayout) findViewById(R.id.tbl_packagelist);
Drawable background;
// Define layout parameters
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemCBParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams itemIVParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
itemIVParams.gravity = Gravity.CENTER; // Centre the image display
TableRow.LayoutParams itemParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
ViewGroup.MarginLayoutParams mLayoutItemParams = new ViewGroup.MarginLayoutParams (itemParams);
MarginLayoutParamsCompat.setMarginStart(mLayoutItemParams, 4); // Need this workaround for API level < 17
for (MCPackage pkg: list_packages) {
// Create the TableRow and widgets to add to the TableRow
TableRow tbl_row = new TableRow(this);
CheckBox cb_select = new CheckBox(this);
ImageView iv_state = new ImageView(this);
TextView tv_package = new TextView(this);
TextView tv_instver = new TextView(this);
TextView tv_currver = new TextView(this);
TextView tv_pkgsize = new TextView(this);
int index = list_packages.indexOf(pkg);
if(index % 2 == 0) {
background = getResources().getDrawable(R.drawable.cell_shape_white);
} else {
background = getResources().getDrawable(R.drawable.cell_shape_grey);
}
// Set GUI parameters for the TableRow
tbl_row.setLayoutParams(rowParams);
tbl_row.setBackground(background);
tbl_row.setId(index); // Set ID to match index of list_packages
// Initialize the cells
cb_select.setLayoutParams(itemCBParams);
cb_select.setId(index + CHECKBOX_ID);
cb_ids[index] = (index + CHECKBOX_ID);
cb_select.setOnClickListener(checkboxOnClick());
cb_select.setOnCheckedChangeListener(checkboxOnCheckChange());
iv_state.setLayoutParams(itemIVParams);
iv_state.setImageResource(pkg.getState().getImageResourceId());
iv_state.setId(index + IVSTATE_ID);
iv_state_ids[index] = (index + IVSTATE_ID);
tv_package.setLayoutParams(itemParams);
tv_package.setText(pkg.getPackageName());
tv_package.setGravity(Gravity.FILL);
tv_package.setTextAppearance(this, android.R.style.TextAppearance_Small);
tv_package.setTextSize(12);
tv_package.setTextColor(Color.DKGRAY);
tv_package.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_instver.setLayoutParams(itemParams);
tv_instver.setText(pkg.getInstalledVersion());
tv_instver.setGravity(Gravity.FILL);
tv_instver.setTextSize(12);
tv_instver.setTextColor(Color.BLUE);
tv_instver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_currver.setLayoutParams(itemParams);
tv_currver.setText(pkg.getUpdateVersion());
tv_currver.setGravity(Gravity.FILL);
tv_currver.setTextSize(12);
tv_currver.setTextColor(Color.DKGRAY);
tv_currver.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv_pkgsize.setLayoutParams(itemParams);
tv_pkgsize.setText(String.valueOf(((int) pkg.getPackageSize()) / KBINBYTES) + "K");
tv_pkgsize.setGravity(Gravity.FILL);
tv_pkgsize.setTextSize(12);
tv_pkgsize.setTextColor(Color.BLUE);
tv_pkgsize.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
// Add the cells to the row
tbl_row.addView(cb_select);
tbl_row.addView(iv_state);
tbl_row.addView(tv_package);
tbl_row.addView(tv_instver);
tbl_row.addView(tv_currver);
tbl_row.addView(tv_pkgsize);
main_table.addView(tbl_row); // Add the row to the table
tbl_row.setOnClickListener(rowClickListener);
tbl_row.setOnLongClickListener(rowLongClickListener);
registerForContextMenu(tbl_row); // Register the table row for context menu
Logger.d(TAG, "Added package: " + pkg.getPackageName());
}
}

I'm not sure if it is still a problem, but try to use NestedScrollView instead of ScrollView.

Related

Fields overlapping, creating layout programmatically

I have a layout XML and when I click on a floatbutton, I create a layout programmatically, so that the data of this layout is passed to this new one.
Here's my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundnewlayout"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#drawable/img_interna" />
<TextView
android:id="#+id/txtTitle_avaliacao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Medicamento"
android:textColor="#color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/txtTitle_avaliacao"
android:layout_marginBottom="40dp"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/linearCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_margin="5dp"
android:elevation="7dp"
android:minHeight="200dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtNome_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Nome:"
android:textSize="10sp" />
<AutoCompleteTextView
android:id="#+id/edtNome_medicamento"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtNome_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="Paracetamol"
android:maxLines="1"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtNome_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Quantidade:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtQtd_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtQtd_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="3"
android:maxLines="1"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/txtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/edtQtd_medicamento"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:alpha="0.54"
android:text="Posologia:"
android:textSize="10sp" />
<EditText
android:id="#+id/edtPosologia_medicamento"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtPosologia_medicamento"
android:layout_marginStart="10dp"
android:backgroundTint="#color/White"
android:hint="2 vezes ao dia a cada 8 horas"
android:minWidth="50dp"
android:padding="7dp"
android:textColor="#color/black"
android:textSize="14sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fltNewLayout"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/cardView"
android:layout_marginRight="5dp"
android:layout_marginTop="170dp"
android:backgroundTint="#color/colorPrimaryDark"
android:clickable="true"
android:src="#drawable/icone_check"
app:fab_colorPressed="#color/colorPrimary"
app:rippleColor="#android:color/white" />
<LinearLayout
android:id="#+id/linearReceita"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fltNewLayout"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
</RelativeLayout>
When I click on the floatbutton, I create a new layout with the data of this layout that is fixed.
But, the fields of the layout created programmatically, overlap, how to fix?
The method below is as follows:
private void createNewLayout(){
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,15,5,5);
//linearLayout.setLayoutParams(params);
cardView = new CardView(getContext());
cardView.setLayoutParams(linearLayout.getLayoutParams());
cardView.setPadding(10,10,10,10);
cardView.setMaxCardElevation(6);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(linearLayout.getLayoutParams());
cardView.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
cardView.addView(txtNomeMedicamento);
linearLayout.addView(cardView);
}
I solved the issue by adding a new linear layout, this linear adds the textviews vertically, after adding these fields, it is inserted into the cardview.
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
Change the code below:
private void createNewLayout() {
linearLayout = (LinearLayout) view.findViewById(R.id.linearReceita);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 0, 5, 5);
cardView = new CardView(getContext());
cardView.setLayoutParams(params);
//cardView.setPadding(10, 10, 10, 10);
cardView.setMaxCardElevation(6);
/**
* New added Linearlayout
*/
LinearLayout linearCard = new LinearLayout(getContext());
linearCard.setOrientation(LinearLayout.VERTICAL);
cardView.addView(linearCard);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cardView.setElevation(6);
}
TextView txtNomeTitle = new TextView(getContext());
txtNomeTitle.setText("Nome");
txtNomeTitle.setLayoutParams(params);
//cardView.addView(txtNomeTitle);
linearCard.addView(txtNomeTitle);
TextView txtNomeMedicamento = new TextView(getContext());
txtNomeMedicamento.setText(edtNomeMed1.getText().toString());
edtNomeMed1.setText("");
txtNomeMedicamento.setLayoutParams(params);
txtNomeMedicamento.setPadding(10, 10, 10, 10);
// cardView.addView(txtNomeMedicamento);
linearCard.addView(txtNomeMedicamento);
//cardView.addView(linearLayout);
linearLayout.addView(cardView);
}
I was inserting the fields directly into the cardview, without a layout inside it, which made the fields overlap.

Dynamic list not showing all elements in a linear layout

I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time

Android scrollview not working - TableLayout

I am programatically adding components to TableLayout but scrolling is not working.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ScrollView
android:id="#+id/appointments_view"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/appointmentType_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="#string/text_appointment_type"
android:background="#color/text_grey_light"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<Spinner
android:id="#+id/appointmentTypes"
android:layout_width="wrap_content"
android:padding="#dimen/booking_text_padding"
android:layout_below="#+id/appointmentType_title"
android:background="#null"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/appointmentTypeDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="16sp"
android:layout_below="#+id/appointmentTypes"
android:paddingStart="28dp"
android:paddingEnd="#dimen/list_item_top_padding"
android:textColor="#color/text_grey_light"/>
<TextView
android:id="#+id/select_appointments_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="#id/appointmentTypeDescription"
android:textStyle="bold"
android:text="#string/text_appointment"
android:background="#color/text_grey_light"
android:layout_marginTop="#dimen/list_item_top_padding"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<TextView
android:id="#+id/no_appointments_available"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="#id/select_appointments_title"
android:text="#string/text_no_appointment"
android:layout_centerHorizontal="true"
android:padding="#dimen/list_item_top_padding"
android:textColor="#color/text_grey"/>
<Spinner
android:id="#+id/appointment_days"
android:layout_width="wrap_content"
android:padding="#dimen/booking_text_padding"
android:background="#null"
android:layout_below="#id/select_appointments_title"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/table"
android:layout_below="#id/appointment_days"
android:paddingStart="#dimen/booking_text_padding"
android:paddingEnd="#dimen/booking_text_padding">
</TableLayout>
</RelativeLayout>
</ScrollView>
<android.support.v7.widget.AppCompatButton
android:id="#+id/action_book"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/action_booking"
android:textStyle="bold"
android:background="#color/colorPrimary"
android:textColor="#color/white"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Adding components to tableLayout container
private void addComponents(List<String> values){
for(String value: values){
TableRow tr = new TableRow(getActivity());
TableRow.LayoutParams p1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
tr.setPadding(getValueInDp(0),getValueInDp(8),getValueInDp(0),getValueInDp(0));
tr.setLayoutParams(p1);
TextView tv = new TextView(getActivity());
tv.setText(value);
int padding = getValueInDp(10);
tv.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.rounded_time_view));
tv.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
tv.setId(View.generateViewId());
TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
p.setMarginStart(getValueInDp(25));
tv.setLayoutParams(p);
tv.setClickable(true);
tv.setOnClickListener(valueClicked);
tv.setPadding(padding,padding,padding,padding);
tr.addView(tv);
container.addView(tr);
}
}

Create multiple linear layouts and text views programmatically

Create multiple linear layouts and text views programmatically
I have tried to explain please see the code of xml file to understand the question
See image for my desired output
The xml code below is what I want exactly but programmatically
<RelativeLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABc" />
</LinearLayout>
</LinearLayout>
</RelativeLayout
I want to achieve the same output but programmatically
Try This
add LinearLayout into your xml
<LinearLayout
android:id="#+id/ll_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white">
</LinearLayout>
and change in your Java file like this
LinearLayout ll_main = (LinearLayout) findViewById(R.id.ll_main);
for(int i= 0; i <5 ;i++) {
LinearLayout parent = new LinearLayout(Main.this);
LinearLayout.LayoutParams param= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(Main.this);
tv.setText("T1");
TextView tv2 = new TextView(Main.this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
ll_main.addView(parent);
}
i just got my answer and its below
LinearLayout ll_main = (LinearLayout) findViewById(R.id.linear);
ll_main.setVisibility(View.VISIBLE);
int j = 5;
final LinearLayout[] linearlayout = new LinearLayout[j];
for (int i = 0; i < j; i++) {
LinearLayout parent = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
parent.setLayoutParams(param);
parent.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("T1");
TextView tv2 = new TextView(this);
tv2.setText("T2");
parent.addView(tv);
parent.addView(tv2);
linearlayout[i] = parent;
ll_main.addView(parent);
}
}
}

Adding view to bottom of Table Layout

I am working on an android project and I have a table layout. I have the Table Layout with one row in the XML layout file for the table headings. The rest of the table is populated dynamically from the result of an API.
I'm adding the views to the table layout, but the rows I'm adding dynamically appear above the table headings.
Below is my XML layout:
<?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">
<include layout="#layout/toolbar" />
<TableLayout android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="false"
android:layout_margin="10dp"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardToolbar">
<TextView
android:text="Device Name"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Last Logged In"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="From"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white"/>
</TableRow>
</LinearLayout>
Below is how I am populating the rest of the table
JSONArray dataObject = jsonObject.getJSONArray(Defines.JSONElements.DATA);
for (int i = 0; i < dataObject.length(); i++)
{
TableRow row = (TableRow)getLayoutInflater().inflate(R.layout.table_row_layout, tblAuthenticatedDevices, false);
JSONObject deviceData = dataObject.getJSONObject(i);
TextView txtDeviceName = new TextView(AuthenticatedDevices.this);
txtDeviceName.setText(deviceData.getString("DeviceName"));
row.addView(txtDeviceName);
TextView txtLastLoggedIn = new TextView(AuthenticatedDevices.this);
txtLastLoggedIn.setText(deviceData.getString("LastLoggedIn"));
row.addView(txtLastLoggedIn);
TextView txtLocation = new TextView(AuthenticatedDevices.this);
txtLocation.setText(String.format("%s - %s", deviceData.getString("Country"), deviceData.getString("City")));
row.addView(txtLocation);
tblAuthenticatedDevices.addView(row);
}
Just to re-iterate, the row data that I am dynamically adding to the table appears above the row headings I've added to my XML layout. I expected the row headings to appear, then below that would appear the rows that I am adding to the table.
Okay, so I've been researching regarding TableLayouts where there is always a header included to them, and basically, all of them just adds the TableRow header first via code, in order for it to be on top. So here's what I did as a reference for you.. First, I just did a separate layout for the header.
tr_header.xml
<?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="match_parent">
<TextView
android:textStyle="bold"
android:id="#+id/tv_device_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="device name" />
<TextView
android:id="#+id/tv_last_login"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="last_login" />
<TextView
android:id="#+id/tv_location"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="location" />
</TableRow>
Then in the layout of the activity where the tableLayout is, I just kept it empty, like so...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aalawi.myapplication.MainActivity">
<TableLayout
android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:shrinkColumns="false"
android:stretchColumns="1" />
</LinearLayout>
And then in the code, I just add first the header, then iterated on some sample data afterwards. Also, I presume you also have a different layout for your table_rows as mentioned in the code you provided (named table_row_layout), so I just did something similar.
tl_row.xml
<?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="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/tv_device_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="#+id/tv_last_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<TextView
android:id="#+id/tv_location"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
</TableRow>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
tl = (TableLayout) findViewById(R.id.tblAuthenticatedDevices);
List<String> deviceNames = Arrays.asList("1", "2", "3");
List<String> lastLogin = Arrays.asList("11", "22", "33");
List<String> location = Arrays.asList("111", "222", "333");
// Add Header First...
TableRow header = (TableRow) getLayoutInflater().inflate(R.layout.tr_header, null);
tl.addView(header);
// Fill additional rows...
for (int i = 0; i < deviceNames.size(); i++) {
TableRow row = (TableRow) getLayoutInflater().inflate(R.layout.tl_row, tl, false);
TextView txtDeviceName = (TextView) row.findViewById(R.id.tv_device_name);
TextView txtLastLoggedIn = (TextView) row.findViewById(R.id.tv_last_login);
TextView txtLocation = (TextView) row.findViewById(R.id.tv_location);
txtDeviceName.setText(deviceNames.get(i));
txtLastLoggedIn.setText(lastLogin.get(i));
txtLocation.setText(location.get(i));
tl.addView(row);
}
}
}
That resulted to this.
Add this before you create your data rows to create the header:
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
//Title header for each column here
String[] headerText={"ID","NAME","TYPE"};
for(String c:headerText) {
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(5, 5, 5, 5);
tv.setText(c);
rowHeader.addView(tv);
}
tableLayout.addView(rowHeader);
I've realised what I've done wrong when I took another look this morning.
I've ended the Table Layout (i.e. I have <TableLayout... />) then I've added my TableRow but its outside the TableLayout.
Instead I have put my headings inside the TableLayout tag as below and it works as expected.
<TableLayout android:id="#+id/tblAuthenticatedDevices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="false"
android:layout_margin="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/cardToolbar">
<TextView
android:text="Device Name"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="Last Logged In"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="From"
android:textStyle="bold"
android:padding="10dp"
android:textColor="#color/white"/>
</TableRow>
</TableLayout>

Categories

Resources