For the record, a long time programmer, but very new to android development. Basically trying self learn. I have hit a road block, have research and tried variations on things I've found, but am really stuck.
I want to create a row of controls this is dynamically created when I tap on a button (Set). This row would be added to the bottom of each subsequent row
Picture of what a row of controls would look like
I've was able to get the program to add a layout to a scrollview and achieve the goal of adding dynamically a row, but I cannot get the textview to size out correctly. I've tried layoutparam, setwidth with no luck. The text gets wrapped no matter what I do.
Picture of running app, rows added, TextView not sizing correctly
I tried a linearlayout, but that just overlays and even with just text, the textview remains the same size.
I am trying to get the dynamic creation to match the "hardcoded" layout I did to see how it would look. Specifically, what am I missing to size the TextView and or any of the other controls (view objects?) in a row.
Here is my view xml
<?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">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="#+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="#+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="#+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="#+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="#+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_time"
android:id="#+id/lblTime"
android:layout_row="0"
android:layout_column="1" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="#+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_Gait"
android:id="#+id/lblGait"
android:layout_row="0"
android:layout_column="0" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="#+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="#+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/my_root">
</LinearLayout>
</ScrollView>
</LinearLayout>
This is the original xml I created to help me visualize what I wanted when I created the same thing dynamically.
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<ImageView
android:src="#drawable/ic_action_pointer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="bottom" />
<TextView
android:layout_width="232dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Canter - 99 min"
android:id="#+id/textView5"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:paddingLeft="15dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:text="Del"
android:id="#+id/button2"
android:layout_row="0"
android:layout_column="2" />
Here is the function that attempts to create a row consisting of an image, a textview and a button (I've kept in the comments to show what I've tried)
#TargetApi(Build.VERSION_CODES.M)
private void buildDisplayLine(int count) {
// create a layout params to define how we add
// GridLayout.LayoutParams gridParams = new GridLayout.LayoutParams();
// gridParams.width = GridLayout.LayoutParams.MATCH_PARENT;
// gridParams.height = GridLayout.LayoutParams.WRAP_CONTENT;
// gridParams.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams imgLayout =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create a grid layout to add controls
GridLayout newGrid = new GridLayout(this);
newGrid.setId(100 + count);
newGrid.setColumnCount(3);
// LinearLayout newGrid = new LinearLayout(this);
// newGrid.setLayoutParams(imgLayout);
// newGrid.setVerticalGravity(LinearLayout.VERTICAL);
//create an image control
ImageView newImage = new ImageView(this);
newImage.setLayoutParams(imgLayout);
newImage.setImageResource(R.drawable.ic_action_pointer);
newImage.setId(count + 200);
Spinner spinner = (Spinner)findViewById(R.id.spinner_gaits);
TextView textGait = (TextView)spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView)findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
String display = gtResult + " - " + tmeResult + " min";
txtTime.setText("");
// create a text view control
TextView newText = new TextView(this);
newText.setLayoutParams(imgLayout);
newText.setGravity(Gravity.CENTER_VERTICAL);
Resources r = getResources();
int pc = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 332, r.getDisplayMetrics());
newText.setWidth(pc);
//newText.getLayoutParams().width = toPixels(432);
newText.setPadding(toPixels(15), 0, 0, 0);
// newText.setTextAppearance(android.R.style.TextAppearance_Large);
newText.setText(display);
newText.setId(300 + count);
// create a new button
Button newButton = new Button(this);
newButton.setLayoutParams(imgLayout);
newButton.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
newButton.getLayoutParams().width = toPixels(79);
newButton.setText("Del");
newButton.setId(400 + count);
// now add to the original grid layout
newGrid.addView(newImage);
newGrid.addView(newText);
newGrid.addView(newButton);
LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(newGrid);
spinner.requestFocus();
}
Why don't you use LayoutInflater to inflate your XML, set the values, and add the view? Something like this:
GridView newGrid = getLayoutInflater().inflate(R.layout.grid_row, null);
newGrid.setId(100 + count);
...
my_root.addView(newGrid);
Related
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
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.
I've been trying to separate 3 EditTexts from each other with a small margin in between. However, i've been unsuccessful. I've googled a lot and tried multiple different options, but this is the closest i can find to give me my result.
I want the second line of "Item Part No." to look like the first line. The closest i can get is a small gap between "Item Part No." and "Q", but "Q" and "RRP" still remain squished together. To do this i remove the "Params3" linear layout. But for now i'm keeping it in, but they all touch together.
The first line is done in the XML Layout so i know how i wanted it to look.
Below is the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.7f);
params.setMargins(0,0,10,10);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.15f);
params.setMargins(10, 0, 10, 10);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 0.15f);
params.setMargins(10, 0, 0, 10);
LinearLayout LL = new LinearLayout(Home.this);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
LL.setWeightSum(1f);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setLayoutParams(LLParams);
EditText partNo = new EditText(Home.this);
partNo.setId(View.generateViewId());
partNo.setLayoutParams(params);
partNo.setPadding(10,10,10,10);
partNo.setBackgroundResource(R.drawable.textinputborder);
partNo.setSingleLine(true);
partNo.setHint("Item Part No.");
LL.addView(partNo);
EditText partqua = new EditText(Home.this);
partqua.setId(View.generateViewId());
partqua.setLayoutParams(params2);
partqua.setPadding(10,10,10,10);
partqua.setBackgroundResource(R.drawable.textinputborder);
partqua.setSingleLine(true);
partqua.setHint("Q");
LL.addView(partqua);
EditText itemPri = new EditText(Home.this);
itemPri.setId(View.generateViewId());
itemPri.setLayoutParams(params3);
itemPri.setPadding(10,10,10,10);
itemPri.setBackgroundResource(R.drawable.textinputborder);
itemPri.setSingleLine(true);
itemPri.setHint("RRP");
LL.addView(itemPri);
mLayout.addView(LL);
Here is the XML used for the first row. I've tried changing the margins to match those of the XML and unfortunately the gaps still don't appear between Edit Texts.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/items"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1name"
android:layout_weight=".7"
android:hint="Item Part No."
android:singleLine="true"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1quant"
android:layout_weight=".15"
android:layout_marginRight="5dp"
android:hint="Q"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1price"
android:layout_weight=".15"
android:layout_marginRight="5dp"
android:hint="RRP"
/>
</LinearLayout>
EDIT: Solved
Thanks to rajen-raiyarela for telling me about the LayoutInflator.
Home.java
plusItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ViewGroup parent = (ViewGroup) findViewById(R.id.room1);
view = LayoutInflater.from(Home.this).inflate(R.layout.newitem, parent, false);
view.generateViewId();
parent.addView(view);
}
});
newitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/items"
android:layout_marginTop="5dp"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1name"
android:layout_weight=".7"
android:hint="Item Part No."
android:singleLine="true"
android:textSize="12dp"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1quant"
android:layout_weight=".07"
android:layout_marginRight="5dp"
android:hint="Q"
android:textSize="12dp"
android:singleLine="true"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/textinputborder"
android:id="#+id/r1i1price"
android:layout_weight=".2"
android:layout_marginRight="5dp"
android:hint="RRP"
android:textSize="12dp"
android:singleLine="true"
/>
</LinearLayout>
i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/price_list_background"
/>
<CheckBox android:id="#+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="#+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="#+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="#+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="#+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.
I'd like to display this under a toggle button when the toggle button is pressed
then disappear when the toggle button is pressed again, I've looked into expandable list views but it's seems over complicated for a few items that wont change visually.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textSize="40dp"
android:id="#+id/button"
android:text="Dummy Job"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- <android.support.v7.widget.RecyclerView
android:id="#+id/jobStats"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView> -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewMachine" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewItem" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="New Text"
android:id="#+id/viewDate" />
<LinearLayout
android:id="#+id/testing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobOpen"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="Button"
android:id="#+id/bJobEdit"
android:layout_weight="1" />
<Button
android:layout_width="245dp"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/bJobExport"
android:textSize="40dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Here is what i have for it's creation.
LinearLayout layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
//layout.setLayoutParams(lp);
//layout.setLayoutParams(lp);
//layout.setOrientation(LinearLayout.HORIZONTAL);
TextView viewMachine = new TextView(Reports.this);
TextView viewItem = new TextView(Reports.this);
TextView viewDate = new TextView(Reports.this);
LinearLayout horLayout = new LinearLayout(context);
Button enterJob = new Button(Reports.this);
Button editJob = new Button(Reports.this);
Button exportJob = new Button(Reports.this);
//enterJob.setLayoutParams(lp);
//editJob.setLayoutParams(lp);
//exportJob.setLayoutParams(lp);
if( button.isChecked()==(true)) {
layout.addView(viewMachine,lp);
layout.addView(viewItem,lp);
layout.addView(viewDate,lp);
layout.addView(enterJob,lp);
layout.addView(editJob, lp);
layout.addView(exportJob, lp);
}
else{
//layout.setVisibility(View.INVISIBLE);
}
}
};
}
Problems are when i create this it looks like this
setting the view to invis
suggestion to use this lib directly in your app.
ExpandableLayout