for (int i = 0; i < listData.size(); i++) {
final TableRow tr = new TableRow(referenceActivity);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LayoutInflater inflater1 = (LayoutInflater) referenceActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowData = inflater1.inflate(R.layout.categoryadapter, null);
final ImageView lPic = (ImageView) rowData.findViewById(R.id.imageView1);
final ImageView rPic = (ImageView) rowData.findViewById(R.id.imageView2);
TextView category = (TextView) rowData.findViewById(R.id.textView_category);
tr.addView(rowData);
tv.addView(tr);
}
If i click on a row, i want to change the images in ImageView.So I want to get the all three views ( two ImageView and one TextView ) in the row.
How can i get the views in row?
set one id to you TableRow while creating and every where you want get That just get that with following code.
TableRow tr = findViewById(yourId);
and for getting your imageView
ImageView iv =(ImageView ) tr.getChildAt(0); // 0 is imageView position in tableRow
// change that with your need
and if you add View directly you can try:
ImageView iv = tr.findViewById(R.id.imageID);
Related
I have created dynamic TextViews array list added to FlowLayout. Now I have to add one EditText at left of the each TextView when click on ImageView.
Code :
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(
FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
mFlowLayout.removeAllViews();
View[] view = new View[2];
for (int i = 0; i < 2; i++) {
view[i] = LayoutInflater.from(getActivity()).inflate(R.layout.item_rich_element_edit_flow_layout
, mFlowLayout, false);
view[i].setId(i);
view[i].setTag(i);
TextView textView = view[i].findViewById(R.id.text_view_attribute_name);
ImageView imageView = view[i].findViewById(R.id.image_view_left_cursor);
imageView.setTag(i);
imageView.setOnClickListener(view1 -> addView((Integer) view1.getTag(), view1));
textView.setText(element.getElementName());
mFlowLayout.addView(view[i]);
}
I'm trying to show images dynamically by string as ID and want to display it in Horizontal scroll-view as i have 13 cards which can't be fit on screen
Here is my code its working fine but i want it in Horizontal Scroll View
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(im.getLayoutParams());
lp.setMargins(counter * 43, 0, 0, 0);//left,right,top,bottom
im.setLayoutParams(lp);
im.setImageResource(id);
im.setOnClickListener(this);
counter++;
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicksound);
}![enter image description here][2]
As per your requirement I will suggest you to RecyclerView which comes with library which you will have to add and use RecyclerView with horizontal orientation refer to link below.
https://developer.android.com/training/material/lists-cards.html
Or you can do it dynamically as below
here what you can do add Linear horizontal layout inside scrollview say id is imgcontainer then you can do is
create image view dynamically and add to linear layout.
for(int i=0;i<10;i++)
{
//create imageview here and setbg
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
((LinearLayout) findViewById(R.id.imgcontainer)).addView(
imageView, i);
}
int[] image_array = new int[]{R.drawable.image1, R.drawable.image2, ... R.drawable.image10};
LinearLayout ll = (LinearLayout) findViewById (R.id.ll_images);
for (int i=0 ; i<10; i++){
ImageView img = new ImageView (this);
img .setBackgroundResource (image_array [i]);
ll .addView(img );
}
Very simple, I'm using list view from this tutorial and I have tried using
AutoFitTextView and also AutoResizeTextView, I have made the row MATCH_PARENT for its width also height, I made the different TextViews MATCH_PARENT.
I even tried getting the screenWidth, subtracting it for the image width and making that result the width of the textView and then making it resize, and still doesn't work.
Here is my getView implemented in my CustomList class:
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TableRow tbrow = (TableRow) rowView.findViewById(R.id.idTableRow1);
tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
AutoResizeTextView tv = new AutoResizeTextView(context);
((TableRow)imageView.getParent()).addView(tv);
tv.setGravity(Gravity.CENTER);
tbrow.setGravity(Gravity.CENTER);
//Fix for android 4.0.4 versions and earlier
final String DOUBLE_BYTE_SPACE = "\u3000";
String fixString = "";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1
&& android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
fixString = DOUBLE_BYTE_SPACE;
}
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f));
tv.setText(fixString + web[position] + fixString);
imageView.setImageDrawable(imageId[position]);
if(setParameters){
imageView.setLayoutParams(params);
}
return rowView;
}
Please I'm desperate in search of this, I have looked everywhere and none helped me...
Thanks in advance.
Here the code sample:
TableLayout ll = (TableLayout)findViewById(R.id.dyn_lyr);
also same with LinearLayout
//LinearLayout ll = (LinearLayout)findViewById(R.id.dyn_lyr);
TextView tv1 = (TextView) findViewById(R.id.testEditText);
tv1.setText("SomeTextGoesHere");
for(int i=1 ; i<= 5 ; i++){
ll.addView(tv1);
}
the LinearLayout/Table is inside ScrollView!
You should create TextView Dynamically and add in ll...
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(getApplicationContext());
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
There is not enought info but I guess that Tv1 already has a parent . I guess that you should add new TextView every time . Try this one. Ok then create brand new TextView
for(int i=1 ; i<= 5 ; i++){
TextView tv1 = new TextView(this);
tv1.setText("SomeTextGoesHere");
ll.addView(tv1);
}
If you are using findViewById, the resulting view is already a part of your layout and has a parent. Each view can only be added to a ViewGroup once. You need to be making new TextViews and adding those to your LinearLayout. You can do this either via the TextView constructor or a LayoutInflater with a separate xml layout.
In my app I create scrollable view with ViewPager,RelativeLayout, and GridView,but GridView is scrollable too. But I need dinamicly create view with two rows that contains ImageView and TextView. Can I use TableLayout for this matter or there is another solution with other GridView usage?
Ya you can use table layout.try this below code.
TableRow tableRow = null;
TextView textView = null;
ImageView imageView = null;
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
RelativeLayout relativeLayout = null;
for (String string: listOfStrings)
{
tableRow = new TableRow(this);
relativeLayout = (RelativeLayout) View.inflate(this, R.layout.row, null);
textView = (TextView) relativeLayout.getChildAt(0);
textView.setText(string);
imageView= (ImageView) relativeLayout.getChildAt(1);
imageView.setBackgroundColor(R.color.blue);
TableLayout.LayoutParams rlParams = new TableLayout.LayoutParams(FILL_PARENT, WRAP_CONTE);
tableRow.addView(relativeLayout, rlParams);
tableLayout.addView(tableRow);
}