Spread imageViews evenly over tablerow - android

Like the title says, I have problems in spreading ImageViews evenly over tableRows. My xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ProgressBar/>
<TextView/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progressBar">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
After the Activity is started a dynamical Table is created with the following code:
for (int i = 0; i < 4; i++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.setId(i + 1);
tableRow.setBackgroundResource(R.mipmap.shelfboard);
tableRow.setPadding(20, 0, 20, 0);
tableRow.setOnClickListener(thisvariable);
tableLayout.addView(tableRow);
}
Now I want to add different numbers of ImageViews of the same width for each tableRow to each tableRow.
I have 4 TableRows
add 2 ImageViews to tableRow 1
add 5 ImageViews to tableRow 2
add 3 ImageViews to tableRow 3
add 7 ImageViews to tableRow 4
My Code is:
for (int i = 0; i < adpList.size(); i++) {
int row = adpList.get(i).getShelfBoard();
if (row==1) {
int productsPerBoard = 2;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==2) {
int productsPerBoard = 5;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
productPicture.setBackgroundResource(R.drawable.border);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==3) {
int productsPerBoard = 3;
int width = (tableLayout.getWidth()) / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
if (row==4) {
int productsPerBoard = 7;
int width = tableLayout.getWidth() / productsPerBoard;
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width, TableRow.LayoutParams.MATCH_PARENT);
productPicture = new ImageView(this);
r = (TableRow) findViewById(row);
productPicture.setLayoutParams(tlp);
}
productPicture.setBackgroundResource(R.mipmap.ic_launcher);
r.addView(productPicture);
Unfortunately every row has only two pictures. It always takes the width of the first board. My Output:
How can I add different numbers of ImageViews to each row?

I thought too complicated. Changing the LayoutParams solved my problem:
r = (TableRow) findViewById(shelfboard);
productPicture = new ImageView(this);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT, 1f);
tlp.setMargins(0, 20, 0, 50);
productPicture.setLayoutParams(tlp);

Related

Create buttons programmatically goes new line

I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Modify columns width from code android

I would like to know if there is a way to modify the width of two columns in a table layout (with three columns in total).
The problem is that every column has the same width and a lot of space is wasted because the first two only contain an id and a date meanwhile the last one contains a multiline description.
This is the code in which the table layout is populated:
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tr_head.setBackgroundColor(Color.rgb(0, 230, 230));
TextView lblSystemStoricalHeaderId = new TextView(this);
lblSystemStoricalHeaderId.setText("ID ");
tr_head.addView(lblSystemStoricalHeaderId);
TextView lblSystemStoricalHeaderData = new TextView(this);
lblSystemStoricalHeaderData.setText("Data");
tr_head.addView(lblSystemStoricalHeaderData);
TextView lblSystemStoricalHeaderDescription = new TextView(this);
lblSystemStoricalHeaderDescription.setText(" Descrizione");
tr_head.addView(lblSystemStoricalHeaderDescription);
tr_head.setMinimumHeight(30);
tlSystemStorical.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
for(int i=0;i<reportList.length;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setText(""+reportList[i].getId()+"");
tr.addView(lblSystemStoricalId);
TextView lblSystemStoricalData = new TextView(this);
long dataC = reportList[i].getDate()*1000;
java.util.Date df = new java.util.Date(dataC);
String vv = new SimpleDateFormat("dd/MM/yyyy").format(df);
lblSystemStoricalData.setText(vv);
tr.addView(lblSystemStoricalData);
TableRow.LayoutParams tv3Params = new TableRow.LayoutParams();
tv3Params.width=0;
tv3Params.weight=1;
TextView lblSystemStoricalDescription = new TextView(this);
lblSystemStoricalDescription.setText(""+reportList[i].getReportDescription());
lblSystemStoricalDescription.setLayoutParams(tv3Params);
tr.addView(lblSystemStoricalDescription);
tr.setBackgroundResource(R.drawable.border);
tr.setMinimumHeight(40);
tlSystemStorical.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
and this is the xml file:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scroll_view_system_description"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/tl_system_storical"
>
</TableLayout>
</FrameLayout>
</ScrollView>
You can set width property to 0 for TableRow children, but specify layout_weight for them. So you'd write:
float[] weights = new float[] { 0.2f, 0.2f, 0.6f};
TextView lblSystemStoricalId = new TextView(this);
lblSystemStoricalId.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weights[0]))
Where 0 is no width for view (because you're using weights), and weights array contains weights for your three views.
These weights should sum up to 1, and each weight is percent of the width that the column should take. So you need to assign weights to your TableRow children - lblSystemStoricalId would get weights[0], lblSystemStoricalData would get weights[1] and lblSystemStoricalDescription would have weights[2].

TableLayout fills parent

i want to add a TabeLayout to a LinearLayout programmatically.
This is my activity_main.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"
android:id="#+id/content">
</LinearLayout>
and this is how i add the table in my MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.content);
String[] content = new String[] {"Hello", "World", "Test"};
float d = getResources().getDisplayMetrics().density;
int dp = (int)(3*d);
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableLayout table = new TableLayout(this);
table.setLayoutParams(tableParams);
for(int i = 0; i < content.length; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(rowParams);
int length = content[i].length();
TextView textView = new TextView(this);
textView.setLayoutParams(rowParams);
textView.setText(content[i]);
textView.setPadding(dp, 0, dp, 0);
row.setBackgroundColor(Color.BLUE);
row.addView(textView);
table.addView(row);
}
table.setBackgroundColor(Color.GRAY);
linearLayout.addView(table);
}
}
as you can see the tableParams are set to wrap_content. However, when i set the background color of the table i see, that the width of the table is as long as the display. Why is that so and how can i change that?
This is the output:
http://postimg.org/image/e29bsg9vv/
TableLayout deals with width a bit differently.
You need to let the TableLayout know that it can shrink the columns.
Try:
table.setShrinkAllColumns(true);
Take a look at the reference docs for TableLayout. In particular, notice it states the following:
The total width of the table is defined by its parent container.
Also, as a tip, you can replace:
table.setLayoutParams(tableParams);
linearLayout.addView(table);
with:
linearLayout.addView(table, tableParams);

adding table layout programmatically in android

I'm adding table in linearlayout programmatically but it is not displaying on screen.
Below is code -
public void displayTable(){
TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableLayout mTableLayout=new TableLayout(getActivity());
TableRow mTableRow;
LinearLayout mTableLinearLayout;
TextView mSrNotxt,mDatetxt,mTimetxt;
Date mDate=new Date();
String mCDate=mDate.getDate()+"-"+(mDate.getMonth()+1)+"-"+(mDate.getYear()+1900)+"";
mTableLayout.setLayoutParams(lp2);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams tlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
for(int count=0;count<mTimeList.size();count++)
{
mTableRow=new TableRow(getActivity());
mTableRow.setLayoutParams(lp);
mSrNotxt=new TextView(getActivity());
mDatetxt=new TextView(getActivity());
mTimetxt=new TextView(getActivity());
mSrNotxt.setTextColor(getResources().getColor(R.color.black));
mDatetxt.setTextColor(getResources().getColor(R.color.black));
mTimetxt.setTextColor(getResources().getColor(R.color.black));
mSrNotxt.setLayoutParams(tlp);
mDatetxt.setLayoutParams(tlp);
mTimetxt.setLayoutParams(tlp);
mSrNotxt.setTextSize(12);
mDatetxt.setTextSize(12);
mTimetxt.setTextSize(12);
if(count==0){
mSrNotxt.setText("No.");
mDatetxt.setText("Date");
mTimetxt.setText("Time in Min.");
}
else{
mSrNotxt.setText((count+1+""));
mDatetxt.setText(mCDate);
mTimetxt.setText(mTimeList.get(count));
}
mTableLinearLayout=new LinearLayout(getActivity());
mTableLinearLayout.setLayoutParams(lp);
mTableLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
mTableRow.addView(mTableLinearLayout);
mTableLayout.addView(mTableRow);
}
System.out.println("table created");
mChartLayout.addView(mTableLayout);
}
xml view for chartLayout -
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/chartView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
How can I display table?Is there any way?
Answer -
Need to add textview in view -
mTableLinearLayout.addView(mSrNotxt);
mTableLinearLayout.addView(mDatetxt);
mTableLinearLayout.addView(mTimetxt);
I am able to add Table using this code :
TableLayout mTableLayout = new TableLayout(this);
mTableLayout.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int count = 0; count < 3; count++) {
TableRow row = new TableRow(this);
row.setLayoutParams((new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)));
TextView valueTV = new TextView(this);
valueTV.setText("text : "+count);
// valueTV.setId(5);
valueTV.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
row.addView(valueTV);
mTableLayout.addView(row);
}
mChartLayout.addView(mTableLayout);
Add other required properties
Hope it helps ツ

How to convert rows to columns using tablelayout dynamically in Android

I have a tablelayout that gets data dynamically. and I would like to convert the rows to columns. I changed orientation but didnt work.
TableLayout tableLayout = (TableLayout) second_row_b.findViewById(R.id.table_current_picks);
List<PickGame> selectedPicks = buildSelectedPicks(response);
int selectedPicksSize = selectedPicks.size() > 5 ? 5 : selectedPicks.size();
for (int i = 0; i < selectedPicksSize; i++) {
TableRow rowView = (TableRow) inflater.inflate(R.layout.include_current_picks_row, null);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 0, 0.1f);
tableLayout.addView(rowView, rowParams);
PickGame pickGame = selectedPicks.get(i);
TextView totalPointsText = (TextView) rowView.findViewById(R.id.text_total_points);
TextView gamePointsText = (TextView) rowView.findViewById(R.id.text_game_points);
NetworkImageView imageView = (NetworkImageView) rowView.findViewById(R.id.image_team_pick);
and this is my XML
<TableLayout
android:id="#+id/table_current_picks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="1">
</TableLayout>

Categories

Resources