Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding image programmatically to RelativeLayout

I want to add to a linearlayout, various relative layouts via code. Each relative layouts consists of: an imageview at the left, a textview beside it to the right (exactly in the middle) and another Image at the right. I must add them using data read from a database. It must be with relativelayout because I want to use a listener over the image and a different listener over the RL.

Each relativelayout looks like this thing I have in XML.

<RelativeLayout
    android:id="@+id/relativecanal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:src="@drawable/antena3" />

       <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerVertical="true"
           android:layout_marginLeft="39dp"
           android:layout_toRightOf="@+id/imageView1"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge" />

       <ImageView
           android:id="@+id/imageView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"
           android:src="@drawable/star" />

    </RelativeLayout>

I am using this code: but I get a NullPointerException at the moments of making the addView() for the image, I tried just with adding the textView and it works.

    try {
     File sdcard = Environment.getExternalStorageDirectory();
     File file = new File(sdcard,archivoCanales);
  LinearLayout layout = (LinearLayout) findViewById(R.id.channelslistlayout);
     BufferedReader br = new BufferedReader(new FileReader(file));
     String line;



     while ((line = br.readLine()) != null) {



         RelativeLayout channel=new RelativeLayout(this);
         channel.setBackgroundColor(2);

         TextView channelName=new TextView(this);
         channelName.setText(new String(line));

         ImageView image=(ImageView)findViewById(R.drawable.animalplanet);


      LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT,
              LinearLayout.LayoutParams.WRAP_CONTENT
      );    

    RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            firstImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


         RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                secImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);


         channel.addView(image,firstImageParams);
         channel.addView(channelName,secImageParams);

         layout.addView(channel,llp);
}

 }
 catch (IOException e) {
    e.printStackTrace();
 }
like image 769
danywarner Avatar asked Dec 27 '22 01:12

danywarner


2 Answers

you are using (ImageView)findViewById(R.drawable.animalplanet) and its not a proper way to programatically do it afaik. If you want to inflate an ImageView you should do something like this:

RelativeLayout.LayoutParams imParams = 
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
imSex.setId(2);
addView(imSex,imParams);    

However, if you will only display the image without beeing clickable and loading it from a database I suggest you use a ViewBinder

like image 188
Raykud Avatar answered Jan 10 '23 10:01

Raykud


You don't make a new image, thats why you get nullpointer exception. You should either make a new or layout inflate a new.

like image 43
Warpzit Avatar answered Jan 10 '23 11:01

Warpzit