Search code examples
androidfor-loopviewfindviewbyid

I need to instantiate and modify views from a for. Is it possible to get the ids in that for base on the i?


I have the following linear layour:

<?xml version="1.0" encoding="utf-8"?>

<View
    android:id="@+id/white1"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white2"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white3"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white4"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white5"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white6"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white7"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white8"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white9"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white10"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/white_mic_value"/>
<View
    android:id="@+id/white11"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>
<View
    android:id="@+id/white12"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>
<View
    android:id="@+id/white13"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>
<View
    android:id="@+id/white14"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>
<View
    android:id="@+id/white15"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>
<View
    android:id="@+id/white16"
    android:layout_width="8dp"
    android:layout_height="30dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/half_white_mic_value"/>

and I want to use them in a for, something like:

 for(int i= 1 ; i < 17; i++){
     if(soundMeter.getAmplitude() > i*viewThreshold) {
        rootView.findViewById(R.id.white+).setBackgroundResource(R.drawable.white_mic_value);
     }else{
        rootView.findViewById(R.id.white+i).setBackgroundResource(R.drawable.half_white_mic_value);
     }
 }

I tried: int id = getResources().getIdentifier("white" + i, "view", getActivity().getPackageName()); but it crashes with the following error:

06-20 09:12:45.011: E/InputEventSender(28432): Exception dispatching finished signal.
06-20 09:12:45.011: E/MessageQueue-JNI(28432): Exception in MessageQueue callback: handleReceiveCallback
06-20 09:12:45.031: E/MessageQueue-JNI(28432): java.lang.NullPointerException

Solution

  • Classic Android problem, at least for me. What you are trying to achieve is not syntactically possible as far as I know, but there are other ways. You have at least 2 options here.

    1. Dynamically create the views in code. This would look something like the following:

      LinearLayout layout = new LinearLayout(ArmedResponseDetailsActivity.this);
      TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
      layout.setLayoutParams(layoutParams);
      for (int i = 0; i < 10; i++) {
          View v = new View(ArmedResponseDetailsActivity.this);
          TableLayout.LayoutParams viewParams = new TableLayout.LayoutParams(8, 30);
      layoutParams.rightMargin = 5;
          v.setLayoutParams(viewParams);
          v.setBackground(R.drawable.white_mic_value);
          layout.addView(v);
      }
      
    2. Use a ListView along with an ArrayAdapter.

    The more professional and preferred way would be #2. Then again, it appears that you might be using images. If that's the case, you could look at a library like Glide.