How to get view (ImageView ) using id in android - 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]

Related

Best way to have lots of pictures in an App? Android Studio

So I am making an app that has over 200 sunset pictures (taken by myself:P). I want to display them all via android.R.layout.simple_gallery_item I tried tossing them all in the drawable folder, then bitmapping each one and drawing it on the screen, but I get an Out of memory error.
What would be the best way to store these jpg?
The only two ways I can think of are:
A separate zip file download from the app?
Drawable File? (Using bitmap wouldn't be the right way?)
Somehow storing them in the apk then pushing them onto the device before installing the app?(If possible, because if I am correct(which I am probably not), when building the apk, everything comes together and some files morph into one)
(PS: If can't tell, I am new to Android Developing)
Thanks!
Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("Noobs");
ArrayList<Image> img = new ArrayList<Image>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier("#drawable/bm" + String.valueOf(i), null, getPackageName());
img.add(ImageView.setImageResource(imageResource));
System.out.println("Did Pic "+String.valueOf(i));
}
System.out.println("Come so far!");
ListAdapter picAdapt = new ArrayAdapter<Image>(this, android.R.layout.simple_gallery_item, img);
ListView picListView = (ListView) findViewById(R.id.gallery);
picListView.setAdapter(picAdapt);
}
}
I recommend you use picasso http://square.github.io/picasso/
or glide https://github.com/bumptech/glide
those two help you to get and optimize your images
ArrayList<Integer> img = new ArrayList<Integer>();
for (int i=1; i<3; i++) {
int imageResource = getResources().getIdentifier(mContext.getPackageName() + ":drawable/bm" + String.valueOf(i), null, null);
img.add(imageResource);
}
ImageView imageView = findViewById(R.id.<UR_ID>);
imageView.setImageResource(img.get(0));

Display images programmatically in 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))

assigning setting onClick listener imageview dynamically

I am working on an application where hundreds of Image Views are being used and I want to set the onClick listener and assignment dynamically
but unfortunately i am getting Null Pointer Exception
the code is shown below
ImageView simageView1, simageView2, simageView3, simageView4, simageView5;
ImageView[] imgViewArray = { simageView1, simageView2, simageView3,
simageView4, simageView5 };
for (int i = 0; i < imgViewArray.length; i++) {
int integere = i+1;
String imageViewName = "simageView" + integere;
Log.d("tag", "name of the ImageView are " +imageViewName);
imgViewArray[i] = (ImageView) findViewById(getResources()
.getIdentifier(imageViewName, "id", getPackageName()));
imgViewArray[i].setOnClickListener(this);
i am getting NullPointerException
Hundreds of ImageViews?
Sounds like you should use a ListView / GridView /RecyclerView with an adapter and an onItemClicked listener.
I think this would be much better.
See:
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
http://developer.android.com/guide/topics/ui/layout/gridview.html

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

Android: adding multiple Views programmatically

I want to add a LinearLayout wrapped around a TextView and Button programmatically. I want it to take a String array and then using the length of the string array, add that many TextViews each with their own button.
So first:
String [] s = { .... the values ....}
int sL = s.length;
TextView t1 = new TextView (this);
// then somehow create t2, t3... etc. matching the length of the String array.
Is this the best way to do this or is there another way to do this? For some context, it's a quiz app and I've created a list of categories inside resources as values and I'm trying to programmatically get my app to create as many TextViews as there are categories then set each TextView to each category then get each button to take the user to that category of questions.
You are starting it right, just do a for loop and add textviews to your linearlayout.
// You linearlayout in which you want your textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
String [] s = { .... the values ....}
int sL = s.length;
TextView textView = null;
// For later use if you'd like
ArrayList<TextView> tViews = new ArrayList<TextView>();
for (int i = 0; i < sL; i++)
{
textView = new TextView(this);
textView.setText(s[i]);
linearLayout.addView(textView);
tViews.add(textView);
}
There is nothing wrong with this way of doing it. If you want to use these textview later on (set text for them or something) store them in an Array of some kind. Edited code
You can do the following:
for(int i=0;i<s.length;i++){
TextView t=new TextView(this);
t.setText(s[i]);
yourLinearLayout.addView(t);
}
But I really think that using a ListView would be better for performance ;)

Categories

Resources