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

Jithin
  • 1,745
  • 4
  • 18
  • 25

7 Answers7

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.

britzl
  • 10,132
  • 7
  • 41
  • 38
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.

Munish Thakur
  • 926
  • 1
  • 8
  • 25
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)
Mr.India
  • 674
  • 2
  • 9
  • 19
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

Community
  • 1
  • 1
smily
  • 15
  • 1
  • 5
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

Community
  • 1
  • 1
Pinki
  • 21,723
  • 16
  • 55
  • 88
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
  • 12,423
  • 14
  • 84
  • 114
CoOkie
  • 84
  • 1
  • 5
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.

Tegra Detra
  • 24,551
  • 17
  • 53
  • 78