-1

I'm a beginner in Android Studio and am trying to figure out how to edit different layouts(.xml files) with one java class(.java). I am trying to get the id of an ImageView on another layout, but the findViewById of the ImageView returned null. What do I need to do? Also, I have two layout files because I am using a popupWindow. So, I need to access the ImageView of the popupWindow.

Thanks!

Hudson
  • 60
  • 9
  • Can you elaborate on "trying to edit different layouts with one java class"? Are you trying to include multiple layouts in one activity? – TechnoBlahble Feb 26 '16 at 19:03
  • It can be achieved by using a flag to determine the layout file to load in your setContentView method but I don't think it is a good idea. – Inducesmile Feb 26 '16 at 19:04
  • @TechnoBlahble Yes, multiple layouts in one activity. I'm using a popupWindow to display the second layout. – Hudson Feb 26 '16 at 19:07
  • Take a look here http://stackoverflow.com/questions/14943862/calling-multiple-layouts-in-one-activity-on-2-different-call-backs – Gueorgui Obregon Feb 26 '16 at 19:10
  • If you want to use multiple layouts you might want to use tag in your layout and reference that layout in your activity. You can read more about it at -http://developer.android.com/training/improving-layouts/reusing-layouts.html – TechnoBlahble Feb 26 '16 at 19:10
  • @TechnoBlahble The tag just sandwiches the two layouts together, because I am using a popupWindow I don't want that window to already be up. I just need a way to access the ImageView of the popup but it returns null – Hudson Feb 26 '16 at 19:22
  • Did you try this - http://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on-android – TechnoBlahble Feb 26 '16 at 19:35
  • @TechnoBlahble Yep, tried an alert already. Functionality is too limited. – Hudson Feb 26 '16 at 19:38
  • @Hudson Describe how `AlertDialog` is limited. You can inflate a custom layout into it. – OneCricketeer Feb 26 '16 at 19:39
  • @cricket_007 I can't call an AlertDialog outside of the onCreate, which is essential to what I'm doing. – Hudson Feb 26 '16 at 19:46
  • @Hudson Sure you can... make a `dialog` field variable. Use `AlertDialog.Builder` in `onCreate` to build the dialog. Call `dialog.show()` and `dialog.hide()` and `dialog.findViewById()` wherever you need to. – OneCricketeer Feb 26 '16 at 19:53
  • @cricket_007 What is a dialog field variable? Maybe an example would help – Hudson Feb 26 '16 at 19:58
  • @Hudson https://en.wikipedia.org/wiki/Member_variable – tachyonflux Feb 26 '16 at 20:06
  • @karaokyo like this? public void dialog{//Enter further dialogbuilder code here} – Hudson Feb 26 '16 at 20:12
  • @cricket_007 Like this? public void dialog{//Enter further dialogbuilder code here} – Hudson Feb 26 '16 at 20:14
  • 1
    @Hudson Nope. I'm fairly confident that this question could be answered in 30 seconds if you had posted an [MCVE](http://stackoverflow.com/help/mcve). – tachyonflux Feb 26 '16 at 20:16
  • @karaokyo It would be nice to show me what a dialog field variable is. – Hudson Feb 26 '16 at 20:19
  • Java. Fields. Make one for the dialog. https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html – OneCricketeer Feb 26 '16 at 20:40

1 Answers1

1

It sounds like you are just using Activity's findViewById, which would search the activity's layout for your ImageView. You would need to call findViewById on your inflated popup view:

View popupView = getLayoutInflater().inflate(R.layout.popup, null);

//Get your image view from the inflated popup
ImageView image = (ImageView) popupView.findViewById(R.id.imageView);
tachyonflux
  • 20,103
  • 7
  • 48
  • 67