Dynamic creation of Tablelayout or nonscrollable GridView? - android

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);
}

Related

Adding Dynamically ImageViews to horizontal scrollview in android

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 );
}

creating text view programmatically(not in xml)

I want to create more then one TextView based on the condition of a for loop.
how to do it programmatically?
Template :
app name: facebook
duration: 2
app name: messaging
duration: 4
app name: temple run
duration: 2
You can just create a TextView like this:
TextView myTextView = new TextView(this);
myTextView.setText("A TextView");
then you can add this view to your layout
Add an id field in layout in xml. Take reference of layout in your class. Create views and add it to layout.
LinearLayout linearLayout = (LinearLayout) findViewById (R.id.YourID);
TextView textView = new TextView (this);
textView. setText ("YourText");
linearLayout. addView (textView);
You can use this code to create text view programmaticaly:
TextView mTextView = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mTextView.setLayoutParams(params);
rootView.addView(mTextView, 0);
What I understoond:
You have two array 1.appname 2. duration then try following code.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.YourID);
for (int i = 0; i < appname.length; i++)
{
TextView textAppname = new TextView(this);
TextView textDuration = new TextView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textAppname.setLayoutParams(params);
textDuration.setLayoutParams(params);
textAppname.setText(appname[i]);
textDuration.setText(duration[i]);
linearLayout.addView(textAppname);
linearLayout.addView(textDuration);
}

Get Views in a Table Row Android

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);

Android: How to create such layout dynamicaly (not by xml)?

In My application i want to create the layout as like below image:
So How to make it possible ?
I have done Something like below code:
public void showResult()
{
List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
for(int i = 0; i<=tempEmployerList.size()-1; i++)
{
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
innerLayout.setBackgroundColor(0x00000000);
// set the Multiple TextView
TextView mHeading = new TextView(getApplicationContext());
TextView middleValue = new TextView(getApplicationContext());
TextView aImageView = new TextView(getApplicationContext());
mHeading.setText("\n"+"1");
//mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mHeading.setTextColor(0xFFFF0000);
mHeading.setPadding(3, 0, 0, 0);
middleValue.setText("\n"+"2");
//middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
middleValue.setTextColor(0xFFFF0000);
middleValue.setGravity(Gravity.RIGHT);
middleValue.setPadding(2, 0, 9, 0);
aImageView.setText("\n"+"3");
//aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
aImageView.setGravity(Gravity.RIGHT);
aImageView.setTextColor(0xFF000000);
aImageView.setPadding(0, 0, 9, 0);
View line = new View(getApplicationContext());
//line.setOrientation(1);
line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT));
line.setBackgroundColor(0xFF000000);
/**** Any other text view setup code ****/
innerLayout.addView(mHeading);
innerLayout.addView(middleValue);
innerLayout.addView(aImageView);
innerLayout.addView(line);
myLinearLayout.addView(innerLayout);
textListWord.add(mHeading);
textListAnswer.add(middleValue);
imageListAnswer.add(aImageView);
}
}
So please guide me that what more i have to do to create such view ?
Thanks.
try TableLayout and TableRow instead of LinearLayout.
You can create TableLayout in xml as well, as it, I think would be static and add TableRows. To Create a TableLayout in Java use,
TableLayout tblLayout=new TableLayout(this);
Set LayoutParams and other properties of table layout in java, like I am setting LayoutParams:
LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content);
tblLayout.setLayoutParams(params);
Create a loop for TableRows creation and insertion:
for(int i=0;i<arrList1.size();i++)
{
TableRow row=new TableRow(this);
row.setLayoutParams(params);
TextView lbl1=new TextView(this);
lbl1.setText(arrList1.get(i));
row.addView(lbl1);
TextView lbl2=new TextView(this);
lbl2.setText(arrList2.get(i));
row.addView(lbl2);
TextView lbl3=new TextView(this);
lbl3.setText(arrList3.get(i));
row.addView(lbl3);
tblLayout.addView(row);
}

Dynamically add ImageView to TableRow

I have a TableLayout in my XML, and I'd like to dynamically add a TableRow with an ImageView to that TableLayout. What I have so far is this:
TableRow tr = new TableRow(this);
ImageView imgView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(lp);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.icon_test));
tr.addView(imgView);
tlCollection.addView(tr);
What am I doing wrong? If I want to add a Textview to the same TableRow, it works, but adding an ImageView doesn't work..
The code for adding a TextView:
TextView myTextView = new TextView(this);
myTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
myTextView.setText("Test");
tr.addView(myTextView);
Any idea?
I fixed it, using an LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow)inflater.inflate(R.layout.collection_row, tlCollection, false);
//fill textview
TextView content = (TextView)row.findViewById(R.id.txtCollection);
content.setText("Test");
//fill imageview
ImageView myImgView = ImageView)row.findViewById(R.id.imgCollection);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.my_icon));
//add row to tablelayout
tlCollection.addView(row);
I think it has to do with the layout params. You're setting both dimensions to WRAP_CONTENT but the image view doesn't have "content" per se, it has a source drawable. Try playing around with adjustViewBounds, setScaleType, and the layout params. Notice that in your example of being able to add a TextView, you're setting the width to FILL_PARENT?
The only example I can find quickly in my open projects of adding an ImageView dynamically in this way was a case in which I was using FILL_PARENT in both directions. That might not work for you since I'm not doing it exactly the same, but it's worth playing around with these settings as well as the two others mentioned above.
try this
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
tr.addView(imgView);
I Had the same problem now its fixed using the below code
CandFlag =new ImageView(this);
CandFlag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
CandFlag.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.flag));
LayoutParams layoutParams = (LayoutParams) CandFlag.getLayoutParams();
layoutParams.width = 75;
layoutParams.height = 75;
CandFlag.setLayoutParams(layoutParams);
Tablerow.addView(CandFlag);

Categories

Resources