I'm making an layout with 400 imageView. The question is how I can set R.id.imageView>>i<<
for(int i = 1; i < 401; i++){
ImageView imageView = (ImageView)findViewById(R.id.imageView-i-);
}
for (int i = 1; i <= 400; i++) {
String s = "imageview" + i;
int id = getResources.getIdentifier(s, "id", getPackageName ());
ImageView imageview = (ImageView)findViewById (id);
}
This should work
Related
I'm using android studio and with opencv 3.4.0. I want to display a Multidimensional Array in text view. I have an a array "candidats_result" and I copied her values into a Multidimensional array " finale". after runing my code I got this result in emulator display. what I have to change in my code to get the text displayed
double [][] R_finale = new double[20][20];
int ZZ = 0;
int ZE = 0;
int EE = 0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
R_finale[i][j] = candidats_result[ZZ];
ZZ++;
}
}
String [][] finale = new String [20][20];
//showing the array in android
for(int i=0;i<19;i++) {
for (int j = 0; j < 19; j++) {
finale[i][j] = Double.toString(R_finale[i][j]);
}
}
String [][] details = new String[20][20];
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";");
}
}
textView.setText(String.valueOf(builder));
Notice below lines:
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";"); //should be builder.append(details[i][j] + ";");
}
}
I have 1 top layout and 3 bottom layouts, a user drags items from top layout to one of the 3 below.
The code below:
puts sequentially every image in every bottom layout into a list
removes items from each bottom layout
puts them back into top layout
public void onClick(View view) {
LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1);
LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2);
LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3);
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
int total_num1 = bottomLinearLayout1.getChildCount();
int total_num2 = bottomLinearLayout2.getChildCount();
int total_num3 = bottomLinearLayout3.getChildCount();
View current_image = null;
List<View> listOfkids = new ArrayList<>() ;
//************ REPEATS **************
for(int i = 0 ; i < total_num1 ; i++){
current_image = bottomLinearLayout1.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout1.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num2 ; i++){
current_image = bottomLinearLayout2.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout2.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num3 ; i++){
current_image = bottomLinearLayout3.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout3.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
Basically, the only difference between these loops is the last digit in "bottomLinearLayout"; the other code just copies itself!
Can i do this:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
and then add this string as a command right into the java source code?
is this possible?
Assuming that you want to refactor the onClick method, you could create an array of the layout ids and iterate through them as follows:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}
I have a requirement that asks for a looping of 100 images that are around 13kb each. At first I tried to use AnimationDrable and load them programatically like this:
public void animationPlay(){
frameAnimation = new AnimationDrawable();
String index = "00000";
for(int i = 1; i <= 100; i++)
{
if(i<10 ){
index = "0000" + i;
}else if(i< 100){
index = "000" + i;
}else if(i< 10000){
index = "00" + i;
}
String finalString = "acting_10_confetti" + index;
int frameIdentifier = getResources().getIdentifier(finalString, "drawable", getPackageName());
frameAnimation.addFrame(getResources().getDrawable(frameIdentifier), 70);
}
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setBackground(frameAnimation);
frameAnimation.start();
}
The problem is as soon as I add the 10th frame, the app crashes with Out of Memory Error.
Has anyone came across this problem before?
this is my code
LinearLayout ll[] = new LinearLayout [9];
ll[0]=(LinearLayout)findViewById(R.id.rlay1);
ll[1]=(LinearLayout)findViewById(R.id.rlay2);
ll[2]=(LinearLayout)findViewById(R.id.rlay3);
ll[3]=(LinearLayout)findViewById(R.id.rlay4);
ll[4]=(LinearLayout)findViewById(R.id.rlay5);
ll[5]=(LinearLayout)findViewById(R.id.rlay6);
ll[6]=(LinearLayout)findViewById(R.id.rlay7);
ll[7]=(LinearLayout)findViewById(R.id.rlay8);
ll[8]=(LinearLayout)findViewById(R.id.rlay9);
ll[9]=(LinearLayout)findViewById(R.id.rlay10);
You could put the ids into an array:
int[] ids = new int[] {R.id.rlay1, R.id.rlay2, R.id.rlay3, R.id.rlay4, R.id.rlay5,
R.id.rlay6, R.id.rlay7, R.id.rlay8, R.id.rlay9, R.id.rlay10};
LinearLayout ll[] = new LinearLayout [10];
for (int i = 0; i < 10; i++) {
ll[i] = (LinearLayout)findViewById(ids[i]);
}
I prefer this solution over the use of Resources.getIdentifier since it does not cause resource id lookup costs at runtime.
LinearLayout ll[] = new LinearLayout [10];
int counter = 0;
for (int i = 1; i < 11; i++) {
int id = getResources().getIdentifier("rlay"+i, "id", getPackageName());
ll[counter++] = (LinearLayout)findViewById(id);
}
Add this method to your code:
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
Then change your code so:
LinearLayout ll[] = new LinearLayout [10];
Context ctx = getApplicationContext();
for (int i = 0; i < 10; i++)
{
ll[i] = (LinearLayout) findViewById(getResourceID("rlay" + (i + 1), "id", ctx));
}
If I have an array of buttons in android and want to get these buttons (findviewbyid) through a for loop how do i do this? Lets say I have in my xml defined button1, button2 and button3. How do I assign arr[0],arr[1] and arr[2] to these?
for(int a = 0; a < arr.length; a++){
arr[a] = (Button) findViewById(R.id.button[a + 1]); //Doesn't work
}
Thanks in advance!
Try to implement this:
for(int a=0; a<arr.length; a++) {
String buttonID = "btn" + a;
int resID = getResources().getIdentifier(buttonID, "id", "com.package.your_app");
// To fetch Package name, you can directly call getPackageName() instead of static string "com.package.your_app
buttons[a] = ((Button) findViewById(resID));
buttons[a].setOnClickListener(this);
}
Then this work:
for(int a = 0; a < arr.length; a++) {
String buttonId = "btn" + String.valueOf(a);
int resButton = getResources().getIdentifier(buttonId, "id", "com.package.your_app");
arr[a] = (Button) findViewById(resButton);
}