Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Align Android TextView with Table

05.10.2018 (👁6646)


 

This post shows how to align display elements in a cell to center in a table in a table.

If you first create a table in Android, then the view elements within a cell are always displayed oriented only to the left side

 

The reason for this is that an embedded TextView element in a TableRow is always first inserted in WRAP mode, that is, it is only as long as the text itself is.

Therefore, as in the design layout, one must ensure that the inner element in a cell is automatically generated as wide as the comprehensive cell element.

 

Solution:

Therefore, you always have to associate an Inner View element with the LayoutParameter at runtime

TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);


lblMonth_Text.setLayoutParams(lpMatchWidth);

 

 

 

Adjust full width

With Layout_Parameter: MATCH_PARENT

For the Alignment attribute to affect CENTER or CENTER_HORIZONTAL, the View element must also have a full width.

To do this, you have to assign MATCH_Parent to the TextView via LayoutParameter.

 

TextView lblMonth_Text = new TextView(this);
lblMonth_Text.setText(sMonth);   

lblMonth_Text.setGravity(Gravity.CENTER);
//<

TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);


lblMonth_Text.setLayoutParams(lpMatchWidth);

 

 

TableRow.LayoutParams lpMatchWidth = new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Year.setLayoutParams(lpMatchWidth);
rowHeader.addView(lblMonth_Year);
tblHeader.addView(rowHeader);

panel_Month.addView(tblHeader);
//</ Header1 >

//< Header2 >
//scroller_Vertical.setMinimumWidth(500);
TableLayout tblHeader2 = new TableLayout(this);
tblHeader2.setLayoutParams(layout_Table);

TableRow rowHeader2 =
new TableRow(this);
rowHeader2.setBackgroundColor(Color.
BLACK);

SimpleDateFormat fmtMonth=
new SimpleDateFormat("MMM");
String sMonth=fmtMonth.format(dtMonth);

TextView lblMonth_Text =
new TextView(this);
lblMonth_Text.setText(sMonth);   
//*cast integer to string
lblMonth_Text.setTextColor(Color.WHITE);
lblMonth_Text.setGravity(Gravity.
CENTER);
lblMonth_Text.setLayoutParams(lpMatchWidth);
lblMonth_Text.setBackgroundColor(Color.
BLACK);
rowHeader2.addView(lblMonth_Text);
tblHeader2.addView(rowHeader2);


panel_Month.addView(tblHeader2);

 

 

 

 

 

 

Referral Code

Complete code block

Date dtMonth = clsDate.getDate_by_Year_Month_Day(Calendar_Year, Calendar_Month, 1);

LinearLayout stack_Horizontal = findViewById(R.id.
stack_Horizontal);

LinearLayout panel_Month=
new LinearLayout(this);
panel_Month.setOrientation(LinearLayout.
VERTICAL );
LinearLayout.LayoutParams lpTable=
new LinearLayout.LayoutParams(_intTableWidth,ViewGroup.LayoutParams.WRAP_CONTENT);
panel_Month.setLayoutParams(lpTable);

TableLayout tblHeader =
new TableLayout(this);
TableLayout.LayoutParams layout_Table=
new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
tblHeader.setBackgroundColor(Color.rgb(
50,50,50));
tblHeader.setLayoutParams(layout_Table);

//< Header1 >
TableRow rowHeader = new TableRow(this);
rowHeader.setBackgroundColor(Color.
GRAY);
rowHeader.setBackground(ContextCompat.getDrawable(
context, R.drawable.border));


TextView lblMonth_Year =
new TextView(this);
lblMonth_Year.setText(String.valueOf(Calendar_Year) );   
//*cast integer to string
lblMonth_Year.setTextColor(Color.BLACK);
lblMonth_Year.setBackgroundColor(Color.
YELLOW);
lblMonth_Year.setGravity(Gravity.
CENTER_HORIZONTAL);
TableRow.LayoutParams lpMatchWidth =
new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
lblMonth_Year.setLayoutParams(lpMatchWidth);
rowHeader.addView(lblMonth_Year);
tblHeader.addView(rowHeader);

panel_Month.addView(tblHeader);
//</ Header1 >

//< Header2 >
//scroller_Vertical.setMinimumWidth(500);
TableLayout tblHeader2 = new TableLayout(this);
tblHeader2.setLayoutParams(layout_Table);

TableRow rowHeader2 =
new TableRow(this);
rowHeader2.setBackgroundColor(Color.
BLACK);

SimpleDateFormat fmtMonth=
new SimpleDateFormat("MMM");
String sMonth=fmtMonth.format(dtMonth);

TextView lblMonth_Text =
new TextView(this);
lblMonth_Text.setText(sMonth);   
//*cast integer to string
lblMonth_Text.setTextColor(Color.WHITE);
lblMonth_Text.setGravity(Gravity.
CENTER);
lblMonth_Text.setLayoutParams(lpMatchWidth);
lblMonth_Text.setBackgroundColor(Color.
BLACK);
rowHeader2.addView(lblMonth_Text);
tblHeader2.addView(rowHeader2);


panel_Month.addView(tblHeader2);
//</ Header2 >