Skip to content Skip to sidebar Skip to footer

Change View Content In Activity Dynamically

My abstract class extended from Activity class consists of three Views as described in the following XML snippet:

Solution 1:

First change the view in your main layout into a view group (for example, a LinearLayout). Then you can add views to it. If you add a unique view, it will have exactly the effect you want to achieve.

classOneActivityextendsMyActivity {
   @OverrideprotectedvoidinitializeContent() {
      finalViewGroupviewGroup= (ViewGroup) findViewById(R.id.activity_content);
      viewGroup.addView(View.inflate(this, R.layout.some_view, null)); 
   }
}

In your case that should work. If your custom view group contained other views from higher up in the hierarchy, you can clean it before adding your custom view:

viewGroup.removeAllViews();

It works, I do exactly that in most of my projects.

An alternative is to look at the Fragments API, available for latest versions of the SDK.

Solution 2:

You can't override class data members in Java, use methods instead

Post a Comment for "Change View Content In Activity Dynamically"