How to set columns programmatically to have dynamic with in Android? - java

I have created a table like this:
TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);
tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.LEFT);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
The result of this code is:
And I want to achieve this:
How is this possible?

With this
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
<TableRow
android:layout_weight="1"
android:gravity="center">
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_1"
android:background="#null" />
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_2"
android:background="#null"/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:scaleType="center"
android:src="#drawable/ic_3"
android:background="#null" />
</TableRow>
</TableLayout>
The three button appears aligned. I think you need gravity="center"and/or android:stretchColumns="*" in your code.
UPDATE
Try with this:
TableLayout table = new TableLayout(getApplicationContext());
table.setColumnStretchable(1, true);
table.setColumnStretchable(2, true);
table.setStretchAllColumns(true);
tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);
for (int j = 0; j < 4; j++) {
statistics = new TextView[4];
statistics[i] = new TextView(getApplicationContext());
statistics[i].setText("Text");
statistics[i].setGravity(Gravity.LEFT);
tableRow[i].addView(statistics[i]);
}
table.addView(tableRow[i]);
UPDATE
I did a mock of your problem and use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout table = new TableLayout(getApplicationContext());
table.setStretchAllColumns(true);
for (int i = 0; i<20; i++) {
TableRow[] tableRow = new TableRow[20];
tableRow[i] = new TableRow(getApplicationContext());
tableRow[i].setGravity(Gravity.CENTER);
TextView pos = new TextView(getApplicationContext());
pos.setGravity(Gravity.LEFT);
pos.setText(String.valueOf(i) + ". " + getName(i));
TextView a = new TextView(getApplicationContext());
a.setGravity(Gravity.LEFT);
a.setText("2/9");
TextView points = new TextView(getApplicationContext());
points.setGravity(Gravity.LEFT);
points.setText("2/9");
tableRow[i].addView(pos);
tableRow[i].addView(a);
tableRow[i].addView(points);
table.addView(tableRow[i]);
}
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
container.addView(table);
}
private String getName(int i) {
if (i == 2) {
return "Recooooooooooooooord";
} else if (i == 3) {
return "Recooooooord";
}
return "Fran";
}
The result is like the one you want, you can see the result here

Related

Have a problem on creating table Android Studio

I am trying to make a simple app. The program should create a table with given row and column numbers. I'm adding my Java and xml files below:
public class MainActivity extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
TableLayout table = new TableLayout(this);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainActivity.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainActivity.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
mainLayout.addView(table);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp" />
<EditText
android:id="#+id/txtColumn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
This is the error message that I got:
The specified child already has a parent. You must call removeView() on the child's parent first.
Therefore, whenever I tried to add mainLayout.removeView(table); to the code nothing happens when I click the button create. Thank you all. I will be appreciated for any kinds of comments.
You have the following errors:
1: The layout above the mainLayout layout is letting the height be match_parent. Make mainLayout not displayable.
2: The layout mainLayout is also setting the height to match_parent which is also wrong. I wonder do you understand what match_parent means?
3: The first time you add successfully, but because you are setting the height to match_parent, it makes it not visible, in fact it successfully added the table. The second click because it has been added to the table, it will crash.
The code below I have fixed for you, let's try it.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp"
android:text="3"/>
<EditText
android:id="#+id/txtColumn"
android:text="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
JAVA:
public class MainAct extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (table != null) {
mainLayout.removeView(table);
}
table = new TableLayout(getBaseContext());
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainAct.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainAct.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
if (table != null) {
mainLayout.addView(table);
}
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}

How to set TableRow width to max in Android?

I have a table defined in .xml file but rows and headers have to be added dynamically. I have a problem with fill width with table row on horizontal and vertical orientation. I have tried stretchColumns parameter but it did not help. What is the best way to do it?
table = (TableLayout) findViewById(R.id.testList);
TableRow headerRow = new TableRow(this);
JSONObject obj;
obj = new JSONObject(loadJSONFromAsset());
JSONArray records = obj.getJSONArray("tests");
ArrayList<String> headers = new ArrayList<>();
headers.add("Header 1");
headers.add("Header 2");
headers.add("Header 3");
headers.add("Header 4");
headers.add("Header 5");
for(int i = 0; i < headers.size(); i++) {
TextView header = new TextView(this);
header.setText(headers.get(i));
header.setTextColor(Color.WHITE);
header.setBackgroundColor(Color.rgb(24, 116, 205));
header.setPadding(15, 15, 15, 15);
header.setTextSize(20);
header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
header.setMaxLines(1);
headerRow.addView(header);
}
table.addView(headerRow);
and xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TableLayout
android:id="#+id/testList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</GridLayout>
Try this way. The following will make sure TableRow is aligned to full available width.
After base components, put your TableLayout in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLL"
android:orientation="vertical">
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Now in your logic, you can add according to the number of items from response. This should be in a for loop.
for(int i = 0; i < leftComponent.size(); i++)
{
//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(leftComponent[i]);
nameHead.setLayoutParams(textViewParam);
nameHead.setGravity(Gravity.CENTER);
trHead.addView(nameHead);
TextView detailHead = new TextView(context);
detailHead.setText(rightComponent[i]);
detailHead.setGravity(Gravity.CENTER);
detailHead.setLayoutParams(textViewParam);
trHead.addView(detailHead);
tableLayout.addView(trHead);
}
You may need to customize according to your requirement.

tableRow entries go off screen

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);

Setting table layout through Java dynamically

I need my screen to have a couple of text views defined in the activity layout. Below those views I want to set a table whose number of rows and columns will depend upon the user's input (dynamic).
This code is in the oncreate of the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
TableLayout tableLayout = new TableLayout(getApplicationContext());
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
But this code is taking the whole screen and I am unable to have those text views which I have defined in the activity layout.
Here's my activity_home_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
If you only want your TableLayout to below your TextViews then use LinearLayout with orientation "vertical" in activity_home_page instead of RelativeLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.attendancerecord.HomePage"
tools:ignore="MergeRootFrame"
android:orientation="vertical" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/display_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
And add mainLinear.addView(tableLayout); at the end of your loop
LinearLayout mainLinear = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < 4; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
mainLinear.addView(tableLayout);
And remove setContentView(tableLayout);
Hope this works
Add this to your activity file:
<TableLayout
android:id="#+id/table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/display_user_name"
android:layout_marginTop="15dp" >
</TableLayout>
And in your class :
TableLayout tableLayout = (TableLayout)findViewById(R.id.table1);
TableRow tableRow;
TextView textView;
for (int i = 0; i < no_days; i++) {
tableRow = new TableRow(getApplicationContext());
for (int j = 0; j < no_subjects; j++) {
textView = new TextView(getApplicationContext());
textView.setText("Hello");
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}

Alignment of imageview in tablelayout

This should be easy, how can I align the star in the middle. I'm adding the top header textviews and stars dynamically in code. I tried setting gravity on the imageview did not work.
UPDATE: I added iv.setScaleType(ImageView.ScaleType.FIT_START) now star aligns to left
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<TableLayout
android:id="#+id/TableAch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:layout_alignParentTop="true"
android:shrinkColumns="*"
android:stretchColumns="*"
>
<TableRow
android:id="#+id/RowAchTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:id="#+id/ViewAchTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Achievements"
android:gravity="center"
android:layout_span="6">
</TextView>
</TableRow>
<TableRow
android:id="#+id/RowAchHeader"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="6"
>
</TableRow>
</TableLayout>
</RelativeLayout>
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acheivements);
tbLayout = (TableLayout) this.findViewById(R.id.TableAch);
rowHeader = (TableRow) this.findViewById(R.id.RowAchHeader);
FlurryAgent.logEvent("Achievements");
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 5);
TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
params.weight = 1;
params.gravity= Gravity.CENTER;
int fontSize = 12;
TextView tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText(" ");
tv.setTextSize(fontSize);
rowHeader.addView(tv); //This is blank top left corner
for (Level level : Level.values()) { //Then we add all the headers for games
tv = new TextView(this);
tv.setText("EASY");
tv.setTextSize(fontSize);
tv.setLayoutParams(params);
rowHeader.addView(tv);
}
//Next we add the levels for each game, putting a pass image where passed
for (Games game : Games.values()) {
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(tableRowParams);
String[] words = game.name().split("(?=[A-Z])");
String name = "";
for (String word : words) {
if(word.length() < 1) continue;
name += word + "\n";
}
tv = new TextView(this);
tv.setTextSize(fontSize);
tv.setText(name);
tv.setLayoutParams(params);
newRow.addView(tv);
newRow.setWeightSum(6);
for (Level level : Level.values()) {
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_START);
iv.setLayoutParams(params);
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("EASY");
tv.setTextSize(fontSize);
if( acheivements.getPassed(level, game) )//has achievement
{
iv.setImageResource(R.drawable.star_gold_full_small);
}
else {
iv.setImageResource(R.drawable.star_gold_empty_small);
}
newRow.addView(tv);
}
tbLayout.addView(newRow);
}
setupAds(true);
}
You should do this:
TableRow.LayoutParams params = new TableRow.LayoutParams(0,WRAP_CONTENT);
you should have layout params of tablerow for textviews.
then in this params you should add weight.
params.weight = 1;
params.gravity = GRAVITY.CENTER;
and your table row should have gravity "Center" for its content (not layout gravity).
Try do this:
for (Games game : Games.values()) {
TableRow newRow = new TableRow(this);
newRow.setLayoutParams(tableRowParams);
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
params2.weight = .8f;
params2.gravity= Gravity.CENTER;
String[] words = game.name().split("(?=[A-Z])");
String name = "";
for (String word : words) {
if(word.length() < 1) continue;
name += word + "\n";
}
tv = new TextView(this);
tv.setTextSize(fontSize);
tv.setText(name);
tv.setLayoutParams(params2);
newRow.addView(tv);
newRow.setWeightSum(6);
for (Level level : Level.values()) {
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(params);
tv = new TextView(this);
tv.setLayoutParams(params);
tv.setText("EASY");
tv.setTextSize(fontSize);
if( acheivements.getPassed(level, game) )//has achievement
{
iv.setImageResource(R.drawable.star_gold_full_small);
}
else {
iv.setImageResource(R.drawable.star_gold_empty_small);
}
newRow.addView(tv);
}
tbLayout.addView(newRow);
}
In this code i have created new layout params as params2 because the textview was using weight of 1 so that your images were little right sided. i had used .8f weight for the names so now its working ok. so do as per your req.
Hope it Helps!!
Here's a minimal solution based on the above comments:
<ImageView
android:scaleType="fitCenter"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#mipmap/cross" />
The first half containes the attributes that seem to be necessary to solve the problem.

Categories

Resources