How to get a View from another activity

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to get a View from another activity

Postby Apap » Thu Jan 21, 2010 11:25 am

Hello,

How can I get a view that is setted in another activity than my current one.
I would like to do something like this :

//Activity 1 ; onCreate :
setContentView(R.layout.gallery);
findViewById(R.id.myView); //ok it works because I defined myView in the gallery.xml

//Activity 2 ; onCreate :
setContentView(R.layout.anothergallery)
findViewById(R.id.myView); // it returns null because myView is not setted in anothergallery.xml


I thought that any view can be retrieved with his id but it seems to be wrong ...
Can you help me please ?
Apap
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 18, 2010 5:31 pm

Top

Postby appforce » Thu Jan 21, 2010 1:57 pm

Hi,

Looking up with findViewById will work only on inflated and added as children Views. Can you describe better your situation because I feel that if you rephrase the question there may be a solution.

Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby Apap » Thu Jan 21, 2010 3:15 pm

Thanks for answering

Ok I try to better explain :

I have an activity where there is a galleryView, from this activity I call a second activity (with a menu button) which is transparent (so we also see in background the image selected in the galleryView ) and displays buttons on the screen. There are two buttons "previous" and "next" in order to switch the image in the galleryView, so I want to get this galleryView to do this ...
Maybe a solution is to find the first activity in the intent but I can't find how to do this ...
Apap
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 18, 2010 5:31 pm

Postby qlimax » Thu Jan 21, 2010 3:52 pm

question: why do you use another activity, just to show two buttons within transparent layout?
you can set up the buttons inside the first activity, set the visibility to false, then when you push that button in the menu you set the visibility to true...

no?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Apap » Thu Jan 21, 2010 4:47 pm

There are others buttons on the screen which made some actions about the image, and I have to do two activity because it's needed for the next step of my application ...
I think there is a way to get a view from any activity ...
Apap
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 18, 2010 5:31 pm

Postby spring » Thu Jan 21, 2010 4:49 pm

I think,you don't have to access the UI in other activity.
if i take you correctly,you can use the Intent to transfer the operate message between these Activities

there are the code sample,wish useful for you.

in you sub Activity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. protected void onCreate(Bundle savedInstanceState) {   
  2.  
  3.                 super.onCreate(savedInstanceState);
  4.  
  5.                
  6.  
  7.                 //.......other code
  8.  
  9.                
  10.  
  11.                 btnPrevious.setOnClickListener(new OnClickListener() {                 
  12.  
  13.                         @Override
  14.  
  15.                         public void onClick(View v) {
  16.  
  17.                                
  18.  
  19.                                 Bundle b = new Bundle();
  20.  
  21.                                 b.putString("operate", "previous");
  22.  
  23.                                 Intent intent = new Intent();
  24.  
  25.                                 intent.putExtras(b);
  26.  
  27.                                 setResult(RESULT_OK, intent);
  28.  
  29.                         }
  30.  
  31.                 });
  32.  
  33.                
  34.  
  35.                 btnNext.setOnClickListener(new OnClickListener() {
  36.  
  37.                        
  38.  
  39.                         @Override
  40.  
  41.                         public void onClick(View v) {
  42.  
  43.                                
  44.  
  45.                                 Bundle b = new Bundle();
  46.  
  47.                                 b.putString("operate", "next");
  48.  
  49.                                 Intent intent = new Intent();
  50.  
  51.                                 intent.putExtras(b);
  52.  
  53.                                 setResult(RESULT_OK, intent);
  54.  
  55.                         }
  56.  
  57.                 });
  58.  
  59.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



in you main Activity ,you can check the result from the sub Activity to decide which operate will be perform,in the Activity's onActivityResult method.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  4.  
  5.        
  6.  
  7.         super.onActivityResult(requestCode, resultCode, data);
  8.  
  9.        
  10.  
  11.         switch (requestCode) {
  12.  
  13.        
  14.  
  15.                 //...other request code...
  16.  
  17.        
  18.  
  19.        
  20.  
  21.                 //your sub activity code
  22.  
  23.                         case 0:{
  24.  
  25.                                 String operate = data.getExtras().getString("operate");
  26.  
  27.                                 if(operate.equals("previous")){
  28.  
  29.                                         // do your program logic
  30.  
  31.                                 }else if(operate.equals("next")){
  32.  
  33.                                         // do your program logic
  34.  
  35.                                 }
  36.  
  37.                         }
  38.  
  39.                 }
  40.  
  41.     }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Last edited by spring on Thu Jan 21, 2010 5:16 pm, edited 1 time in total.
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Top

Postby appforce » Thu Jan 21, 2010 4:54 pm

Hi,

I too find your approach a little uncommon, but if having the controls in another Activity is what you want - here's a way to do it. In the Activity with the Gallery register a BroadcastReceiver as inner class for two custom actions: "com.yourname.PREV_IMAGE" and "com.yourname.NEXT_IMAGE". Make your other Activity to fire the corresponding Intents on Button clicks and handle them in the BroadcastReceiver.



Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby appforce » Thu Jan 21, 2010 4:58 pm

One more thing - you can access UI objects in another Activity if you make it possible to get a reference to them. For example if you'll have only one instance of that Activity, make a static getInstance() method. Otherwise, think of a way to get Activity instances trough your Application object.
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby spring » Thu Jan 21, 2010 5:18 pm

:) Sorry I have a grammar error in english....

it's don't have to
not have not to :lol:
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Postby qlimax » Thu Jan 21, 2010 5:33 pm

apap
here is the "most greedy" way.
But I don't like this design... you will have a "high coupling" between the Activities
Attachments
two.zip
(45.18 KiB) Downloaded 47 times
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Apap » Fri Jan 22, 2010 11:55 am

Ok, I will use your solution qlimax (with a static attribute) even if I agree with you that it's not the best solution ...

Thanks to all of you for answering
Bye
Apap
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 18, 2010 5:31 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: field_flying and 5 guests