I am using a table layout to show information based on a user specifications. The user enters data into a database, then this data is taken and displayed on a table were each row has 3 textView entries. The problem is that if the data entered by the user is to long, it goes off screen and is not visible. Any ideas? The code I used to do this is below:
xml
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FF0000"/>
....
</TableRow>
</TableLayout>
Java
for (final String time : timesArray)
{
i++;
TableRow row1= new TableRow(this);
event = db.getEvent(i, gameId);
player = db.getPlayer(i, gameId);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
timeRow.setText(time);
timeRow.setTextColor(Color.WHITE);
playerRow.setText(player);
playerRow.setTextColor(Color.WHITE);
eventRow.setText(event);
eventRow.setTextColor(Color.WHITE);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1);
}
And it looks like this if the data is too long
SOLUTION!
Setting layout_width to 0 and then assigning layout_weight to a float based on the amount of the screen you want a column to take up (For example, 0.5 would let the column take up half the screen) allows all the information to be shown on the screen. The code to do this is below.
XML
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tl">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp" >
<TextView
android:id="#+id/timeCol"
android:layout_width="0px"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="Time"/>
<TextView
android:id="#+id/playerCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Player"/>
<TextView
android:id="#+id/eventCol"
android:layout_width="0px"
android:layout_weight="0.4"
android:layout_height="wrap_content"
android:text="Event"/>
</TableRow>
java
TableLayout tl = (TableLayout) findViewById(R.id.tl);
TableRow row1= new TableRow(this);
TextView timeRow = new TextView(this);
TextView playerRow = new TextView(this);
TextView eventRow = new TextView(this);
TableRow.LayoutParams text1Params = new TableRow.LayoutParams();
text1Params.width = 0;
text1Params.weight = (float) 0.2;
TableRow.LayoutParams text2Params = new TableRow.LayoutParams();
text2Params.weight = (float) 0.4;
text2Params.width = 0;
TableRow.LayoutParams text3Params = new TableRow.LayoutParams();
text3Params.weight = (float) 0.4;
text3Params.width = 0;
timeRow.setLayoutParams(text1Params);
playerRow.setLayoutParams(text2Params);
eventRow.setLayoutParams(text3Params);
row1.addView(timeRow);
row1.addView(playerRow);
row1.addView(eventRow);
tl.addView(row1, params);
Related
I wish to add a custom tailored layout for each ListView item. I have an empty linear layout in the xml layout file for ListView item layout. It has horizontal orientation.
I wish to add three FrameLayouts dynamically to above mentioned LinearLayout and set custom weight for each FrameLayout.
I found a part of the solution for it, as in the code below. Problem arises when the ListView has to be updated. ListView is initially hidden. When a certain button is pressed, ListView gets updated (hash map cleared and then filled with a new data set) and its visibility set from GONE to VISIBLE. Next thing is to loop through all the items and add designed view programmatically.
If I invoke the method add_designed_views(null); right after updating ListView items, the app loops through items, creates all the new views and adds them to right items. However, they are not added. I know this because of check if the item is already added, and if I invoke this method several times in a row, it will do the same each time, as if it didn't add anything at all.
Next thing that I tried is I postponed adding views dynamically by invoking the method add_designed_views from onClick of a dummy button. When I see the list has loaded (without the custom views) I press the dummy button and the custom views appear. But every 6th is missing. If I press it again, the missing ones appear. Which makes me even more confused.
It's obvious that the views can't be added immediately after changing the visibility of ListView. Probably because it takes the system some time to inflate the ListView, or it does some background task, but I couldn't find any listener or some function that tells me that it has finished loading so that I can add my views then.
Custom ListView item layout (xml):
<?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="wrap_content"
android:minHeight="110dp"
android:background="#drawable/textview_border_dark_line"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="21"
android:minHeight="40dp">
<TextView
android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:minHeight="90dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/path_model"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal"
android:onClick="onClick_listItem"
>
<!-- Path Model. -->
<!-- This is where I add view dynamically. -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_time"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/clock"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/text4_1"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_time" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_changes"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/change"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_2"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_changes"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_cost"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/coins"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_3"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_cost"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="21"
android:minHeight="38dp">
<TextView
android:id="#+id/text3"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
</LinearLayout>
add_designed_views method (Java):
public void add_designed_views(View view)
{
for(int i=0; i<hashmap_search_results_list.size(); i++)
{
/* Check if it is already added. We have to check,
because we run it twice in consecutive order. */
LinearLayout listView_item = (LinearLayout)getViewByPosition(i, listView_search_results);
View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
LinearLayout already_added_ll_path = (LinearLayout)((ViewGroup)path_model).getChildAt(0);
if(already_added_ll_path != null)
{
Log.d(TAG, "Already has linear layout: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
continue;
}
Log.d(TAG, "Doing: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
Search_Result_Item result_item = (Search_Result_Item)search_results_list.get(i);
/* How to access each view at each item position in list view. */
//LinearLayout listView_item = (LinearLayout) getViewByPosition(i, listView_search_results);
//View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
//LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
/* Setting the proportions of lines regarding walking time and riding time. */
HrMin hrmin_walking_to_entrance_time = result_item.walking_to_entrance_time();
HrMin hrmin_riding_time = result_item.travel_time();
HrMin hrmin_walking_from_exit_time = result_item.walking_from_exit_time();
int int_walking_to_entrance_time = hrmin_get_minutes(hrmin_walking_to_entrance_time);
int int_riding_time = hrmin_get_minutes(hrmin_riding_time);
int int_walking_from_exit_time = hrmin_get_minutes(hrmin_walking_from_exit_time);
int total = int_walking_to_entrance_time +
int_riding_time +
int_walking_from_exit_time;
float weight_walking_to_entrance = (float)int_walking_to_entrance_time/total;
float weight_bus_ride = (float)int_riding_time/total;
float weight_walking_from_exit = (float)int_walking_from_exit_time/total;
/* How to add a new layout, view to existing layout. */
LinearLayout ll_path = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
/* Pedestrian image. */
ImageView pedestrian = new ImageView(this);
pedestrian.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
int dimensionInPixel = 16;
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian.setLayoutParams(pedestrian_params);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_to_entrance);
View gray_line = new View(this);
gray_line.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line.setLayoutParams(gray_line_params);
frameLayout_pedestrian_container.addView(gray_line);
frameLayout_pedestrian_container.addView(pedestrian);
frameLayout_pedestrian_container.setLayoutParams(frame_layout_params);
ll_path.addView(frameLayout_pedestrian_container);
/* Bus image. */
FrameLayout frameLayout_bus_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_bus_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_bus_ride);
ImageView bus = new ImageView(this);
bus.setBackground(getResources().getDrawable(R.drawable.bus_rnd));
dimensionInPixel = 17;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams bus_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
bus.setLayoutParams(bus_params);
/* Green line. */
View green_line = new View(this);
green_line.setBackground(getResources().getDrawable(R.drawable.horizontal_green_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams green_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
green_line.setLayoutParams(green_line_params);
frameLayout_bus_container.addView(green_line);
frameLayout_bus_container.addView(bus);
frameLayout_bus_container.setLayoutParams(frame_layout_bus_params);
ll_path.addView(frameLayout_bus_container);
/* Walking from exit path. */
ImageView pedestrian2 = new ImageView(this);
pedestrian2.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
dimensionInPixel = 16;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params2 = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian2.setLayoutParams(pedestrian_params2);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container2 = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_from_exit);
View gray_line2 = new View(this);
gray_line2.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params2 = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line2.setLayoutParams(gray_line_params2);
frameLayout_pedestrian_container2.addView(gray_line2);
frameLayout_pedestrian_container2.addView(pedestrian2);
frameLayout_pedestrian_container2.setLayoutParams(frame_layout_params2);
ll_path.addView(frameLayout_pedestrian_container2);
ll_path.setLayoutParams(params);
ll_path.setOrientation(LinearLayout.HORIZONTAL);
path_model.addView(ll_path);
}
}
Part where I update the ListView and show it:
search_results.setVisibility(View.VISIBLE);
ListView is a child of two layouts:
<LinearLayout
android:id="#+id/search_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone"
android:paddingTop="140dp"
android:orientation="vertical"
android:onClick="onClick_nothing">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_options_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="#+id/mapView"
app:layout_constraintTop_toTopOf="#+id/mapView"
android:background="#android:color/white"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarThumbVertical="#android:color/darker_gray"
/>
</RelativeLayout>
</LinearLayout>
One thing that helped is Handler.postDelayed():
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
add_designed_views (null);
}
}, 300);
This function delays method call for 300 mS. Programmatically added layouts are visible. But, still every 6th is missing. They also appear after I touch any of the items.
I have a TableLayout which is dynamically populated. On getting to the page, it shows as expected but the moment I switch away from the app or a call comes in, the layout is distorted, resulting in inconsistent column width as shown in the images below.
Find below, the code used to generate the table
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical|horizontal"
android:background="#color/colorPrimaryDark">
<TableLayout
android:id="#+id/predictionTableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:stretchColumns="*"
android:background="#color/colorPrimaryDark">
<TableRow>
<TextView
android:text="RESULT"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:background="#000"
android:layout_marginRight="1dp"/>
<TextView
android:text="TIME"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="LEAGUE"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="TEAM"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="PREDICTION"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="SCORE"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:background="#000"/>
</TableRow>
</TableLayout>
</HorizontalScrollView>
Java
int i = 1;
for (Prediction prediction : predictionCategory.getPredictions()) {
//int backgroundColor = (i%2 == 0 ? fragmentActivity.getResources().getColor(R.color.colorPrimaryDark) : fragmentActivity.getResources().getColor(R.color.colorAccent));
Drawable backgroundDrawable = fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even); //(i%2 == 0 ? fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even) : fragmentActivity.getResources().getDrawable(R.drawable.table_cell_odd));
int textColor = fragmentActivity.getResources().getColor(R.color.white);
TableRow tableRow = new TableRow(fragmentActivity);
tableRow.setBackground(backgroundDrawable);
int colWidth = predictionTableView.getChildAt(0).getWidth();
TextView timeTextView = new TextView(fragmentActivity);
TextView leagueTextView = new TextView(fragmentActivity);
TextView teamTextView = new TextView(fragmentActivity);
TextView predictionTextView = new TextView(fragmentActivity);
TextView scoreTextView = new TextView(fragmentActivity);
TextView resultTextView = new TextView(fragmentActivity);
timeTextView.setTypeface(Typeface.DEFAULT);
timeTextView.setText(prediction.getTime());
timeTextView.setTextSize(13);
timeTextView.setPadding(3, 3, 3, 3);
timeTextView.setBackground(backgroundDrawable);
timeTextView.setTextColor(textColor);
leagueTextView.setTypeface(Typeface.DEFAULT);
leagueTextView.setText(prediction.getLeagueName());
leagueTextView.setTextSize(13);
leagueTextView.setPadding(3, 3, 3, 3);
leagueTextView.setBackground(backgroundDrawable);
leagueTextView.setTextColor(textColor);
teamTextView.setTypeface(Typeface.DEFAULT);
teamTextView.setText(prediction.getLocalTeamName() + " - " + prediction.getVisitorTeamName());
teamTextView.setTextSize(13);
teamTextView.setPadding(3, 3, 3, 3);
teamTextView.setBackground(backgroundDrawable);
teamTextView.setTextColor(textColor);
predictionTextView.setTypeface(Typeface.DEFAULT);
predictionTextView.setText(prediction.getPrediction());
predictionTextView.setTextSize(13);
predictionTextView.setPadding(3, 3, 3, 3);
predictionTextView.setBackground(backgroundDrawable);
predictionTextView.setTextColor(textColor);
scoreTextView.setTypeface(Typeface.DEFAULT);
scoreTextView.setText(prediction.getScores());
scoreTextView.setTextSize(13);
scoreTextView.setPadding(3, 3, 3, 3);
scoreTextView.setBackground(backgroundDrawable);
scoreTextView.setTextColor(textColor);
resultTextView.setTypeface(Typeface.DEFAULT);
resultTextView.setText(prediction.getResult());
resultTextView.setTextSize(13);
resultTextView.setPadding(3, 3, 3, 3);
resultTextView.setBackground(backgroundDrawable);
resultTextView.setTextColor(textColor);
if (!prediction.getResult().matches("")) {
if (prediction.getResult().toUpperCase().matches("WIN")) {
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorSuccess));
} else {
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
}
} /*else {
resultTextView.setText("LOST");
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
}*/
tableRow.addView(resultTextView);
tableRow.addView(timeTextView);
tableRow.addView(leagueTextView);
tableRow.addView(teamTextView);
tableRow.addView(predictionTextView);
tableRow.addView(scoreTextView);
TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(trParamsSep);
predictionTableView.addView(tableRow);
i++;
}
What is it that I'm doing wrong? How do I maintain equal column width irrespective of whether the user switch away and come back to the app or stayed on it for long.
Reloading data onResume() works well for me. Thanks #SumitShukla
i'm using a TableLayout on my applycation. This is my XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none">
<include
android:id="#id/action_bar"
layout="#layout/actionbar_toolbar" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/action_bar">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</HorizontalScrollView>
And, i fill the table with Java code:
TableRow tbrow = new TableRow(this);
TextView txt_plazo = new TextView(this);
txt_plazo.setText(" Plazo ");
txt_plazo.setTextColor(Color.WHITE);
txt_plazo.setTextSize(16);
txt_plazo.setMinimumHeight(0);
txt_plazo.setBackgroundColor(Color.DKGRAY);
txt_plazo.setGravity(Gravity.CENTER);
tbrow.addView(txt_plazo);
TextView txt_saldoInicial = new TextView(this);
txt_saldoInicial.setText(" Saldo Inicial");
txt_saldoInicial.setTextColor(Color.WHITE);
txt_saldoInicial.setTextSize(16);
txt_saldoInicial.setBackgroundColor(Color.DKGRAY);
txt_saldoInicial.setGravity(Gravity.CENTER);
tbrow.addView(txt_saldoInicial);
TextView txt_parcialidades = new TextView(this);
txt_parcialidades.setText(" Parcialidades ");
txt_parcialidades.setTextColor(Color.WHITE);
txt_parcialidades.setTextSize(16);
txt_parcialidades.setBackgroundColor(Color.DKGRAY);
txt_parcialidades.setGravity(Gravity.CENTER);
tbrow.addView(txt_parcialidades);
TextView txt_interes = new TextView(this);
txt_interes.setText(" Interés ");
txt_interes.setTextSize(16);
txt_interes.setBackgroundColor(Color.DKGRAY);
txt_interes.setGravity(Gravity.CENTER);
txt_interes.setTextColor(Color.WHITE);
tbrow.addView(txt_interes);
TextView txt_total = new TextView(this);
txt_total.setText(" Abono Capital");
txt_total.setTextSize(16);
txt_total.setBackgroundColor(Color.DKGRAY);
txt_total.setGravity(Gravity.CENTER);
txt_total.setTextColor(Color.WHITE);
tbrow.addView(txt_total);
table.addView(tbrow);
fillTable();
Etc...
All works fine but i'm trying to view the table in landscape mode, the problem is that the table don't fill all screen. In portrait mode looks good.
P.D: android:shrinkColumns and android:stretchColumns don't work for me.
Use match_parent for your views.
I am trying to create the rows below the table heading programmatically, but they are spilling over and I am not sure how to split them up to only take up 25% of the width each?
Here is the code for the row creation:
TableRow row = new TableRow(activity);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f);
tl = new TableLayout.LayoutParams();
tl.weight = 1;
lp.height = 100;
row.setLayoutParams(lp);
row.setWeightSum(4);
tv = new TextView(getActivity());
tv.setLayoutParams(tl);
tv.setTextSize(14);
tv.setText(whotos.get(i));
tv2 = new TextView(getActivity());
tv2.setLayoutParams(tl);
tv2.setTextSize(14);
tv2.setText(documents.get(i));
tv3 = new TextView(getActivity());
tv3.setLayoutParams(tl);
tv3.setTextSize(14);
tv3.setText(directions.get(i));
tv4 = new TextView(getActivity());
tv4.setLayoutParams(tl);
tv4.setTextSize(14);
tv4.setText(thedates.get(i));
row.addView(tv); //tp name
row.addView(tv2); //document type
row.addView(tv3); //direction
row.addView(tv4); //date
ll.addView(row, i + 1 + tableHeadingRows);
Here is my code for the layout above the programmatically created rows:
<TableRow
android:id="#+id/tableRow2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dip"
android:stretchColumns="1"
android:weightSum="4">
<TextView
android:id="#+id/tp"
android:layout_weight="1"
android:layout_width="0dp"
android:text="TP"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/doc"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Doc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dir"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Direction"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/date"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<!-- draw a red line -->
<View
android:layout_height="2dip"
android:background="#color/tableHorizontalRule" />
Does anyone have a solution to this issue?
I changed this over to a LinearLayout, as it simplified things a bit and allowed me to easily set column weights programmatically.
I have created an activity in which I want to programmatically generate rows in a table. The table will have a checkbox, imageview, and 2 textviews in every rows. The rows are getting generated, but the alignment of the contents in the rows are not correct.
The activity code:
public class PlanTripActivity extends ActionBarActivity {
int tableRowCount = 0;
TableLayout plan_a_trip_table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plan_trip);
plan_a_trip_table = (TableLayout) findViewById(R.id.plan_a_trip_table);
planTripBody("", "Test Name Type",
"P1");
}
//table body
public void planTripBody(String imagename, String nameType,
String placeId) {
tableRowCount = tableRowCount + 1;
// ----------------plan_a_trip_table table
// body------------------------------------------
TableRow plan_trip_tr_data = new TableRow(this);
plan_trip_tr_data.setId(10);
//plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
plan_trip_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// select checkbox
final CheckBox select_checkbox = new CheckBox(this);
select_checkbox.setId(20);
select_checkbox.setTextColor(Color.BLACK);
select_checkbox.setGravity(Gravity.CENTER);
select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
plan_trip_tr_data.addView(select_checkbox);
// image
final ImageView place_imageview = new ImageView(this);
place_imageview.setId(20);
// test_name.setText(parser.getValue(e, KEY_SETNAME));
//place_imageview.setPadding(5, 5, 5, 5);
place_imageview.setImageResource(R.drawable.planatrip);
plan_trip_tr_data.addView(place_imageview);// add the column to
// the table row
// here
// Type
final TextView name_type = new TextView(this);
name_type.setId(21);// define id that must be unique
// no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
// the text for the header
name_type.setText(Html.fromHtml(nameType));
name_type.setTextColor(Color.BLACK); // set the color
name_type.setPadding(5, 5, 5, 5); // set the padding (if
// required)
name_type.setGravity(Gravity.LEFT);
name_type.setTextSize(18);
plan_trip_tr_data.addView(name_type); // add the column
// to
// the table row
// here
// ID
final TextView place_id = new TextView(this);
place_id.setId(21);// define id that must be unique
// total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
// the text for the header
place_id.setText(placeId);
place_id.setTextColor(Color.BLACK); // set the color
place_id.setPadding(5, 5, 5, 5); // set the padding (if
// required)
place_id.setGravity(Gravity.CENTER);
place_id.setTextSize(10);
plan_trip_tr_data.addView(place_id); // add the column
// to the
// table row here
plan_a_trip_table.addView(plan_trip_tr_data,
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ----------------------On click name_type
name_type.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!select_checkbox.isChecked()) {
select_checkbox.setChecked(true);
} else {
select_checkbox.setChecked(false);
}
}
});
// ----------------------On click customer_name
}
}
The layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/city_name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="City name"
android:textSize="20sp" />
<ImageView
android:id="#+id/update_indicator_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/presence_busy"
android:layout_marginLeft="10dp"/>
</TableRow>
<TableLayout
android:id="#+id/plan_a_trip_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tableRow1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:stretchColumns="0,1,2,3">
</TableLayout>
<Button
android:id="#+id/plan_a_trip_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/plan_a_trip_table"
android:layout_centerHorizontal="true"
android:text="Plan Trip"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The output:
Expected output:
What should I do to center the checkbox, imageview and textboxes in the same line such that they do not look haphazard as in the above output?
In your code,you also need to align your TableRow in CENTER,as
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rowParams.gravity = Gravity.CENTER;
And also set layoutParams for all the child Views.
i.e. Simply,Try to change planTripBody function with below code,it will work.
public void planTripBody(String imagename, String nameType,
String placeId) {
tableRowCount = tableRowCount + 1;
// ----------------plan_a_trip_table table
// body------------------------------------------
TableRow plan_trip_tr_data = new TableRow(this);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
rowParams.gravity = Gravity.CENTER;
plan_trip_tr_data.setLayoutParams(rowParams);
plan_trip_tr_data.setId(10);
//plan_trip_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
plan_trip_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams innerParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
// select checkbox
final CheckBox select_checkbox = new CheckBox(this);
select_checkbox.setId(20);
select_checkbox.setLayoutParams(innerParams);
select_checkbox.setTextColor(Color.BLACK);
select_checkbox.setGravity(Gravity.CENTER);
select_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
plan_trip_tr_data.addView(select_checkbox);
// image
final ImageView place_imageview = new ImageView(this);
place_imageview.setId(20);
place_imageview.setLayoutParams(innerParams);
// test_name.setText(parser.getValue(e, KEY_SETNAME));
//place_imageview.setPadding(5, 5, 5, 5);
place_imageview.setImageResource(R.drawable.planatrip);
plan_trip_tr_data.addView(place_imageview);// add the column to
// the table row
// here
// Type
final TextView name_type = new TextView(this);
name_type.setLayoutParams(innerParams);
name_type.setId(21);// define id that must be unique
// no_of_types.setText(parser.getValue(e, KEY_RIGHTMARKS)); // set
// the text for the header
name_type.setText(Html.fromHtml(nameType));
name_type.setTextColor(Color.BLACK); // set the color
name_type.setPadding(5, 5, 5, 5); // set the padding (if
// required)
name_type.setGravity(Gravity.LEFT);
name_type.setTextSize(18);
plan_trip_tr_data.addView(name_type); // add the column
// to
// the table row
// here
// ID
final TextView place_id = new TextView(this);
place_id.setLayoutParams(innerParams);
place_id.setId(21);// define id that must be unique
// total_amount.setText(parser.getValue(e, KEY_WRONGMARKS)); // set
// the text for the header
place_id.setText(placeId);
place_id.setTextColor(Color.BLACK); // set the color
place_id.setPadding(5, 5, 5, 5); // set the padding (if
// required)
place_id.setGravity(Gravity.CENTER);
place_id.setTextSize(10);
plan_trip_tr_data.addView(place_id); // add the column
// to the
// table row here
plan_a_trip_table.addView(plan_trip_tr_data,
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ----------------------On click name_type
name_type.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!select_checkbox.isChecked()) {
select_checkbox.setChecked(true);
} else {
select_checkbox.setChecked(false);
}
}
});
// ----------------------On click customer_name
}
EDIT
You need to set android:gravity="center_vertical" property to TableRow to align its child vertically center.
And Use TableRow height as android:layout_height="match_parent"
and set Padding to table row as android:paddingTop="10dp",do not set Margin to Textview as
So Change your layout to below code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/city_name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City name"
android:textSize="20sp" />
<ImageView
android:id="#+id/update_indicator_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/presence_busy"
android:layout_marginLeft="10dp"/>
</TableRow>
<TableLayout
android:id="#+id/plan_a_trip_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tableRow1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:stretchColumns="0,1,2,3">
</TableLayout>
<Button
android:id="#+id/plan_a_trip_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/plan_a_trip_table"
android:layout_centerHorizontal="true"
android:text="Plan Trip"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>