0

I am trying to add a certain Relative Layout xml programmatically. It consists of an ImageView, EditText and a TextView. The problem seems to be that I am trying to do it on that "onActivityResult" method after returning from another activity. When I do it somewhere else it works and the layout I want to create appears correctly (for example I tried successfully "onCreate" and by the press of a button), but if inside "onActivityResult" doesn't work. I need it to be there.

The prints are outputted successfully in either case, there is no error or crash, but the layout just won't show. Any suggestions are appreciated.

This is the xml file I want to create:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="260dip">

    <ImageView
        android:id="@+id/damagePicture"
        android:layout_width="match_parent"
        android:layout_height="210dip"
        android:scaleType="centerCrop"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:src="@drawable/nocar"/>

    <EditText
        android:id="@+id/orderTextfield"
        android:layout_height="40dip"
        android:layout_width="match_parent"
        android:gravity="left|center"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="45dip"
        android:hint="Enter damage description"
        android:textSize="14sp"
        android:layout_alignParentLeft="true"
        android:textColor="@color/black"
        android:textColorHint="@color/gray_vin_search_text"
        android:background="@null"
        android:singleLine="true"
        android:editable="true"
        android:layout_below="@+id/damagePicture"/>

    <TextView
        android:id="@+id/removePicture"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:text="@string/discardPicture"
        android:gravity="center|center"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:onClick="removePicture"/>

    <View
        android:id="@+id/bottomLine"
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_alignParentBottom="true"
        android:background="@color/gray_for_line"></View>

</RelativeLayout>

An here are the relevant parts of the code:

public void addResultingDamagePicture(String pathToFile) {
        System.out.println("ADD RESULTING DAMAGE PICTURE");
        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
        View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, false);
        ((TextView) damagePictureSet.findViewById(R.id.removePicture)).setTypeface(fontAwesome);
        TextView remove = (TextView)damagePictureSet.findViewById(R.id.removePicture);
        remove.setTypeface(fontAwesome);
        File imgFile = new  File(pathToFile);
        if(imgFile.exists()) {
            System.out.println("ADD RESULTING DAMAGE PICTURE - FILE OK");
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inSampleSize = 4;
            final Bitmap thebmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opt);
            ((ImageView) damagePictureSet.findViewById(R.id.damagePicture)).setImageBitmap(thebmp);
            picturesCell.addView(damagePictureSet);
        } else {
            System.out.println("ADD RESULTING DAMAGE PICTURE - NO FILE EXISTS!!!");
        }
        return;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SHOOT_DAMAGE_PICTURE && resultCode == RESULT_OK) {
            final String encodedPicture = data.getStringExtra("PATH_TO_PICTURE");
            System.out.println("PICTURE RESULT = " + encodedPicture);
            addResultingDamagePicture(encodedPicture);
        } else {
            System.out.println("ON ACTIVITY RESULT = NOT OK");
            return;
        }
    }
CJ_COIMBRA
  • 319
  • 1
  • 3
  • 13
  • On first glance, you are missing to refresh your view, cf. http://stackoverflow.com/questions/5991968/how-to-force-an-entire-layout-view-refresh – TAM Apr 08 '16 at 15:39
  • You can try Hierarchy Viewer to see where the view went : https://developer.android.com/tools/help/hierarchy-viewer.html – Samuel Peter Apr 08 '16 at 16:55
  • Does `View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, true);` instead of `View damagePictureSet = layoutInflater.from(this).inflate(R.layout.damage_picture_set, picturesCell, false);` help? – jaibatrik Apr 08 '16 at 17:09
  • Thanks for the suggestion but refreshing the view as exemplified didn't make it show. @jaibatrik Thanks, I've tried that before, setting the attachToRoot param as true, then I get "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first." – CJ_COIMBRA Apr 08 '16 at 17:14
  • 1
    Another thing to check would be if `onCreate` is getting called after `onActivityResult`. – jaibatrik Apr 08 '16 at 17:19
  • @jaibatrik you are right. The onCreate is called because the other activity I am coming back from has a different orientation then forcing onCreate instead of just the onResume once I return to it. Then my layout gets wiped and restarted because of that. Thanks for pointing that out! – CJ_COIMBRA Apr 08 '16 at 17:52
  • So, I'll be greedy to post it as an answer :) – jaibatrik Apr 09 '16 at 17:47

1 Answers1

1

In some scenarios, the onCreate method can get called after onActivityResult method. In that case, the setContentView method gets called again, rendering the initial layout again. You might want to set some flags, or update your data independent of your Activity so that you can recreate the View depending on the flag / data.

jaibatrik
  • 6,770
  • 9
  • 33
  • 62