How do I clone a View?
You cannot clone views, the way to do it is to inflate your View every time. Note that the XML is compiled into binary which can be parsed very efficiently.
How to create a clone from specific a view in Android?
You don't clone existing views, the system doesn't work that way. However if the view is in xml, you can inflate a new copy. LayoutInflater.from(context).inflate(layoutId, fragmentLoginBinding.getRoot())
will inflate it and add it to that parent. If you don't want to inflate the entire xml file, then you probably need to break it out into its own xml file and include one into the other with the <include>
tag
Duplicate a view programmatically from an already existing view
Apparently you cannot clone views as stated by these answers:
How do I clone a View?
How to create Clone-Duplicate View?
If you inflated the first view from XML the way to go seems to be inflating the second view from XML, too.
To inflate your view from XML put it into an extra layout file, e.g. "textview.xml"
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
Then, inflate it from XML in your onCreate():
View view = LayoutInflater.from(this).inflate(R.layout.textview, null);
myLayout.addView(view);
Android: Duplicating View element in the ViewGroup
Thanks for the answer S.D and Ivan.
After the long break I could find my own answer keeping those solutions in my mind.
The Clone method directly cannot be added in the View and adding the interface makes more complexity for the codes.
Even my requirement was to clone the view for which the image was dynamically added and source was unknown.
Some trick must be done just to copy the view,
first of all get the another instance of view and copying the properties such as drawable, background, padding, etc on the second one.
The solution was much easier by using following codes.
// Create new Instance of imageView
ImageView view = new ImageView(context);
// Get the original view
ImageView org = (ImageView)getChildAt(index);
// Copy drawable of that image
view.setImageDrawable(org.getDrawable());
// Copy Background of that image
view.setBackground(org.getBackground());
// Copy other required properties
....
// Lastly add that view
addView(view);
Related Topics
Android Manifest Restrict to Tablets
Using Onsaveinstancestate with Fragments in Backstack
How to Read CPU Frequency on Android Device
Which Can Replace Capturepicture Function
Adding Your Own SQLite Database to an Android Application
Android Studio 3.0 Manifest Error: Unknown Element <Action> Found
What's the Maximum Size for an Android Shared Preference Value
Navigation Drawer with Backword Compatibility Android
Certpathvalidatorexception:Trust Anchor for Certificate Path Not Found - Retrofit Android
Android 4.3: How to Connect to Multiple Bluetooth Low Energy Devices
Android Alarmmanager Not Waking Phone Up
How to Change the Font Size of Tabhost in Android
Memory Analyzer Tool in Android
Listen to Own Application Uninstall Event on Android
How to Add Android Support V7 Libraries in Eclipse
How to Write a Drawable Resource to a File
Android Recyclerview Findviewholderforadapterposition Returns Null