2


I want to replace a view with other other by code
For example i need to change a imageview to progressBar by code.

public void changeToProgressBar(ImageView imageview,Context context){
 ProgressBar one = new ProgressBar(context);
 one.setLayoutParams(imageview.getLayoutParams());
 imageview.setVisibility(View.GONE);
 one.setVisibility(View.VISIBLE);
 //NOW I NEED TO PLACE 'one' EXACTLY THE PLACE WHERE 'imageview' WAS THERE 
 }

I need to use this many place. I know to set by sending the parent viewgroup.
Is there anyway to get the parent viewgroup from imageview
Thanks

asked Mar 2, 2011 at 9:55
1
  • the same question was asked here :) Commented Feb 14, 2016 at 22:56

7 Answers 7

10

This is trivial to do and other posters have linked to or hinted at the solution. You can get the parent of a View by View.getParent(). You need to cast the returned value to a ViewGroup (q: can a View's parent be anything else than a ViewGroup?). Once you have the parent as a ViewGroup you can do ViewGroup.removeChild(viewToRemove) to remove the old view and add the new one using ViewGroup.addChild(viewToAdd).

You might also want to add the new view at the same index as the remove view to make sure that you don't put the new view on top of or below other views. Here's a complete utility class for you to use:

import android.view.View;
import android.view.ViewGroup;
public class ViewGroupUtils {
 public static ViewGroup getParent(View view) {
 return (ViewGroup)view.getParent();
 }
 public static void removeView(View view) {
 ViewGroup parent = getParent(view);
 if(parent != null) {
 parent.removeView(view);
 }
 }
 public static void replaceView(View currentView, View newView) {
 ViewGroup parent = getParent(currentView);
 if(parent == null) {
 return;
 }
 final int index = parent.indexOfChild(currentView);
 removeView(currentView);
 removeView(newView);
 parent.addView(newView, index);
 }
}

Something to consider is that you'll lose any positioning in a relative layout when you replace one view with another. One solution to this would be to make sure that the view you want to replace is wrapped in a another view and that wrapped container view is the one that is positioned in a relative layout.

answered Apr 18, 2013 at 11:53
Sign up to request clarification or add additional context in comments.

Comments

0

Retrieve the view you would like to change by calling findViewById() from the activity level
http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
Then find the sub view you would like to change
http://developer.android.com/reference/android/view/View.html#findViewById(int)
Then use the functions from http://developer.android.com/reference/android/view/ViewGroup.html to manipulate the view.

answered Mar 2, 2011 at 10:16

Comments

0

Just as User333 described that could be one solution.. It's also possible to delete the imageview by calling yourview.removeView(imageview) and then create your progress bar and put that inside the view instead by yourview.addView(progressbar)

sjngm
13k16 gold badges90 silver badges118 bronze badges
answered Mar 2, 2011 at 10:08

4 Comments

How to get the yourview form my imageview
'yourview', is the view containing your 'imageview'.. ex. a 'LinearLayout' or some other view ?
you can also do it like this, imageview.getParent() which returns the view that is containing your imgageview..
No. getParent will only return the view not the parent viewgroup. We cannot add view to another view group
0

You can change Android Activities view from any other simple java class or in other activity.
you only need to pass current view and get your element by this view you want to change

As :

public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Setting UI
 View currentView = getWindow().getDecorView().getRootView();
 //showHeaderLayout is simple java class or it can be any activity
 changeLayout.setView(currentView);
}


Class : changeLayout

 public class changeLayout{
 public static View setView(final Activity activity, final View myView)
 {
 // myView helps to get Activity view , which we want to change.
 TextView tv = (TextView) myView.findViewById(R.id.tv); 
 tv.setText("changs Text Via other java class !!");
 }
 }

Note : Passing view makes you able to change any activity view outside an Activity.

answered Dec 28, 2012 at 13:01

Comments

0

I think following ould be the best approach as in this we don't need to set layout params.

setvisibility(Visibility.GONE)
setvisibility(Visibility.VISIBLE)
answered Jun 22, 2013 at 8:53

Comments

0

I am sure that following link not only help you, but even shows you the direction for your requirement. https://stackoverflow.com/a/3760027/3133932

answered Jan 22, 2014 at 11:14

Comments

0

For this, take both imageview and progressBar and set the visibility according to your requirements.

For example

If you want progressBar to be visible, put setvisibility(Visibility.GONE) for imageview and put setvisibility(Visibility.VISIBLE) for progressBar

answered Mar 2, 2011 at 9:59

3 Comments

Thank for your answer. I know that, but for this i need to declare the progressbar in my xml. Here i'm creating it by code
yourview.removeView(imageview) and youtview.addView(progressbar)
How to get the yourview form my imageview

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.