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);
}
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 using view injection like this. GridViews are created according to API data.
if(followup.size()>0)
{
for (Followup fData : followup)
{
String followupId= fData.get("id");
LinearLayout lLayout=(LinearLayout) findViewById(R.id.lLayout);
LinearLayout ll1= new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
TextView txtfup= new TextView(this);
txtfup.setBackground(getResources().getDrawable(R.drawable.bg));
txtfup.setTypeface(face);
txtfup.setText(" Followup " + i++);
txtfup.setTextColor(Color.WHITE);
txtfup.setGravity(Gravity.LEFT);
txtfup.setTextSize(18);
LayoutParams params = new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT
);
txtfup.setLayoutParams(params);
ll1.addView(txtfup);
TextView txtEmpty= new TextView(this);
txtEmpty.setText("");
ll1.addView(txtEmpty);
lLayout.addView(ll1);
RelativeLayout rLayout=new RelativeLayout(this);
LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams.setMargins(40, 8, 40, 30);// left, top, right, bottom
rLayout.setLayoutParams(relativeParams);
rLayout.requestLayout();
gridViewF= new GridView(this);
gridViewF.setHorizontalSpacing(10);
gridViewF.setNumColumns(5);
rLayout.addView(gridViewF);
lLayout.addView(rLayout);
//gridViewF.setOnItemClickListener(gridViewOnclickListener_followup);
//---------------------------------
RelativeLayout rLayout1=new RelativeLayout(this);
LinearLayout.LayoutParams relativeParams1 = new LinearLayout.LayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
relativeParams1.setMargins(40, 10, 40, 40);// left, top, right, bottom
rLayout1.setLayoutParams(relativeParams);
rLayout1.requestLayout();
TableRow row=new TableRow(this.getApplicationContext());
TableRow row1=new TableRow(this.getApplicationContext());
TableLayout tlayout=new TableLayout(this.getApplicationContext());
row=new TableRow(this);
row1=new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(6, 2, 6, 2);
row.setLayoutParams(tableRowParams);
row1.setLayoutParams(tableRowParams);
TextView text1=new TextView(this);
TextView text2=new TextView(this);
TextView text3=new TextView(this);
TextView text4=new TextView(this);
text1.setTypeface(face);
text2.setTypeface(face1);
text3.setTypeface(face);
text4.setTypeface(face1);
text1.setTextSize(18);
text2.setTextSize(16);
text3.setTextSize(18);
text4.setTextSize(16);
text1.setHeight(70);
text2.setHeight(70);
text3.setHeight(70);
text4.setHeight(70);
text1.setText("Description : ");
row.addView(text1);
text2.setText(fData.get("note"));
row.addView(text2);
text3.setText("Followup Date : ");
row1.addView(text3);
text4.setText(fData.get("followup_date"));
row1.addView(text4);
tlayout.addView(row);
tlayout.addView(row1);
rLayout1.addView(tlayout);
lLayout.addView(rLayout1);
//-----------------------------
photo= new ArrayList<Followup_Photos>();
for (Followup_Photos photos : followupPhoto) {
if(followupId.equalsIgnoreCase(photos.get("photo_type_id")))
photo.add(new Followup_Photos(photos.get("photo_id"), photos.get("photo_name"), photos.get("date_added"),
photos.get("photo_type_id")));
}
setGridView_Followup(photo);
Log.i(TAG, "followup photos"+ followupPhoto.toString());
Log.i(TAG, "photo array=>"+ photo.toString());
//------------------------------
LinearLayout ll4= new LinearLayout(this);
ll4.setOrientation(LinearLayout.HORIZONTAL);
TextView txtView= new TextView(this);
txtView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
txtView.setHeight(30);
ll4.addView(txtView);
}
But there is a problem when I call gridViewF.setOnItemClickListener. Because more than one gridViews are created inside the loop but are the same name gridViewF. So, may I know any suggestion, please?
Steps
Get followup size (before the loop)
Make GridView array like GridView[] gridview = new GridView[size of followup];(Dont do it inside your for loop. Do it before it)
Use for loop using int i old pattern (for ease.. may be u have a better idea) and do gridview[i] = new GridView(Context)(Inside the loop)
Now set gridview[i].setonItemClickListener(------------------------);
Thanks a lot for your suggestion, MGDroid. I solved the issues. I changed my codes as you suggested and when I create gridview dynamically, I set id for each gridview. gridview[i].setId(i). And then inside gridView.setOnItemClickListener I get that id by parent.getId(). It worked fine. Here is my codes.
final int size= followup.size();
GridView[] gridview = new GridView[size];
fphotos= new ArrayList[size];
if(followup.size()>0)
{
for(i=0;i< size;i++)
{
fphotos[i]= new ArrayList<Followup_Photos>();
gridview[i]=new GridView(this);
gridview[i].setHorizontalSpacing(5);
gridview[i].setVerticalSpacing(15);
gridview[i].setNumColumns(5);
gridview[i].setId(i);
rLayout.addView(gridview[i]);
lLayout.addView(rLayout);
//gridview[i].setOnItemClickListener(FollowupGridListener);
gridview[i].setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
imageSelectedPosition = position;
int i= parent.getId();
Log.i(TAG, "selectedGrid id=>" + i);
for(int l=0;l<size;l++)
{
if(i==l)
initiatePopupImage_followup(imageSelectedPosition, fphotos[l]);
}
}
});
for (Followup_Photos photos : followupPhoto) {
if(followupId.equalsIgnoreCase(photos.get("photo_type_id")))
fphotos[i].add(new Followup_Photos(photos.get("photo_id"), photos.get("photo_name"), photos.get("date_added"),
photos.get("photo_type_id")));
}
setGridView_Followup(fphotos[i], gridview[i]);
}
//-------------------------------
private void initiatePopupImage_followup(int p, ArrayList<Followup_Photos> photos){
try{
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.image_layout_sever,(ViewGroup)findViewById(R.id.popup_element_sever));
pwindo = new PopupWindow(layout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
pwindo.setBackgroundDrawable(new BitmapDrawable()); //this one to affect setOutsideTouchable
pwindo.setOutsideTouchable(true); //To close when touch back button
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
final ImageView myImage = (ImageView) layout.findViewById(R.id.imgView);
AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" , "image/gif" , "image/jpg"};
imageURL= photos.get(p).get("photo_name");
System.out.println("imageURL=>" + imageURL);
client.get(StaticHelper.AMAZON_BASE_URL + imageURL, new BinaryHttpResponseHandler(allowedContentTypes) {
#Override
public void onSuccess(byte[] fileData) {
Log.i("Image Download","Successful");
Bitmap bmp=BitmapFactory.decodeByteArray(fileData,0,fileData.length);
myImage.setImageBitmap(bmp);
myImage.setVisibility(View.VISIBLE);
}
});
}catch(Exception e){
e.printStackTrace();
}
}
I add one button to my application
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Button");
linLayout.addView(btn, lpView);
But actually I don't know how much buttons will I have, how to generate it in cycle. I mean, how to do different name:
Button btn1 = new Button(this);
Button btn2 = new Button(this); Button
btn3 = new Button(this);
You can use plain Java array of buttons:
Button[] mButtonsArray = new Button[10];
for (Button b : mButtonsArray) {
b = new Button(this);
linLayout.addView(b, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
use a Button array.
Button[] btnarray = new Button[length];
Try out this way:
public void GenerateButtons()
{
LinearLayout m_ll_layout;
public static ArrayList<Button> m_arr_btn= new ArrayList<Button>();
RelativeLayout p_rl_layout=new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = null;
layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
m_ll_layout = new LinearLayout(m_context);
m_ll_layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for (int i = 0; i < 10; i++)
{
Button m_btn = new Button(m_context);
m_btn.setLayoutParams(new LinearLayout.LayoutParams(90, LayoutParams.WRAP_CONTENT));
m_btn.setText("Button");
m_btn.setId(i);
m_btn.setOnClickListener(OnClickBtn_TimeListener);
m_arr_btn.add(m_btn);
m_ll_layout.addView(m_btn);
}
layoutParams.addRule(RelativeLayout.BELOW);
p_rl_layout.addView(m_ll_layout, layoutParams);
}
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.