I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView
Related
My problem
I have some dynamically created imageViews in which I put a drawable (xml vector, SVG). Everything seems to be OK on the android emulator but when I load the app on my phone there is no image visible. I know the image is there because if I increment its size, the views on the right at on the left move to the side. So there is an image!
What I have tried
Check memory of the phone
Use android:src and app:sourceCompat for the imageViews
Convert Drawable into Bitmap
Load another image (which is doing well in another window)
Put the drawable file in another drawable-folder
Change the layout type from RelativeLayout to LinearLayout
Read all the similar posts... No one resolves my problem :(
My class RankingDialog extends from AlertDialog.Builder
My layout is a RelativeLayout but I don't think that this causes the issue...
Create the views
for (int i = 0; i < LocalNames.size(); i++){
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(layoutParams);
TextView position = new TextView(context);
TextView name = new TextView(context);
TextView record = new TextView(context);
position.setTypeface(font);
name.setTypeface(font);
record.setTypeface(font);
position.setText(i+1 + ".");
position.setGravity(Gravity.CENTER);
name.setGravity(Gravity.CENTER);
record.setGravity(Gravity.CENTER);
position.setLayoutParams(tvPosParams);
layout.addView(position);
name.setText(LocalNames.get(i));
name.setLayoutParams(tvNameParams);
layout.addView(name);
//Here is where I create and load the imageView/image
ImageView flag = new ImageView(context);
flag.setLayoutParams(imageParams);
flag.setImageResource(context.getResources().getDrawable(R.drawable.myImage);
layout.addView(flag);
record.setText(LocalPoints.get(i));
record.setLayoutParams(tvPointsParams);
layout.addView(record);
mainLinear.addView(layout);
}
----------Emulator (How it should look like)
How it looks on any real device...
I'm stuck for a couple of weeks here. I can't understand why this is happening...
I will apreciate any help
I'm developing an Android game for fun and I can't remove the space between my ImageViews in my LinearLayouts.
There isn't a defined number of ImageViews in these layouts, that's why I don't deal with XML files.
So I've created every ImageViews in my Java code and I've added them in my Layout.
But there is blank spaces between each of them : Spaces between ImageViews (Blue/red/green squares are ImageViews).
I started with a GridLayout to do that but I can't remove that blank space so I try with some LinearLayouts (Horizontal) into other LinearLayout (Vertical).
I've tried a lot of thing like setMargin and Padding to 0, create a LayoutParams to remove them, etc .. but it didn't work.
Thank you !
EDIT : here's my code
iv = new ImageView[cm.getNbRow()][cm.getNbColumn()]; //cm is the map object
LinearLayout[] linearTab = new LinearLayout[cm.getNbColumn()];
for(int i=0; i<cm.getNbRow(); i++) {
linearTab[i] = new LinearLayout(this);
linearTab[i].setOrientation(LinearLayout.HORIZONTAL);
}
for(int i=0; i<cm.getNbRow(); i++) {
for(int ii = 0; ii < cm.getNbColumn(); ii++) {
if(cm.getMap()[i][ii] == 1) {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.wall);
} else if(cm.getMap()[i][ii] == 2) {
p = new Player(i, ii, this, cm);
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.player);
} else if(cm.getMap()[i][ii] == 3) {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.stop);
} else {
iv[i][ii] = new ImageView(this);
iv[i][ii].setImageResource(R.drawable.path);
}
linearTab[i].addView(iv[i][ii]); //Horizontal LinearLayout
}
ll.addView(linearTab[i]); //Vertical LinearLayout
}
EDIT 2 : Android Studio adds transparents borders to the ImageViews (don't know why tho) but I've solved it by editing the generated image and coloring the transparents parts.
When you import an image, you need to make sure that the option "Trim" is set to "yes" and "Padding" is set to 0%. That should ensure that the image doesn't have a transparent region around it.
Have you tried with android:adjustViewBounds="true"?
I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.
I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.
Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.
What I need to do is somehow set dynamic variables that change as the loop iterates.
The code for my method is as follows:
for (int i = 0; i < cardArray.length; i++) {
LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
item.addView(child);
//Set objects from array to variables
String cardTitle = cardArray[i][0];
String cardContent = cardArray[i][1];
String cardImage = cardArray[i][2];
//Set XML elements to variables
TextView title = (TextView) findViewById(R.id.card_title);
TextView content = (TextView) findViewById(R.id.card_content);
ImageView image = (ImageView) findViewById(R.id.card_image);
//Load variable content into card layout
title.setText(cardTitle);
content.setText(cardContent);
image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
}
What I thought I could do was to set the views like:
TextView title[i] = (TextView) findViewById(R.id.card_title);
however this doesn't work. Does anyone know how I can acheive this?
You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:
TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);
You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.
You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:
Change:
TextView title = (TextView) findViewById(R.id.card_title);
By;
TextView title = (TextView) child.findViewById(R.id.card_title);
I'm trying to set a font across all TextView's by iterating through the LinearLayout's views and using instanceof.
The form currently consists of 4 TextView's and one Button.
The below code is detecting all Views, even the Button as a TextView?
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
If I log each view's class name it returns the TextView and the Button so I know the correct controls are being picked up.
The problem is the Button and TextView's fonts are being set, and I only want the TextView's.
I have found a work around and that is to do the following but am intrigued as to why the above code does not work as expected.
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v.getClass().getName().contains("TextView")) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
Is it because both Button and TextView are of type View? If so, what would be the correct way about doing this?
Any help appreciated, thanks. Ricky.
Actually, Button is a subclass of TextView! That's why you see it as a TextView (it is also a TextView).
http://developer.android.com/reference/android/widget/Button.html
public class Button extends TextView
You could make a second if instanceof to exclude Buttons or use
if (v.getClass() == TextView.class)
But this won't match any other subclasses of TextView (if you use them).
It is simple
Button Class extends TextView Class
Check the documentaion
I have a TableLayout, in which rows are dynamically created, thus it may cross the limited screen size. To avoid this I am trying to add a scroll view after creating the table layout as:
TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
ScrollView sv = new ScrollView(this);
tl.addView(sv);
for (i = 0; i < count; i++) {
TableRow tr = new TableRow(this);
.
.
.
. //finally iam adding this table row to layout.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
submit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(),
SmsActivity.class);
// setMessageBody((String) tvf.getContentDescription());
// submit.setEnabled(false);
String messageText = (String) valueTV.getText();
String dateText = (String) labelTV.getText();
myIntent.putExtra("messageText",dateText+"\n"+message);
startActivity(myIntent);
}});
}
However the application is getting force closed if the number of rows increases more than the size of display.
My log cat shows the error as
06-28 14:41:49.533: ERROR/AndroidRuntime(1256): Caused by: java.lang.StringIndexOutOfBoundsException
What is the problem and how can I fix it?
You need to add the TableLayout to the ScrollView, and not the other way around.
ScrollView can only have one child and will throw an IllegalStateException if you try to add more than one. Typical practice is a ScrollView with another view inside it containing the views over which you wish to scroll.
Having said that, you appear to be getting a different sort of IllegalStateException so I'm not 100% sure that this is the issue.
In my view, though, the real solution is to not dynamically populate a ScrollView but instead use a ListView, which is explicitly intended for just this sort of thing. It's a little more work but very rewarding in terms of performance and scalability.