I've a TableLayout element in my main.xml:
<TableLayout android:id="#+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableLayout>
I don't want to hardcode the ImageView in main.xml.
How to embed the ImageView in .java ?
Hope this helps.
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int r=1; r<=rowCount; r++){
TableRow tr = new TableRow(this);
for (int c=1; c<=columnCount; c++){
ImageView im = new ImageView (this);
im.setImageDrawable(getResources().getDrawable(R.drawable.image_name));
im.setPadding(0, 0, 0, 0); //padding in each image if needed
//add here on click event etc for each image...
//...
tr.addView(im, imageWidth,imageHeight);
}
table.addView(tr);
}
Related
I'm trying to make a list of database items like the following..
for (int i=0; i < jArray.length(); i++)
{
JSONObject row = jArray.getJSONObject(i);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100));
Button btn = new Button(MainActivity.this);
//btn.setText(row.getString("subject"));
btn.setText(String.valueOf(i));
btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8));
File imgFile = new File(file_path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(myBitmap);
iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2));
ll.addView(btn);
ll.addView(iv);
LinearLayout sv = findViewById(R.id.sv_layout);
sv.addView(ll);
}
The result is like this:
But I want to have space between Linear Layouts and make Image height shorter to be equal with button height
How can I achieve this? Thank you,
Please Try this code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(0, 5, 5, 10);
ll.setLayoutParams(params);
you can add your specific margins in this line params.setMargins(0, 5, 5, 10);
Take button and image in linear layout with horizontal orientation. Set Button and Imageview height to wrap content. Now for gap between Linear Layouts give appropriate margin.
Set the margins in the layout params and pass them to your ImageView object.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
Function getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
This looks like:
I'm trying to add some EditTexts to a TableRow programmatially, yet they don't show up on the screen. If I add the EditTexts directly to the TableLayout it works fine but I do want to have them in one row afterall...
public void createPlayers(){
EditText[] editTextSpieler = new EditText[anzahl_Spieler];
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableLayout.addView(tableRow);
for(int i = 0; i < anzahl_Spieler; i++){
editTextSpieler[i] = new EditText(getActivity());
editTextSpieler[i].setLayoutParams(layoutparams);
tableRow.addView(editTextSpieler[i]);
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
}
}
Thanks in advance.
Solution:
layoutparams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
specifying MATCH_PARENT and WRAP_CONTENT with TableRow.LayoutParams did the job
Can you try this change.
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
//Move to last line in the loop.
tableRow.addView(editTextSpieler[i]);
I am trying to building a linear layout programmatically, in which, there are 4 buttons in all, each of the button simply positioned below the previous button. Just like the picture shows below:
And as you can see from the picture above, each button has exactly the same size but they have different layout_margins, the first button has a larger value in layout_marginTop while the other 3 buttons has the same value in layout_marginTop.
Basically to build a layout like this by xml is very simple but now I have really come across difficulties in building all this only through java code. I have gone through many posts and now I can easily add 4 buttons in proper sizes but I just could not find a proper way to programmatically set the layout_margin for each of the button.
To simple add four buttons, I could do like this:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4 )));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
setContentView(layout);
//setContentView(R.layout.main);
}
}
And to programmatically set margins for each button(view), I could do like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params);
But here comes the problem: as long as I setLayoutParams for the 4 buttons, and then add 4 buttons into Linearlayout by "addView(button)", only the first button could be displayed in the proper size with the proper margins. All the other 3 buttons just disappeared. And With many tests I just found it seems that only one layoutParams could be allowed in a linearlayout object. As a result, as long as I set different layout params for different buttons, only the first button could be displayed. But since here the margin params for my 4 buttons are definitely different thus I think I have to use different layoutparams for different buttons.
So pls anyone tell me how could I programmatically set the margin for my each of my buttons and make them display properlly? This has already sucked my life for two days, pls help! :)
linear = (LinearLayout) rootView.findViewById(R.id.linear);
.
.
.
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(layoutParam);
layout.setOrientation(LinearLayout.VERTICAL);
// Below will add three linear layout with 4 buttons in each
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
//Here is important
row.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < 4; j++) {
Button btnTag = new Button(getActivity());
btnTag.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btnTag.setText("Button " + (j + 1 + (i * 4)));
btnTag.setId(j + 1 + (i * 4));
row.addView(btnTag);
}
layout.addView(row);
}
linear.addView(layout);
// You can set also
// setContentView(layout)
I have the following code, which tries to set the margin:
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView name = new TextView(this);
name.setText(Html.fromHtml(venues.get(j).name + "<br><br>" + venues.get(j).getFullAddress()));
However this doesn't set the margin. What am I doing wrong here?
Edit. Whoops, my bad, TableLayout.LayoutParams is fine. Use TableRow.LayoutParams on the children of the TableRow.
I just ran this code (which is, for the most part, your code) and the margin sets correctly
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=100;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView tv = new TextView(this);
tv.setText("EXAMPLE ROW");
TableRow.LayoutParams textParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textParams);
tr.addView(tv);
tlayout.addView(tr);
isn't it like so?
TableRow tr = new TableRow(Activity.this)
And I thik you'll nee a table for that row too.
Why not go for XML ?
<TableRow
android:id="#+id/tr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>
I'm battling with Android's awful layout system. I'm trying to get a table to fill the screen (simple right?) but it's ridiculously hard.
I got it to work somehow in XML like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
However I can not get it to work in Java. I've tried a million combinations of the LayoutParams, but nothing ever works. This is the best result I have which only fills the width of the screen, not the height:
table = new TableLayout(this);
// Java. You suck.
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp); // This line has no effect! WHYYYY?!
table.setStretchAllColumns(true);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn);
}
table.addView(row);
}
Obviously the Android documentation is no help. Anyone have any ideas?
There are two mistakes in the above discussion.
It is possible to programatically set the weight by specifying TableLayout.LayoutParams and TableRow.LayoutParams and using the appropriate constructor, e.g.
TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
A widget must have the LayoutParams of its parent. Therefore, the rows must use TableLayout.LayoutParams.
This gives you the following working version of your initial code:
TableLayout table = new TableLayout(this);
// Java. You succeed!
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
setContentView(table);
Thanks to Romain Guy's comment on Android developer's forum for the solution.
Finally worked out how to do this. Gave up on TableLayout and just used horizontal LinearLayouts inside a vertical one. The critical key is to set the weight. If you specify FILL_PARENT but with the default weight, it doesn't work:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
for (int c = 0; c < 4; ++c)
{
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
row.addView(btn, lp);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
buttonsView.addView(row, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
Found the answer: apparently it is the layout_weight that makes it work and there is no way to set it from Java. Damnit.
See How can I get an Android TableLayout to fill the parent in landscape mode?
You never set the row or button layout parameters whereas in the posted xml you do that..change the details of the for loops to set both the row layout parameters and the button layout parameters than it should give the same result as your xml.
To set TableLayout LayoutParams, we logically expect to use TableLayout.LayoutParams class, but you will get a cast error stating that TableLayout.LayoutParams cannot be casted into FrameLayout.LayoutParams.
So, you should use FrameLayout.LayoutParams, if you want to programmatically set TableLayout properties. For example:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(80, 0, 0, 0);
TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail);
tableLayout.setLayoutParams(layoutParams);