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>
Related
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.
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.
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);
im using custom listview in my main layout from here https://github.com/NikolaDespotoski/DoubleTapListViewHandler/tree/master/src/com/nikola/despotoski/doubletaplistview but problem is this wil create custom programicaly layout but in my code i already set content setContentView(R.layout.bid); so how do i add new custom layout in my main layout?? help me what will i do??
private CustomListView bidlist = null;
public class BidSchema extends BackBaseActivity implements OnItemDoubleTapLister {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bid);
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
back = (ImageView) findViewById(R.id.bidback);
bottomlayout = (RelativeLayout)findViewById(R.id.bottom_layout);
scroll_down = (ImageView) findViewById(R.id.down);
scroll_up = (ImageView) findViewById(R.id.up);
Layout 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="wrap_content"
android:background="#drawable/bg2" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_bar1"
android:contentDescription="#string/contentdesc" />
<ImageView
android:id="#+id/bidback"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="3dp"
android:background="#drawable/back_arr"
android:contentDescription="#string/contentdesc" />
<ImageView
android:id="#+id/iconpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="4dp"
android:layout_centerHorizontal="true"
android:background="#drawable/treasure"
android:contentDescription="#string/contentdesc" />
<TextView
android:id="#+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:background="#drawable/bar"
android:contentDescription="#string/contentdesc"
android:gravity="center"
android:text="Bid"
android:textColor="#000000"
android:textSize="22sp" />
<LinearLayout
android:id="#+id/jobhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bar"
android:layout_marginTop="10dp"
android:layout_marginLeft="10sp"
android:orientation="horizontal" >
<TextView
android:id="#+id/jobDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:textColor="#ffffff"
android:textSize="22sp"
android:gravity="left"
android:text="Plot"
android:textStyle="bold" />
<TextView
android:id="#+id/jobpick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="right"
android:paddingRight="50dp"
android:text="Job"
android:textColor="#ffffff"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/bidlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/jobhead"
android:layout_marginBottom="10dp"
android:layout_above="#+id/bottomBar"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/fields_bg"
android:visibility="visible" >
</ListView>
<include
android:id="#+id/bottomBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
layout="#layout/bottom" />
</RelativeLayout>
If you have implement custom Listview then you should change this.
setContentView(R.layout.bid);
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
to
bidlist = new CustomListView(this);
bidlist.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(bidlist);
UPDATE:
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);
bidlist = new CustomListView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootLayout.addView(bidlist, rlp);
Because you can't call setContentView() two times. So you have to add it in your parent Relative Layout. so check that.
Give an id to your parent RelativeLayout as below to add new view to this layout later...
<?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="wrap_content"
android:background="#drawable/bg2"
android:id="#+id/layout" >
..........
</RelativeLayout >
Now, add your bidlist to the parent layout as below...
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.layout);
bidlist = new CustomListView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootLayout.addView(bidlist, lp);
You don't setContentView.
If main activity show posted xml, just make Adapter implements baseAdapter, and then just listview.setAdapter( you make adapter)
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