I am developing an android app. I need to populate my list with images. I want to insert image in table layout row by row using array. Can you guide me how to do it? Thank you.
Use this code and make changes according to your code
TableLayout table = new TableLayout(this);
for (int i = 0; i < mRows; i++) {
TableRow tr = new TableRow(mContext);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < mCols; j++) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.star_on)
tr.addView(view);
}
table.addView(tr);
}
Related
I am currently developing an android word search game. And I'm just learning Android Studio for almost a month now. I had made the ui of the game. Now my problem is the gameplay. I used a tablelayout for the textviews containing the letters for the puzzle.
Here is my createGrid() method for creating the tablelayout grid:
public void createGrid(char[][] input){
TableLayout table = (TableLayout) findViewById(R.id.mainLayout);
Typeface font = Typeface.createFromAsset(getAssets(),"kg.ttf");
for(int i = 0; i < input.length; i++){
LinearLayout rowLayout = new LinearLayout(this);
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
rowLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_HORIZONTAL);
for(int j = 0; j < input.length; j++){
TextView text = new TextView(this);
Character temp = input[i][j];
text.setText(temp.toString());
text.setPadding(20, 20, 20, 20);
text.setTextSize(25);
text.setTypeface(font);
text.setGravity(Gravity.CENTER);
row.addView(text);
}
table.addView(row);
}
}
What I want to do is make the tablelayout interactive by drawing a line when highlighting some letters.
I read this article that is all about the onDraw() and onTouchEvent() methods.
Now, I want to learn how to insert those codes on my program.
Or if that's impossible to do, any suggestions to help me improve this game?
I am currently developing an android word search game. And I'm just learning Android Studio for almost a month now. I had made the ui of the game. Now my problem is the gameplay. I used a tablelayout for the textviews containing the letters for the puzzle.
Here is my createGrid() method for creating the tablelayout grid:
public void createGrid(char[][] input){
TableLayout table = (TableLayout) findViewById(R.id.mainLayout);
Typeface font = Typeface.createFromAsset(getAssets(),"kg.ttf");
for(int i = 0; i < input.length; i++){
LinearLayout rowLayout = new LinearLayout(this);
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
rowLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_HORIZONTAL);
for(int j = 0; j < input.length; j++){
TextView text = new TextView(this);
Character temp = input[i][j];
text.setText(temp.toString());
text.setPadding(20, 20, 20, 20);
text.setTextSize(25);
text.setTypeface(font);
text.setGravity(Gravity.CENTER);
row.addView(text);
}
table.addView(row);
}
}
What I want to do is make the tablelayout interactive by drawing a line when highlighting some letters.
I read this article that is all about the onDraw() and onTouchEvent() methods.
Now, I want to learn how to insert those codes on my program.
Or if that's impossible to do, any suggestions to help me improve this game?
I need to create a table layout and add rows dynamically from Java code behind. I have already read questions here, but they are mentioning to add table rows in an already created table layout (from xml).
I need to create the table layout as well as add data to it dynamically.
Can anyone please provide some inputs?
For now, I have linear layout code in place which adds button from code behind one below the other, I need to place it under a tabular format now.
To add three buttons to TableRow use the code below
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < 10; i++)
{
TableRow tableRow = new TableRow(this);
Button button = new Button(this);
button.setText("1");
tableRow.addView(button);
button = new Button(this);
button.setText("2");
tableRow.addView(button);
button = new Button(this);
button.setText("3");
tableRow.addView(button);
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
Add the code below to your onCreate() method in you Activity class:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < 5; i++)
{
TableRow tableRow = new TableRow(this);
for (int j = 0; j < 3; j++)
{
Button button = new Button(this);
button.setText(""+j);
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
The code will add five rows with three buttons with the text 1 to 3 to the table.
Add the following code below your init() method:
for (int i = 0; i < GetGlobal.totalrow; i++) {
TableRow tbrow = new TableRow(this);
// tbrow.setLayoutParams(tableRowParams);
TextView t1v = new TextView(this);
t1v.setText(JSONParser.heading[i].replace('"', ' '));
t1v.setBackgroundResource(R.drawable.diamond_detail1);
t1v.setPadding(5, 3, 5, 3);
t1v.setMinHeight(50);
t1v.setTypeface(Typeface.SERIF);
t1v.setTextColor(Color.parseColor("#FFFFFF"));
t1v.setGravity(Gravity.FILL);
tbrow.addView(t1v);
I am creating a Android app. This Android App will have objects that are dynamic. These objects are Places with a Address or Lat/Long, and distance from current location, and a ETA. What I would like to do is add with objects on a TableLayout with borders, but I need to be able to dynamically add rows as the number of places increase.
I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?
Below is a screenshot of the TableLayout I would like:
So the object would be a place with a address, distance and direction.
but I need to be able to dynamically add rows as the number of places increase.
This isn't difficult, when you have a new object append a TableRow with the data to the TableLayout.
I understand somewhat how to do this for a fixed hardcoded number of items on the xml, but what would be the best way when the number of objects is coming from the Activity.java file?
I don't think there is a best way (or what you consider best way). You either:
Insert fake views to act as dividers. This would be easier to implement visually but it will also increase the memory consumption of your app, with bad consequences if the number of rows is big. (1)
Or use drawables for the backgrounds to simulate the borders (like nine-patch images). This would be simpler then inserting additional views but you need a bit more talent to make it look well. (2)
Some examples for your image:
(1)
private static final int DIVIDER_SIZE = 2;
// rowsCount the number of rows to add to the TableLayout
private void buildOldSchool(TableLayout table, int rowsCount) {
View divider;
for (int i = 0; i < rowsCount; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < 7; j++) {
if (j % 2 == 0) {
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
DIVIDER_SIZE, TableLayout.LayoutParams.MATCH_PARENT));
divider.setBackgroundColor(Color.GREEN);
row.addView(divider, new TableRow.LayoutParams(
DIVIDER_SIZE, TableRow.LayoutParams.MATCH_PARENT));
continue;
}
TextView tv = new TextView(this);
tv.setText("DX"); // dummy data
row.addView(tv, new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
divider.setBackgroundColor(Color.GREEN);
if (i == 0) {
table.addView(divider);
divider = new View(this);
divider.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, DIVIDER_SIZE));
divider.setBackgroundColor(Color.GREEN);
}
table.addView(row);
table.addView(divider);
}
}
(2) or with images:
private void buildWithDrawables(TableLayout table, int rowsCount) {
for (int i = 0; i < rowsCount; i++) {
TableRow row = new TableRow(this);
row.setBackgroundResource(i == 0 ? R.drawable.firstrow
: R.drawable.normalrow);
for (int j = 0; j < 3; j++) {
TextView tv = new TextView(this);
tv.setBackgroundResource(j == 2 ? R.drawable.extra
: R.drawable.cell);
tv.setText("DX");
row.addView(tv, new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
table.addView(row);
}
}
Where the images are:
R.drawable.cell:
R.drawable.extra (a visually transparent drawable which replicates the nine-patch above):
R.drawable.normalrow:
R.drawable.firstrow:
Ignore my design skills.
If your foresee a large number of rows I would advise you to use a ListView, which you could pretty easy make it to look like a table with borders.
Couldn't figure out the vertical line, but something you can build upon
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
TableLayout ll=new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
for(int i=1;i<5;i++) {
TableRow tbrow=new TableRow(this);
for(int j=1;j<=3;j++) {
TextView tv1=new TextView(this);
tv1.setText("Element :"+ i + "" + j);
tbrow.addView(tv1);
}
ll.addView(tbrow);
View v = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
v.setLayoutParams(params);
v.setBackgroundColor(getResources().getColor(android.R.color.white));
ll.addView(v);
}
hsv.addView(ll);
sv.addView(hsv);
setContentView(sv);
}
I'm trying to create a TableLayout in code. Each row in the table should have equally spaced columns. The content of each cell is the same and is just a square button (I'm trying to create a calendar).
How do I get each column to take up equal width and then take up the full width the table? Right now the columns are all the same width because they all have the same content, but columns are not expanding to fill the entire width of the table.
The following is what I have tried. I am extending the TableLayout class which is why I use "this" in the code. The lines commented out are things I tried that had no effect:
this.setStretchAllColumns(true);
TableRow row;
CalendarButton button;
for(int i = 0; i < 6; ++i)
{
row = createRow();
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//params.span = 7;
row.setLayoutParams(params);
//row.setWeightSum(7);
for(int j = 0; j < 7; ++j)
{
button = createCalendarButton();
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.span = 1;
row.addView(button, params);
}
this.addView(row);
}