I have a TabHost set up with tabcontent. However, this tabcontent doesn't take up the entire screen and a different layout is loaded at the top. How can I change the image in the layout above from within the on create of a tab activity?
Thanks,
You will not be able to access Activity A's view from Activity B; this is because it's very possible that the view will be destroyed when you launch Activity B.
What you can do however is somehow let Activity A know to change the ImageView when you come back. For example, you can have a static field or you can use setResult().
I see Three ways:
1.Share the ImageView between two activities. To do that, you can put this element as private and create a getter. Then, you just have to access this element and change the element. I'm not sure that will work.
2.preferred way
Load your image in onResume from a remote source. Then, you juste have to change this remote path from your second activity.
3.previous response
Related
Whether it is possible one layout can be called by two activities.
Say i have a textview and a button.
1) Using one activity i set the textView with a name
2) Using another activity, is it possible to display popup message when i click the button.
first activity should set the textview alone and second activity should be called when i click the button. Because, i will get the text from api webservice call in first activity and will display message in popup window using another activity using some other api webservice call.
yes you can use one layout for two activities,but in that case where you use same controls for both activites:
like activity one:
uses both one textView and Button
like activity two:
uses both one textView and Button
Yes. Two activities don't care if they are using the same layout, and in fact don't really know about it. You can use view.setVisibility to make certain views invisible or visible, and as long as you are using the ids associated with the views can make different calls on them in the activities.
So the answer is yes, you can manipulate that layout however you like in two different activities.
No problem, you can easily use same layout file for more then one activity.
First try to implement this thing in your project, and then see, what happening.
Thanks,
Sumeet.
I have 2 Activities: A,B.Layout of Activity A,has a viewgroup that user changes it's content.In Activity B,I have to show that viewgroup again,without any change,it must be a real copy of that viewgroup,so texts,colors,dimensions,order(of childs) and ... must be same.So I can not use Layout Inflater.Is it possible without creating classes of the type of childrens of that viewgroup and change properties?Because if I have more than 2 Activity with different viewgroups,it is very difficult to show viewgroups of each activity in last Activity.
Also I can not remove those viewgroups from their parents.
If their content will be the same, there is no point in having two different activities. You can dynamically change one activity' s content and the behavior will be the same as there were two activities. If it is really necessary, then you will have to save all the needed information to reconstruct the activity again and pass it to the newly created activity. Take a look at this.
No easy way to do this. You cant move view's between activities. So you have several options:
create a bitmap of viewGroup and show in in new activity (doesn't work is you need editable copy)
save view's hierarchi state in old activity and recreate it in new one (using fragments makes it easier).
do not create new activity. Just change some UI parts in old one not touching target ViewGroup.
Create a class that holds the configuration of your view group. Let this configuration class hold all the information related to your ViewGroup. It will hold the texts, the colors the dimensions the order and everything user has changed. Pass object of this class from Activity A to Activity B and using this, reproduce the same view by inflating the same layout.
Hope that helps.
And to answer your question, there is no other easy way of doing this.
For the ViewGroup that needs to be shared, refactor it into a Fragment named C. Then create Fragments for the sections of Activity A and B minus this shared portion. Then contain all of these Fragments within a new containing Activity (you will no longer need Activities A and B).
Fragment A and C will be the new Activity A. Fragments B and C will be the new Activity B. To transition from the first state to the second, do a FragmentTransaction adding Fragment B and removing Fragment A. Remember to add this transaction to the back stack so the back button will bring you back to the first state.
Just a general doubt ? why would you want to go for two activities why not use two fragments assign to the same view use the underlying activity to store all the changes happening in one of the fragment (View) and when the user is passing to the other view just send in the parameters to the second fragment . Thereby you are emulating to the user that it is two activities but in reality its just two fragments controlled by a single activity .
Situation
In my alarm clock app I start a NewAlarmActivity by clicking a button.
After the user has made all the selections, alarm gets set and `finish() is called.
The matter
I create a new LinearLayout object with images and text within it. That layout must be added to the screen of previous activity (to another LinearLayout placed inside a ScrollView) so the user is able to see the alarm set. Somehow I have to pass that LinearLayout object to the first activity and tell it to receive and add the object to the screen.
How can I do that?
You just need to read some documentation about startActivityForResult().
BTW, IMHO, you should not transport your LinearLayout object between activities, that's ugly.
How to manage `startActivityForResult` on Android?
Starting Activity And Getting Result
This has been asked countless times...
No You don't have to do this. Try the approach of storing your data into a database and then when going into that Activity, build your layout according to the data you have. This is a much better way than passing a layout from one Activity to antoher.
My app contains 2 screens, one having some textboxes on click of which the value in the textbox should be passed to next screen. I have done it using intent, but I want to use viewflipper for sliding effect. Can anyone tell me how to pass value from a view to another using viewflipper?
As viewflipper belongs to one activity you can just store the value in private field of the activity and then read it another view on flip event.
Use only one Activity when using a ViewFlipper.
I'd like to build an android step application so I can go back and forth between activities.
I'd like to create a base class for the step manager that includes two buttons to go forward and back, so in the child activities I don't have to recreate those controls every time. The step manager class has to take care of the switching between activities in a precise order and of passing data between them.
I tried to write some code but I can't merge the parent's layout with the child one!
FrameLayout fl = (FrameLayout)this.findViewById(R.id.frame);
LayoutInflater.from(this).inflate(rId, fl, true);
This code is in the parent class (inherited from Activity):
the FrameLayout is the place holder for the child-layout controls;
rId is the layout resource ID of a LinearLayout in the child xml layout.
I guess that the problem about that code is that I don't call the setContentView of the child layout, so anytime I use the findViewById of a child control I get a null pointer.
What is the best practice to create something like that?
Thanks, Simone.
You can use a single activity thus avoiding passing data between them and saving memory.
In this activity you can use a ViewSwitcher or (better) a ViewFlipper to let the user navigate between steps.