Android Display Invisible View in Table row - android

Hey in my code I have a table which is being populated dynamically by a method that I call. The table row when added has an invisible view which I would like to display for each particular row when its clicked and all the others to remain hidden here is my code
public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
final TableRow tr1 = new TableRow(getApplicationContext());
tr1.setId(id);
tr1.setClickable(true);
tr1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0;i<lytTableData.getChildCount();i++){
tr1.getChildAt(4).setVisibility(View.INVISIBLE);
}
tr1.getChildAt(4).setVisibility(View.VISIBLE);
}
});
tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
EditText textViewItemQuantity;
TextView textViewItemName,textViewItemPrice,textViewItemAmount;
textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
iv2 = new ImageView(this);
textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);
textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
textViewItemQuantity.setBackgroundColor(android.R.color.transparent);
iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv2.setImageResource(R.drawable.delete);
iv2.setVisibility(View.INVISIBLE);
textViewItemQuantity.setOnClickListener(editTextRows);
tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Double intItemAmount = Double.parseDouble(itemAmount);
intTotalAmount = intTotalAmount + intItemAmount;
intTotalAmount2 = intTotalAmount2 + intItemAmount;
txtTotal.setText(String.valueOf(intTotalAmount));
//txtDiscount.setText("0");
txtAmount.setText(String.valueOf(intTotalAmount));
id = id + 1;
System.out.println(tr1.getId());
}
using this method I'm able to create a new row every time I call it
what could I be missing to make it work just to be clear what is not working is the displaying of the Invisible View it displays but not the way i want it. I want it to display on the row clicked and all the rest to remain invisible and when another row is clicked it is displayed and all the rest remain invisible

You can do this by defining your dynamic layout (which you want to be displayed on click) in your list view's row layout file, keep it invisible / gone by-default and mark it as visible for clicked row on onItemClickListener event of list view.
Hope this help you.
If you are using base adapter then it would be better to solve this issue.

well I was finally able to figure it out thanks to all who tried and here is the answer
public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
final TableRow tr1 = new TableRow(getApplicationContext());
tr1.setId(id);
tr1.setClickable(true);
tr1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TableRow vi = (TableRow) lytTableData.getChildAt(lytTableData.indexOfChild(tr1));
for(int i = 0;i<lytTableData.getChildCount();i++){
TableRow vi2 = (TableRow) lytTableData.getChildAt(i);
vi2.getChildAt(4).setVisibility(View.INVISIBLE);
}
vi.getChildAt(4).setVisibility(View.VISIBLE);
}
});
tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView textViewItemName,textViewItemPrice;
textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
iv2 = new ImageView(this);
textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);
textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
textViewItemQuantity.setBackgroundColor(0x0000000000);
iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
iv2.setImageResource(R.drawable.delete);
iv2.setVisibility(View.INVISIBLE);
iv2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lytTableData.removeView(tr1);
}
});
tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Double intItemAmount = Double.parseDouble(itemAmount);
intTotalAmount = intTotalAmount + intItemAmount;
//intTotalAmount2 = intTotalAmount2 + intItemAmount;
txtTotal.setText(String.valueOf(intTotalAmount));
//txtDiscount.setText("0");
txtAmount.setText(String.valueOf(intTotalAmount));
id = id + 1;
System.out.println(tr1.getId());
}

Related

How to add Margin/padding in LinearLayout programmatically?

I am trying to set padding to my linear layouts so that when I press 'create row' they have a gap between them each.
I tried adding marginTop and marginBottom to my LinearLayout but when I run the code, the layout is still spawn in together. not with a gap from the margin I set.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/verticalLayout"
android:layout_below="#id/button"
android:background="#color/colorPrimaryDark"
android:layout_marginTop="50dp"
>
(the above image shows 3 linear layouts I added by pressing create row)
Edit: I tried adding a marginRight to it, just to see if anything worked on it, and it did.
public class MainActivity extends AppCompatActivity {
LinearLayout verticallayout,horizontalLayout; //got
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticallayout = findViewById(R.id.verticalLayout); //got
}
public void createRow(View view){
createRow(); //got
}
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
private void createEditText() {
EditText editText = new EditText(this);
LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editParams.setMargins(10, 10, 10, 10);
editText.setLayoutParams(editParams);
horizontalLayout.addView(editText);
}
private void createCheckbox() { //done
CheckBox checkBox = new CheckBox(this);
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.LEFT;
checkBox.setLayoutParams(checkParams);
horizontalLayout.addView(checkBox);
}
private void createSpinner() {
Spinner spinner = new Spinner(this);
//ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
//spinner.setAdapter(spinnerArrayAdapter);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.setMargins(10, 10, 10, 10);
spinnerParams.gravity = Gravity.LEFT;
spinner.setLayoutParams(spinnerParams);
horizontalLayout.addView(spinner);
}
}
Try this one code
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
horizontalLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
hope so it will be useful for you. :)
Add margin to horizontal layout not in Vertical layout. check following code:
private void createRow() {
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalParams.topMargin = 20; //add margin here.
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout);
}

Hide and show layout on click of dynamic image

I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.

how to set textview and edittext params programmatically in a table row?

i wanted to add textview and edittext params by code.
here is my code
private void draw_table() {
// TODO Auto-generated method stub
TableLayout ll = (TableLayout) findViewById(R.id.input_table_2);
for (int i = 0; i < 2; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// setting textVIEW
textview = new TextView(this);
textview.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//textview.setPadding(0, 0, 5, 10);
//textview.setTextAppearance(this,
// android.R.style.TextAppearance_Medium);
textview.setText("Hello");
// setting editText
edittext = new EditText(this);
edittext.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
//edittext.setEms(10);
//edittext.setHint("gpa");
//edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(textview);
row.addView(edittext);
ll.addView(row, i);
}
}
but nothing appear on the screen. whats wrong in this code ?thanks in advance :)
Use TableRow.LayoutParams to set your widgets LayoutParams. Like this
textview.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
also see how add and delete rows to table layout in java programically

Adding a button to last row of TableLayout

I have added buttons to the TableLayout programmatically usin TableRow. Each row should include three buttons. If number of buttons is multiple of three there is no problem. All buttons are placed with same size. However the number of buttons for last row is less than three, its size is not matched with other buttons.
Here is my code:
tableView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams
.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT,1));
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT,1);
layoutParams.setMargins(0,20,0,20);
int numberOfButtonsForLastRow = masalar.size() - numberOfRows*3;
for (int i = 0; i < numberOfRows; i++)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<3;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
if(numberOfButtonsForLastRow>0)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<numberOfButtonsForLastRow;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
This is how the last button looks like. I want it to be same size with others. How can I do this?
Add a Layout weight in your TableRow.LayoutParams same as what you did for the first 2 rows so buttons are divided into your TableView.
TableRow.LayoutParams params = new TableRow.LayoutParams(btn.getWidth(), btn.getHeight());
You need weigths, you devide the space of one row between (0 .. 1)
TableLayout.LayoutParams rows = new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0,
1.0f / (float) NUMBUTTONS);
row.setLayoutParams(rows););

placing a taking image in a right place. Tags

At first i have a method to create view(yes i know its wrong, but it works fine for me):
public ImageView createPhotoField(final Context context,
LinearLayout reviewsLayout, Integer id, String comment, OnClickListener a, OnClickListener b, int tag) {
/* reviewLayout */
LinearLayout reviewLayout = new LinearLayout(context);
reviewLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(132,
context)));
reviewLayout.setOrientation(LinearLayout.VERTICAL);
reviewLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewEntryTopLayout */
LinearLayout reviewEntryTopLayout = new LinearLayout(context);
reviewEntryTopLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryTopLayout.setOrientation(LinearLayout.HORIZONTAL);
reviewEntryTopLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewsLayout */
reviewsLayout.addView(reviewLayout);
/* entryNumber */
ImageView imageview = new ImageView(context);
LinearLayout.LayoutParams entryNumberParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageview.setTag(tag);
entryNumberParams.setMargins(40, 0, 0, 0);
imageview.setLayoutParams(entryNumberParams);
imageview.setBackgroundResource(R.drawable.ic_bad);
imageview.setImageResource(R.drawable.ic_bad);
//entryNumber.set
imageview.setVisibility(View.GONE);
reviewLayout.addView(reviewEntryTopLayout);
//reviewEntryTopLayout.addView(entryNumber);
/* table layout */
TableLayout tl = new TableLayout(context);
tl.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
tl.setPadding(densityToPixels(20, context), 0, 0, 0);
reviewEntryTopLayout.addView(tl);
/* tableRow1 */
TableRow tableRow1 = new TableRow(context);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView comment_photo = new TextView(context);
comment_photo.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
comment_photo.setText(comment);
tableRow1.addView(imageview);
tableRow1.addView(comment_photo);
/* lightSeperatorLayout */
LinearLayout lightSeperatorLayout = new LinearLayout(context);
lightSeperatorLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(1,
context)));
lightSeperatorLayout.setOrientation(LinearLayout.VERTICAL);
lightSeperatorLayout.setBackgroundColor(Color.parseColor("#d5d5d5"));
/* reviewEntryBottomLayout */
LinearLayout reviewEntryBottomLayout = new LinearLayout(context);
reviewEntryBottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryBottomLayout.setOrientation(LinearLayout.VERTICAL);
reviewEntryBottomLayout.setGravity(Gravity.CENTER_VERTICAL);
reviewLayout.addView(reviewEntryBottomLayout);
/* tableRow3 */
TableRow tableRow3 = new TableRow(context);
tableRow3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow3.setPadding(densityToPixels(10, context),
densityToPixels(10, context), densityToPixels(10, context),
densityToPixels(10, context));
reviewEntryBottomLayout.addView(tableRow3);
/* editButton */
Button editButton = new Button(context);
TableRow.LayoutParams editButtonParams = new TableRow.LayoutParams(
Tabl
eRow.LayoutParams.WRAP_CONTENT, 150);
editButtonParams.weight = 0.5f;
editButtonParams.setMargins(15, 0, 30, 0);
editButton.setLayoutParams(editButtonParams);
editButton.setText("Take a photo");
tableRow3.addView(editButton);
/* reviewButton */
Button reviewButton = new Button(context);
TableRow.LayoutParams reviewButtonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, 150);
reviewButtonParams.weight = 0.5f;
reviewButtonParams.setMargins(30, 0, 15, 0);
reviewButton.setLayoutParams(reviewButtonParams);
reviewButton.setText("Gallery");
tableRow3.addView(reviewButto
n);
reviewLayout.addView(lightSeperatorLayout);
editButton.setOnClickListener(a);
reviewButton.setOnClickListener(b);
return imageview;
}
long story short: this method creates: an imageview, for a photo, a text field, and two buttons, to choose u take a photo, or u take it from gallery.
After calling this method below few times the view is created
image = element.createPhotoField(this, reviewsLayout,
Integer.parseInt(element_id), input_name, a, b,tag);
the problem is: i can't find a way to place a photo in a right place. I have heard about tags, but i dont know how to implement those. Could somebody more experienced give me a hand ?
Maybe i should use adapter for this ??
Use something like this in your adapter is required to achieve what you need :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
view = inflater.inflate(R.layout.youListItem, null);
holder = new ViewHolder();
holder.image=(ImageView)view.findViewById(R.id.youImageView);
view.setTag(holder);
}
else
holder=(ViewHolder)view.getTag();
holder.image.setTag(data[position]);
//to display image
holder.image.setImageDrawable(YourImageDrawable);
return view;
}
public static class ViewHolder{
//view elements of single list item
public ImageView image;
}

Categories

Resources