In this code I'm building a method that adds the same image a number of times to a Relative Layout at runtime. However, the images are not showing up when debugging. The textview that was added is acting normally. Can someone explain how to make the images visible? Thanks in advance.
My Code:
public class TestActivity extends Activity {
ArrayClass arraysObject1 = new ArrayClass();
ArrayList<ImageView> mImages = new ArrayList<ImageView>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
//textview is working
TextView testText = new TextView(this);
testText.setText("Alle aangeboden vacatures vindt u hieronder terug.");
testText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.addView(testText);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
for(int i=0; i<arraysObject1.array1.size(); i++)
{
mImages.add(new ImageView(this));
mImages.get(i).setVisibility(View.VISIBLE);
mImages.get(i).setBackgroundResource(R.drawable.work);
mImages.get(i).setLayoutParams(imgParams);
layout.addView(mImages.get(i));
}
setContentView(layout);
}
}
Your cycle for(int i=0; i < arraysObject1.array1.size(); i++) seemed not to be executed.
arraysObject1.array1 is empty.
Related
public class TabelaResultados extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabela_resultados);
Bundle in = getIntent().getExtras();
int numero = Integer.parseInt(in.getString("Numero"));
if(in!=null) {
for (int i = 0; i < numero; i++) {
String tv1 = in.getString("Nome"+(i+1));
TextView nome = new TextView(this);
nome.setText(tv1);
nome.setId(i+1);
Button passou=new Button(this);
passou.setText("LOL");
passou.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout layout= (RelativeLayout) findViewById(R.id.tabelaLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(i == 0) {
params.addRule(RelativeLayout.BELOW, findViewById(R.id.tabelaLayout).getId());
nome.setPadding(0,0,0,30);
layout.addView(nome, params);
params2.addRule(RelativeLayout.RIGHT_OF,i+1);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,i+1);
layout.addView(passou,params2);
} else {
params.addRule(RelativeLayout.BELOW, i);
layout.addView(nome, params);
params2.addRule(RelativeLayout.RIGHT_OF,i+1);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,i+1);
layout.addView(passou,params2);
}
}
}
}
}
How can I make the button smaller , to wrap content?
I've tried several solutions and none of them worked .
I'm kind of a noob in android developing ...
I want to do it dinamically because the number of buttons depends on the number os persons I add in the previous activity.
It stays like This
EDIT: It's a relative layout.
Try replacing the original line with this:
passou.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
If this doesn't work I'm not sure what your problem is.
I am trying to dynamically generate my activity layout but for some reason I can't get my TableLayout or my TableRow to match the parent width. I am using a relative layout, then I create my TableLayout and fill it with rows before adding my TableLayout to my RelativeLayout.
Here is my onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = this;
RelativeLayout mainLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams relativeLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
mainLayout.setLayoutParams(relativeLayout);
//Create the signature views
//createPage(mainLayout);
setupPage(mainLayout);
setContentView(mainLayout);
}
Here is my setupPage() code:
private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainLayout.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);
//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));
//---- END ----
mainLayout.addView(mainTable);
}
private TableRow getRowTitle(String text)
{
TableRow row = new TableRow(context);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT
);
row.setLayoutParams(rowLayout);
row.setBackgroundResource(R.drawable.rectangle);//so i can see the outline of the row
/*TextView title = new TextView(context);
TableRow.LayoutParams editLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
);
editLayout.setMargins(15,0,0,0);
title.setLayoutParams(editLayout);
title.setText(text);
row.addView(title);*/
return row;
}
You're not setting the LayoutParams on the proper view(which most likely ends up with the default LayoutParams). In the setupPage() method you do:
mainLayout.setLayoutParams(tableLayoutParams);
instead of setting it on the TableLayout:
mainTable.setLayoutParams(tableLayoutParams);
try this -
private void setupPage(RelativeLayout mainLayout)
{
TableLayout mainTable = new TableLayout(context);
RelativeLayout.LayoutParams tableLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
mainTable.setLayoutParams(tableLayoutParams);
mainTable.setBackgroundResource(R.drawable.rectangle);
//JOB DESCRIPTION title
mainTable.addView(getRowTitle("JOB DESCRIPTION"));
//---- END ----
mainLayout.addView(mainTable);
}
I'm quite new to Android programming and stuck at the following Issue.
I developed a tab-based App by using (map-)Fragments.
Now I want to add Checkboxes dynamically (source: database) to one of the Tabs, but do not know how and to which object!
My basis-coding for the Fragment looks like this:
public class SettingsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_set, container, false);
return rootView;
}
}
And the aim is, to insert something like that (example from web):
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this);
cb.setText("check");
ll.addView(cb);
}
this.setContentView(sv);
One additional question: the Fragment-concept seems to me quite complex - is it usefull to use in context with tab-layout including maps, or is there another (more easy) approach?
Thanks in advance! Juergen
You can use this:
ScrollView sv = new ScrollView(getActivity());
sv .setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getActivity());
ll.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
cb.setText("check");
ll.addView(cb);
}
setContentView(sv);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
}
});
I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing.
If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.
This is the simplified code:
public class MessageRow extends TableRow {
public TextView tvData1;
public TextView tvData2;
public TextView tvData3;
public MessageRow(Context context) {
this(context, null);
}
public MessageRow(Context context, AttributeSet attrs) {
super(context, attrs);
tvData1 = new TextView(context);
tvData2 = new TextView(context);
tvData3 = new TextView(context);
}
public void setData(String data1, String data2, String data3) {
tvData1.setText(data1);
tvData2.setText(data2);
tvData3.setText(data3);
addView(tvData1);
addView(tvData2);
addView(tvData3);
}
}
The activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
mTable.setBackgroundColor(Color.WHITE);
populate(mTable);
hview.addView(mTable);
}
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
mHeader.addView(getColumnHeader("Data#1"));
mHeader.addView(getColumnHeader("Data#2"));
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
public void populate(TableLayout tl) {
setHeader(tl);
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mr);
}
}
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Now, I tried bypassing the getColumnHeader() function like this:
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
//mHeader.addView(getColumnHeader("Data#1"));
--> Button bt1 = new Button(getContext());
--> bt1.setText("Data#1");
--> mHeader.addView(bt1);
//mHeader.addView(getColumnHeader("Data#2"));
--> Button bt2 = getColumnHeader("Data#2");
--> mHeader.addView(bt2);
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.
Side note: I don't know if this is the best way, but to compare the two variables I just right-clicked and Copy Variable, then pasted it to notepad++ in two different files (one for bt1 and one for bt2) and used the compare plugin.
Some more info. I managed to display the buttons, further debug showed that setting the Layout params for each button in the function getColumnHeader() caused the problem. Still I don't know why.
The button member mLayoutParams being null is OK, but when created (.setLAyoutParams()) and assigned values (for example width and height) the suddenly the button is not displayed.
This is the patch,... until I figure out why.
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//params.rightMargin = params.leftMargin = 2;
//bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.
The final code:
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
I have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them.
How can it be done dynamically?
LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
The easiest way is to create a layout in xml and inflate it using
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
You may also want to setId() your added views so you can access them easily later on.
I solved it using RelativeLayout which I found a little easier to work with. Yes of-course like the guys pointed out above I used setId(). Here is the code I implemented:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
//Parent RelativeLayout
parentLayout = new RelativeLayout(this);
parentLayout.setBackgroundColor(Color.WHITE);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parentLayout.setLayoutParams(params);
sv.addView(parentLayout);
final String[] comList = getCommunication();
int listLength=0;
try{
listLength= comList.length/3;
}catch(Exception e){System.out.println(e);System.exit(0);}
childLayout= new RelativeLayout[listLength] ;
TextView[] tvName = new TextView[listLength];
TextView[] tvDate =new TextView[listLength];
TextView[] tvMsg =new TextView[listLength];
for(int i =0;i<listLength;i++){
try{
childLayout[i] = new RelativeLayout(this);
childLayout[i].setPadding(5, 5, 5, 5);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
if(i==0){params.addRule(RelativeLayout.BELOW);}
else{params.addRule(RelativeLayout.BELOW,i);}
childLayout[i].setId(i+1);
childLayout[i].setClickable(true);
childLayout[i].setLayoutParams(params);
childLayout[i].setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Create the intent
Intent i = new Intent("ACTIIVTY");
startActivity(i);
}
});
tvName[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tvName[i].setLayoutParams(params);
childLayout[i].addView(tvName[i]);
if(comList[i*3].length()>24){
String name = comList[i*3].substring(0,24)+"...";
tvName[i].setText(name);
}else{
tvName[i].setText(comList[i*3]);
}
tvName[i].setId(listLength+1+i);
tvName[i].setTextSize(12);
tvName[i].setTextColor(Color.BLACK);
tvDate[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tvDate[i].setLayoutParams(params);
childLayout[i].addView(tvDate[i]);
tvDate[i].setTextSize(11);
tvDate[i].setTextColor(Color.BLUE);
tvDate[i].setText(comList[i*3+1]);
tvMsg[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, listLength+1+i);
tvMsg[i].setLayoutParams(params);
childLayout[i].addView(tvMsg[i]);
tvMsg[i].setTextSize(11);
tvMsg[i].setTextColor(Color.GRAY);
if(comList[i*3+2].length()>96){
String msg = comList[i*3+2].substring(0,96)+"...";
tvMsg[i].setText(msg);
}else{
tvMsg[i].setText(comList[i*3+2]);
}
parentLayout.addView(childLayout[i]);
}catch(Exception e){System.out.println("Errrorrrrr");}
}
setContentView(sv);
}