I have been stuck on this issue for hours and I have no idea how to get passed it.
I have programatically created a table (no problem), but this is the issue I am having. When I create the row, I would then like to create two separate columns. As you'll see in my example, I have two different TextViews on the same line ('Type' and 'Number'). As it stands, both of them are shifted over to the left.
What I would like is for them to be in two separate columns, equally split apart centred in each column.
Here is the relevant code to what I am trying to accomplish.
TableLayout serviceLineLayout = new TableLayout(getContext());
serviceLineLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
TableRow firstTableRow = getFirstTableRow(getContext());
serviceLineLayout.addView(firstTableRow);
private TableRow getFirstTableRow(Context context) {
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
TextView typeHeader = new TextView(context);
typeHeader.setId(0);
typeHeader.setText("Type");
typeHeader.setTextSize(20);
tableRow.addView(typeHeader);
TextView numberHeader = new TextView(context);
numberHeader.setId(0);
numberHeader.setText("Number");
numberHeader.setTextSize(20);
numberHeader.setGravity(Gravity.RIGHT);
tableRow.addView(numberHeader);
return tableRow;
}
For this tablelayout defined in xml, following will be the dynamic code in corresponding java file (Activity/Fragment).
<TableLayout
android:id="#+id/fenceTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
Activity logic for TableView
//Table Layout parameters
TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
TableLayout tableLayout = (TableLayout) view.findViewById(R.id.fenceTableLayout);
TableRow trHead = new TableRow(context);
LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
trHead.setLayoutParams(tableRowParams);
TextView nameHead = new TextView(context);
nameHead.setText("Content left");
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.CENTER);
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText("Content right");
detailHead.setGravity(Gravity.CENTER);
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
NOTE:
Many values have been referred from resource files, You can either neglect/replicate those.
Related
Im trying to create LinearLayout programmaticaly and I would want to allow user to put number range.
Now it looks like that:
But when I try to enter more digits eg. 100, 101 or 3,50 it dissapears.
I guess there is not enough space for it to be shown, but I can't figure out what is wrong. Generally I don't want to move + and - buttons when the user enters some values so I guess it should be hardcoded. There would be up to 5-6 digits only, so I need space just for it, but as I said, I can't find the place, where I can change it as my changes eiter move entire layout or doesn't do anything.
Below is my code:
LinearLayout horizontalLayout = new LinearLayout(mContext);
LinearLayout titleLayout = new LinearLayout(mContext);
LinearLayout countLayout = new LinearLayout(mContext);
ImageButton buttonAdd = new ImageButton(mContext);
ImageButton buttonSub = new ImageButton(mContext);
TextView titleTextView = new TextView(mContext);
EditText countEditText = new EditText(mContext);
final int[] currentCount = {defaultValue};
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
LinearLayout.LayoutParams utilParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(params);
utilParams.gravity = Gravity.CENTER_VERTICAL;
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
titleLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
titleLayout.setLayoutParams(linearLayout);
countLayout.setOrientation(LinearLayout.HORIZONTAL);
countLayout.setPadding(0, pxFromDp(mContext, 16),0, pxFromDp(mContext, 16));
countLayout.setLayoutParams(linearLayout);
utilParams.setMargins(0,0,pxFromDp(mContext, 16f),0);
titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
titleTextView.setText(title);
titleTextView.setLayoutParams(utilParams);
titleLayout.addView(titleTextView);
utilParams.setMargins(pxFromDp(mContext, 16f),0,pxFromDp(mContext, 16f),0);
buttonSub.setImageResource(R.drawable.ic_remove);
buttonSub.setLayoutParams(utilParams);
buttonSub.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonSub.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonSub);
countEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
countEditText.setText(String.valueOf(defaultValue));
countEditText.setLayoutParams(linearLayout);
countEditText.setGravity(Gravity.CENTER);
countLayout.addView(countEditText);
buttonAdd.setImageResource(R.drawable.ic_add_24);
buttonAdd.setLayoutParams(utilParams);
buttonAdd.setBackgroundColor(mContext.getResources().getColor(R.color.fsm_survey_btn));
buttonAdd.setColorFilter(ContextCompat.getColor(mContext, R.color.fsm_white), android.graphics.PorterDuff.Mode.SRC_IN);
countLayout.addView(buttonAdd);
horizontalLayout.addView(titleLayout);
horizontalLayout.addView(countLayout);
What you can do is to create separate LinearLayout.LayoutParams for the edittext and add addTextChangedListener to that edit text and on onTextChanged
write switch statement to increase the weight of the edittext on increase of length of the input number.
I got a simple question. I am trying to create some kind of layout that would display my records from SQLite Database in separate cells. I tried to make it using TextViews, but it is not what I expect. Do you know any ideas to create 'excel like' table with headers etc? What is more i need each row to be able to select and open (My goal is to have something like catalogue with products).
final TextView dTable= (TextView) findViewById(R.id.displayTable);
final ManageDatabase md = new ManageDatabase(this);
dTable.setText("");
for(Product p: md.takeAllProducts()){
dTable.setText(dTable.getText()+"\n"+" "+p.getProductCode()+" "+p.getProductName()+" "+p.getQuantity()+" "+p.getExpireDate());
}
You can use TableLayout for it and dynamically add your content there.
First get the reference of table layout from XML file.
TableLayout myTable = (TableLayout) findViewById(R.id.mTable);
Then you can add retrieved data to the table dynamically like below
for(Product p: md.takeAllProducts()){
//create new row
TableRow tableRow = new TableRow(this);
// create new text view
TextView textView1 = new TextView(this);
textView1.setText(p.getProductCode());
//add to row
tableRow.addView(textView1);
//create another text view and add to same row
TextView textView2 = new TextView(this);
textView2.setText(p.getProductName());
tableRow.addView(textView2);
//
TextView textView3 = new TextView(this);
textView3.setText(p.getQuantity());
tableRow.addView(textView3);
//
TextView textView4 = new TextView(this);
textView4.setText(p.getExpireDate());
tableRow.addView(textView4);
}
I am a newbie to Android Programming. I want a table layout that has 3 rows and 3 columns(may be I will dynamically change these numbers), with each table row having a relative layout inside it. This is similar like the view shown in Google Play Store which shows the new arrivals on Play etc.. I have tried using buttons to achieve this. But I want to inflate an xml file, having Relative Layout as the root with an ImageView and TextView. Is this possible? Or what is the best way to acheive this type of layout? Also, how to find the imageview and textview of the inflated layout so that I can programatically add properties to it. This is what I have tried.
private void populateCategoryGrid() {
TableLayout categoriesLayout;
TableRow row;
Button btn_category; int category_count = 12;
int NUM_ROW = category_count/3;
int NUM_COL = category_count/4;
for (int r=0;r<NUM_ROW;r++) {
row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f
));
categoriesLayout.addView(row);
for (int c = 0; c <NUM_COL; c++) {
btn_category = new Button(this);
btn_category.setId(c);
btn_category.setPadding(0, 0, 0, 0);
btn_category.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f
));
row.addView(btn_category);
}
}
}
You pretty much did all the work by looping and inflating buttons into rows. Few adjustments and you're done:
private void populateCategoryGrid() {
TableLayout categoriesLayout;
int category_count = 12;
int NUM_ROW = category_count/3;
int NUM_COL = category_count/4;
LayoutInflater inflater = LayoutInflater.from(this);
// Add rows
for (int r=0;r<NUM_ROW;r++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f
));
// Add inflated layouts
for (int c = 0; c <NUM_COL; c++) {
// Inflate layout
RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.rel_layout, null);
// Modify inflated layout
ImageView img = (ImageView) rl.findViewById(R.id.img);
img.setBackgroundColor(Color.RED);
TextView tv = (TextView) rl.findViewById(R.id.text);
tv.setText("Some text");
// Add the modified layout to the row
row.addView(rl);
}
// Add row to the table
categoriesLayout.addView(row);
}
}
A few notes:
You can use a local variable for each TableRow since once the row is done, you won't change anything inside of it
LayoutInflater.inflate returns the root view, in which you can findViewById any views located inside of it
I have a table layout in XML that does exactly what I want:
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/evenrow">
<TextView android:text="Check" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_weight="1"></TextView>
I am trying to add the table row programmatically, but I can't seem to get the layout_weight set. Here is the java code:
TableRow.LayoutParams trlayout = new TableRow.LayoutParams (TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
TableLayout.LayoutParams tablelayout = new TableLayout.LayoutParams (TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(trlayout);
TextView check = new TextView(this);
check.setText(String.format("%5.2f", i));
tr.addView(check);
I thought the 3rd param to new TableRow.LayoutParams was the default weight for the row's children, but it is not working.
What am I missing?
Well in a regular XML if you set wrap_content on the dimension of orientation you want to expand in then layout weight has no effect.
Change the width parameter to "0dp" and then your layout weight should work.
I have the following code:
TextView name = new TextView(this);
name.setText(venues.get(j).name);
name.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView address = new TextView(this);
address.setText(venues.get(j).getFullAddress());
address.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(name);
tr.addView(address);
And now it gives me a layout like this:
I want the TextView to be in separate lines. How do I do this?
First of all, don't use LayoutParams for Views that are going into a TableRow... use TableRow.LayoutParams.
Second (pls tell me if I misunderstood your question), if you want your edittexts to show up on different lines... that means you want them on different rows. In other words, don't put them into the same TableRow. You can put them into separate TableRows or use a LinearLayout.
Try adding addr to another TableRow.
TextView name = new TextView(this);
TextView addr = new TextView(this);
name.setText("Name");
addr.setText("Addr");
TableRow tableRow1 = (TableRow) findViewById(R.id.tableRow1);
TableRow tableRow2 = (TableRow) findViewById(R.id.tableRow2);
tableRow1.addView(name);
tableRow2.addView(addr);