Display images programmatically in Android - android

I'm trying to write such a code : I have a string, and if string is "stack", I want to print s.gif, t.gif, a.gif, c.gif and k.gif.
Here is the code that I wrote :
String myString = "stack";
for(int i = 0; i < myString.length(); i++)
{
String source= null;
if(myString .charAt(i) == 'a')
{
source = "R.drawable.folder.a.gif";
}
else if (myString .charAt(i) == 'b')
{
source = "R.drawable.folder.b.gif";
}
...//it goes until z
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.MATCH_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource();// I don't know what to write here
imageView.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
setContentView(linearLayout);
}
What I'm trying to do is set source string and give it to the setImageResource(), but I failed. Can you tell me how to fix this code? Thanks.

First of all, Android has int-based resource pointers, so your source variable must be an int.
Then, simply assign it like this:
source = R.drawable.a
Put all your images in the res/drawable directory.
However, I am not sure if you will be able to use gifs like this without an library. Try it and if it does not work, use .pngs.

Change source to int instead of String
int source;
Then use this
imageView.setImageDrawable(getResources().getDrawable(source))

Related

How to get view (ImageView ) using id in android

Update
i know i can use array or list for it but i want to use an efficient way for it with out using arrays or collection thanks to all of you
special thanks to mussharapp
hi i want to access Imageview using its id
for example i have an imageview which is created using loop like
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
now i want to get all images which i have created in loop how i can get these images is there any way using id or some thing else ???
the same problem i am facing
declare a private arraylist:
private ArrayList<Imageview> images = new ArrayList<Imageview>();
then store all your views into that list:
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
images.add(mv);
}
Finally, you can get them based on position/id (since position == id):
ImageView imageView = images.get(position);
Remember to clear list when done or onDestroy().
images.clear();
Given that you are sure that the image view id's will be unique you can do something like this
//Your code
for(int i=1;i<10;i++) {
Imageview mv=new ImageView(this);
mv.setid(i);
// and so on for image properties
}
//...
List<Drawable> drawables = new ArrayList<Drawable>();
for (int i = 1; i < 10; i++) {
ImageView mv = (ImageView) findViewById(i);
drawables.add(mv.getDrawable());
}
Not sure what you are trying to do is efficient. You are probably better off with the other suggestions they have mentioned.
First you have to set Array of images with its path and then you can use it...
You can keep an array of ImageViews.
ImageView[] mImageViews = new ImageView[10];
for(int i=1;i<10;i++) {
mImageViews[i] = new ImageView(this);
}
And access these like mImageViews[2] or mImageViews[5]

Get Text from Multiple EditText with Button in Android

My problem is creating activity to enter an array manually in Android. So I tried to create multiple EditText by these code lines:
public void EnterArray(int n)
{
for(int i=0; i<n; i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
EditText txt = new EditText(this);
txt.setId(i);
layout.addView(txt, params);
}
}
The remain problem is how can I get the text from them by using the Save Button I create in the layout. Is there any resolve? I hope there's someone could help with it.
Thank you in advance ^^
You can try something like this.
int count =layout.getChildCount();
for(int i=0;i<count;i++)
{
EditText text=(EditText)layout.getChildAt(i);
String value= text.getText().toString()
}

android TextView arrays

I am making a word game in which each a user has multiple guesses, each one made up of multiple TextViews. So far my code reads:
TextView[] guess1 = new TextView[numTextViews];
guess1[0] = (TextView) findViewById(R.id.Guess1_1);
guess1[1] = (TextView) findViewById(R.id.Guess1_2);
guess1[2] = (TextView) findViewById(R.id.Guess1_3);
guess1[3] = (TextView) findViewById(R.id.Guess1_4);
guess1[4] = (TextView) findViewById(R.id.Guess1_5);
with the xml looking like:
<TextView
android:id="#+id/Guess1_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/guessChar" />...
which repeats with android:id= changing.
I am going to be repeating myself if I type out TextView[] guess2 and all its elements.
What is a better way to go about this?
Would it be better to create all the TextViews programmatically as they are so similar?
This is how you can iterate through your views without the use of ids in repetitive code:
LinearLayout ll = (LinearLayout) findViewById(R.id.layout_containing_textviews);
for (int i = 0; i < ll.getChildCount(); i++) {
if (ll.getChildAt(i).getClass() == TextView.class) {
guess1[i] = (TextView)ll.getChildAt(i);
}
}
Make sure to tweak this in case you have non-TextView views since the i index will not be consecutive in that case. You can use another counter just for the TextViews.
Now if your layout has only TextViews, you don't even need an array. You can use that layout as a container/array the way it's used in the snipped above.
Do you know what is the amount of guesses for each text view?
I would suggest you to use reflection
Class clazz = R.id.class; // get the R class
Field f = clazz.getField("Guess1_" + "1");
int id = f.getInt(null); // pass in null, since field is a static field.
TextView currcell = (TextView) findViewById(id);
in this case it will bring the Guess1_1
for you case:
for (int i =0; i < numTextViews; i++)
{
Class clazz = R.id.class;
Field f = clazz.getField("Guess1_" + Integer.toString(i+1));
int id = f.getInt(null);
guess[i] = (TextView)findViewById(id);
}
but this only bring you the first array of Guess1 you need to convert it to generic code..
so some problems can be occur.. so read it with the xml as you have right now would be the easiest way..
Edit:
If the all textView have the same attributes you can also create it programmatically
LinearLayout view = new LinearLayout(this); // create new linear layout
view.setOrientation(LinearLayout.HORIZONTAL); // optional.. so the
// view will be horizontaly
view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)); // set the layout
// height and width
for (int i = 0; i < numOf ; i ++)
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
guess[i] = new TextView();
guess[i].setLayoutParams(lp);
guess[i].setID(i+1);
}
You could either create the textViews programmatically (and use inflate if you wish to use some xml too), or you could use the getIdentifier method , for example:
private static final String ID_FORMAT="Guess1_%d";
...
for(int i=0;i<10;++i)
{
String id=String.format(FORMAT,i);
TextView tv = (TextView) findViewById(getResources().getIdentifier(id, "id", getPackageName()));
//...
}
same goes if you wish to do a loop within a loop.
If the layout has a lot of views, I would suggest using an adapterView (listView,gridView,...) instead, and avoid creation of so many views (either programmatically or by xml).

TextView & StringArray misfunction

Well i made an activity where i am creating some TextViews based on the size of a string array! But despite the fact that my string array has 4 items on it, which i tested it with debugging, the textviews that are created is only 1. If anyone has an idea about it please tell me :)
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
final TextView[] tv = new TextView[daily_lessons.length];
final LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
for (int i=0; i<daily_lessons.length; i++){
tv[i] = new TextView(this);
tv[i].setText(daily_lessons[i]);
tv[i].setTextSize(20);
tv[i].setLayoutParams(new LinearLayout.LayoutParams((int)LayoutParams.FILL_PARENT,(int) LayoutParams.WRAP_CONTENT));
tv[i].setGravity(Gravity.CENTER);
layout.addView(tv[i]);
}
If you still need an answer to this question here is what I would do.
setContentView(R.layout.program);
String[] daily_lessons = getResources().getStringArray(R.array.firstGradeLessons);
LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
fasa = (TextView) findViewById(R.id.textView1);
fasa.setText(String.valueOf(daily_lessons.length));
TextView tmpView = null;
for (int i=0; i<daily_lessons.length; i++){
tmpView = new TextView(this);
tmpView.setText(daily_lessons[i]);
tmpView.setTextSize(20);
layout.addView(tmpView , new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
I use this type of code alot for my dynamically genenerated content (obtain content from prepopulated database).
The TextView seems to be created but might now be visible on the GUI may be stacked over each other etc.
1.Use the layout.setOrientation(ORIENTAIION.VERTICAL) on the parent linear layout's.
2.Use the childCount() on layout to make it sure on the fact that the all 4 text views have been added to the snippet.
3.Also make sure your are not using removeALLView() etc methods for your case study to the problem case.

Reference control using string instead ID

Typical way to reference android control is something like this:
TextView tv = (TextView)findViewById(R.id.tv);
Where R.id.tv is integer referencing my xml control.
The thing is I would like to make reference using string "R.id.tv". Is that possible?
Let's say I have multiple controls:
tv1,
tv2,
tv3,
tv4,
tv5,
How would I put this into some sort of loop and interate through controls. I am thinking I would use loop counter to reference different controls. How's that to be done? Thanks.
One approach is to put the ids into an array and reference by subscript.
int[] ids = { R.id.tv1, R.id.tv2 /* etc. */ };
for (int i = 0; i < ids.length; ++i) {
TextView tv = (TextView)findViewById(ids[i]);
}
Try next
private int getIdResourceByName(String aString)
{
String packageName = "com.myProject.myPackage"; // set your package name here
int resId = getResources().getIdentifier(aString, "id", packageName);
return resId;
}
...
for (int i = 1; i<=5; i++) {
TextView tv = (TextView) findViewById(getIdResourceByName("tv" + Integer.toString(i)));
...
}
Have a look at this question:
Using findviewbyid with a string in a loop
I don't understand why you'd want to do this, it's pretty ugly, inefficient, and likely to cause maintenance issues and bugs.
Why not use a collection (e.g. ArrayList) to store references to all the controls?

Categories

Resources