Android layout replacing a view with another view on run time
You could replace any view at any time.
int optionId = someExpression ? R.layout.option1 : R.layout.option2;
View C = findViewById(R.id.C);
ViewGroup parent = (ViewGroup) C.getParent();
int index = parent.indexOfChild(C);
parent.removeView(C);
C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);
If you don't want to replace already existing View, but choose between option1/option2 at initialization time, then you could do this easier: set android:id
for parent layout and then:
ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
View C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);
You will have to set "index" to proper value depending on views structure. You could also use a ViewStub: add your C view as ViewStub and then:
ViewStub C = (ViewStub) findViewById(R.id.C);
C.setLayoutResource(optionId);
C.inflate();
That way you won't have to worry about above "index" value if you will want to restructure your XML layout.
Replace a view with another view dynamically in android?
You can toggle their visibility from code..Use a FrameLayout there like below..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
xmlns:fcf="http://schemas.android.com/apk/res-auto"
android:background="@drawable/bga"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#fff"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#fff"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textColor="#fff"
android:textAppearance="?android:attr/textAppearanceMedium" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<VideoView
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent" />
<imageslideractivty.FancyCoverFlow
android:id="@+id/fancyCoverFlow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
fcf:maxRotation="45"
fcf:unselectedAlpha="0.3"
fcf:unselectedSaturation="0.0"
fcf:unselectedScale="0.4"
fcf:scaleDownGravity="0.5" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:progressDrawable="@null"
android:thumb="@drawable/middle"
android:layout_marginBottom="20dp" />
</LinearLayout>
android : change layout screen in runtime
There are many ways you can achieve that.
1) you can use a ViewSwitcher. It uses two layouts and you can switch them back and forth.
Here is the tutorial on it.
2) you can override setContentView
in the activity and implement your logic in it and call it from anywhere you want. Like you can set a boolean flag in it and set the layout accordingly! you don't need to restart the activity to do that.
How to replace view programmatically in Android?
I'm not really sure that I understand what you're saying but if you simply wanna remove one instance of View from a layout and exchange it with another you could use a utility class like this:
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);
}
}
Follow-up question: What do you mean when you're saying "Very bad to duplicate and not reuse"? Are you aware of the include tag?
Replacing View in LinearLayout
you are removing from this layout ((LinearLayout) findViewById(R.id.chartView)).removeAllViewsInLayout();
but adding to this: ((LinearLayout) findViewById(R.id.linLayout)).addView(view);
replace R.id.linLayout
with R.id.chartView
Related Topics
Android Spinner:Avoid Onitemselected Calls During Initialization
Remove Underline from Links in Textview - Android
Create Custom Notification, Android
How to Pass Integer from One Activity to Another
Windowsoftinputmode="Adjustresize" Not Working with Translucent Action/Navbar
Out of Memory Error with Bitmap
How to Keep/Exclude a Particular Package Path When Using Proguard
Build and Install Unsigned APK on Device Without the Development Server
Transfer Data Between iOS and Android via Bluetooth
How to Sign an Already Compiled APK
Installation App Blocked by Play Protect
How to Set Layout_Gravity Programmatically
Create Blurry Transparent Background Effect
How to Get Current Activity's Layout and Views via Adb