So I have a layout file for a RecycleView which has expandable/collapsible viewholders.
Clicking on the header would expand/collapse the extra data. Everything looks fine in the editor. However, it would have a ghost space similar to layout_marginBottom.
First loaded appearance
Expanded
Collapsed correctly
So the editor would only display the ViewHolder as Figure 2 and 3.
However when running on a device, it would first display Figure 1, then after clicking on it to expand it, it would display Figure 2. Collapsing it once again would display Figure 3 instead of Figure 1. It would continue to display the correct figures (2 and 3).
The extra contents in Figure 2 have common parents with the header which is a ConstraintLayout.
This would happen regardless of the GONE specifiers: Programmatically or XML
Using activity.runOnUIThread would not help. Using a new Handler().onPostDelayed would only be worse as would display Figure 1 but with more unwanted space, as much as Figure 2, only without the extra info.
Here's the XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
app:cardCornerRadius="10dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/validity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<ImageButton
android:id="#+id/expand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
app:srcCompat="#drawable/ic_right" />
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#color/textDark" />
</LinearLayout>
<View
android:id="#+id/divider"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:background="#color/colorSecondaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/header" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="#+id/size"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider" />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="#+id/validity"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/date" />
<LinearLayout
android:id="#+id/validity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/size">
<ImageView
android:id="#+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:contentDescription="#string/backup_layout_help_desc"
app:srcCompat="#drawable/ic_help" />
<TextView
android:id="#+id/invalid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/backup_layout_invalid"
android:textAllCaps="true"
android:textColor="#color/red" />
</LinearLayout>
<TextView
android:id="#+id/passwords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/amount" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="#android:color/transparent"
android:text="#string/backup_layout_delete"
android:textColor="#color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/importshare"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwords"
app:layout_constraintVertical_bias="1.0" />
<LinearLayout
android:id="#+id/importshare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/validity"
app:layout_constraintVertical_bias="1.0">
<Button
android:id="#+id/commit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="Import"
android:textColor="#color/yellow" />
<ImageButton
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent"
android:contentDescription="Share"
android:padding="10dp"
app:srcCompat="#drawable/ic_share" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
And the ViewHolder (cut to show relevant parts):
public ViewHolder(View itemView, Context context) {
super(itemView);
Amount = itemView.findViewById(R.id.amount);
Date = itemView.findViewById(R.id.date);
Delete = itemView.findViewById(R.id.delete);
Divider = itemView.findViewById(R.id.divider);
ExpandCollapse = itemView.findViewById(R.id.expand);
Import = itemView.findViewById(R.id.commit);
ImportShare = itemView.findViewById(R.id.importshare);
Header = itemView.findViewById(R.id.header);
Help = itemView.findViewById(R.id.help);
Passwords = itemView.findViewById(R.id.passwords);
Share = itemView.findViewById(R.id.share);
Size = itemView.findViewById(R.id.size);
Title = itemView.findViewById(R.id.title);
Validity = itemView.findViewById(R.id.validity);
Title.setTypeface(Typeface.createFromAsset(context.getAssets(), "cera.otf"));
Header.setOnClickListener(this);
onClick(Header);
}
#Override
public void onClick(View v) {
if (expanded) collapse();
else expand();
}
public void expand() {
ExpandCollapse.animate().rotation(90).start();
Date.setVisibility(View.VISIBLE);
Size.setVisibility(View.VISIBLE);
Amount.setVisibility(View.VISIBLE);
Passwords.setVisibility(View.VISIBLE);
Divider.setVisibility(View.VISIBLE);
Validity.setVisibility(valid ? View.INVISIBLE : View.VISIBLE);
Delete.setVisibility(View.VISIBLE);
ImportShare.setVisibility(View.VISIBLE);
expanded = true;
}
public void collapse() {
ExpandCollapse.animate().rotation(0).start();
Date.setVisibility(View.GONE);
Size.setVisibility(View.GONE);
Amount.setVisibility(View.GONE);
Passwords.setVisibility(View.GONE);
Divider.setVisibility(View.GONE);
Validity.setVisibility(View.GONE);
Delete.setVisibility(View.GONE);
ImportShare.setVisibility(View.GONE);
expanded = false;
}
Kotlin
I had a very similar issue when attempting to hide elements when the keyboard is open
What would happen is when you open the keyboard you would get two resizes, One adjustment for the opening and the keyboard and another for setting the views to gone. The problem is the second adjustment would only occur when another element requests a layout so your screen would jump around very noticeably twice
When keyboard open
When you tap or move another UI element
I have found a solution for this, but only when Window soft input mode = adjust resize
See below
class MainActivity : AppCompatActivity() {
private var activityHeight = 0
private var keyboardOpen = false
private var mainScreen : ConstraintLayout? = null
private var infoText : TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_screen)
mainScreen = findViewById(R.id.mainConstraintLayout)
infoText = findViewById(R.id.mainInfoText)
//...
/* Capture initial screen size */
this#MainActivity.window.decorView.doOnNextLayout {
val displayFrame : Rect = Rect()
this#MainActivity.window.decorView.getWindowVisibleDisplayFrame(displayFrame)
activityHeight = displayFrame.height()
}
/* Check for keyboard open/close */
this#MainActivity.window.decorView.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
val drawFrame : Rect = Rect()
this#MainActivity.window.decorView.getWindowVisibleDisplayFrame(drawFrame)
val currentSize = drawFrame.height()
if(keyboardOpen){
keyboardOpen = currentSize < activityHeight
if (!keyboardOpen){
infoText?.visibility = View.VISIBLE
}
}else {
keyboardOpen = currentSize < activityHeight
if(keyboardOpen) {
infoText?.visibility = View.GONE
/* Request a new layout on your base layout so screen adjusts correctly */
mainScreen?.requestLayout()
}
}
}
}
}
Related
I am using a constraint layout in my XML file. I have a view like in the example. Imageview and textview. I want both of these to have the same action after clicking. How can I group the two together and give them an id?
xml :
<ImageView
android:id="#+id/menu_drawerLogout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
You can't have 2 components with the same id in an XML layout resource file.
Method 1
If you want both to have the same action set a common onClickListener to both like,
xml
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
inside onCreate method
ImageView imageView = findViewById(R.id.imageView);
TextView textView = findViewById(R.id.textView);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
imageView.setOnClickListener(myListener);
textView.setOnClickListener(myListener);
Method 2
You can put both the views in a container and then set a onClickListener to the container
xml
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
</LinearLayout>
inside onCreate
LinearLayout layout = findViewById(R.id.container);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
layout.setOnClickListener(myListener);
Consider the code below:
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="12dp" />
<ImageView
android:id="#+id/location"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="33dp"
android:layout_marginTop="5dp"
app:srcCompat="#drawable/openLoc" />
You can but it is not recommended.
Set ids in a way that elements can be easily identifiable by ID like android:id="#+id/txtLocation" android:id="#+id/imgLocation" it makes it easy to identify element type just by reading ID. You can make it even easier by appending layout name in beginning like android:id="#+id/profileTxtLocation". Now this will help you while coding as autocomplete feature will assist you. Just type layout name you will get the list of all layout elements, then you will type the kind of element you get the list of all asked elements(es: textViews) in layout.
My layout was inside a constraintlayout and in this case I used the constraintlayout group component.
xml :
<ImageView
android:id="#+id/img_logout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"
android:textColor="#color/black_choice"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/img_logout"
app:layout_constraintTop_toBottomOf="#+id/img_star" />
<androidx.constraintlayout.widget.Group
android:id="#+id/logout_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="img_logout,menu_drawerLogout" />
Then I created the Group.setAllOnClickListener field in MainActivity :
private fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
and
logoutGroup.setAllOnClickListener()
logOutDialog()
}
I'm trying to call setText() method of a TextView frequently to display time on a second thread. The TextView is a part of a layout which added programmatically to the MainActivity's root view. The app crashes when I try to call setText() due to the error.
NullPointerException: Attempt to invoke virtual method on a null
object reference
I have tried accessing the TextView with different approaches, but every time it threw the same NullPointerException. The only solution that I know is to add the layout I want to the activity_main.xml, but that's not quite what I want.
Here's how I inflate and add the views to a Linearlayout(id/WidgetScreen) inside the MainActivity.java and how I'm calling the setText() method.
ยดยดยด
LinearLayout WidgetScreen = (LinearLayout) findViewById(R.id.WidgetScreen);
mTime = getLayoutInflater().inflate(R.layout.widget_time, null);
mDate = (TextView) mTime.findViewById(R.id.TVdate);
mMEZ = (TextView) mTime.findViewById(R.id.TVmez);
mGMT = (TextView) mTime.findViewById(R.id.TVgmt);
WidgetScreen.addView(mTime);
// The code where I set time for TextView
while (!StopDateThread) {
final String mmTime = getformatedUST();
final String mmDate = getformatedDate();
final String mmGMT = getformatedGMT();
Log.d(TAG, "run: ");
try {
mMEZ.setText(mmTime);
mDate.setText(mmDate);
mGMT.setText(mmGMT);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
widget_time.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/WidgetTabTime"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_clock"
android:layout_marginTop="#dimen/LabelIconMarginTop"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="#dimen/LabelIconMarginStart"
android:layout_gravity="center"
android:contentDescription="CLOCK"
android:id="#+id/LabelIcon"/>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="#dimen/StrokeWidth"
android:layout_marginTop="#dimen/LabelDividerMarginTop"
android:background="#color/colorAccent"
app:layout_constraintTop_toBottomOf="#id/LabelIcon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
app:layout_constraintStart_toEndOf="#id/LabelIcon"
android:layout_marginStart="#dimen/LabelTextMarginStart"
app:layout_constraintBottom_toBottomOf="#id/LabelIcon"
android:text="#string/time"
android:textSize="#dimen/LabelText"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date:"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="#dimen/StatsMarginStart"
android:layout_marginTop="#dimen/StatsMarginTop"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintTop_toBottomOf="#+id/view" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/StatsMarginTop"
android:fontFamily="sans-serif-light"
android:text="MEZ:"
android:textColor="#color/colorAccent"
android:textSize="#dimen/StatsText"
app:layout_constraintStart_toStartOf="#+id/textView"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GMT:"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
android:layout_marginTop="#dimen/StatsMarginTop"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LST:"
android:layout_marginTop="#dimen/StatsMarginTop"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintStart_toStartOf="#+id/textView3"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/TVdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CurrentDate"
android:layout_marginEnd="#dimen/StatsMarginEnd"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintBottom_toBottomOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/TVmez"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CurrentLocalTime"
android:layout_marginEnd="#dimen/StatsMarginEnd"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintBottom_toBottomOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/TVgmt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/CurrentGMT"
android:layout_marginEnd="#dimen/StatsMarginEnd"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="02:57:15"
android:layout_marginEnd="#dimen/StatsMarginEnd"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/StatsText"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
app:layout_constraintTop_toBottomOf="#id/textView4"/>
<View
android:layout_width="match_parent"
android:layout_height="5dp"
app:layout_constraintTop_toBottomOf="#id/textView4"/>
</androidx.constraintlayout.widget.ConstraintLayout>
What would be the simplest way to access the setText() method?
you can't use findViewById() on inflated layout
you must add the inflated layout to your main layout via
mainLayout.addView(mTime);
as written here findViewById() returns null for an inflated layout
I'm building an app using Constraint layout, but it has extra space bottom and top. I can't find where they come from. I checked maybe ten times from top to bottom all code but there is no margin or padding or anything else for it.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/Brand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/brandname"
style="#style/viewParent.headerText"
android:text="Did"
android:textColor="#color/white" />
<TextView
android:id="#+id/brandname2"
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname"
android:text="You"
android:textColor="#color/yellow" />
<TextView
style="#style/viewParent.headerText2"
android:layout_toRightOf="#id/brandname2"
android:text="Know!"
android:textColor="#color/red"
android:textStyle="bold|italic" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container_main"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="4dp"
android:background="#drawable/card_bg"
android:paddingLeft="16dp"
android:paddingRight="16dp"
app:layout_constraintBottom_toTopOf="#id/guideline2"
app:layout_constraintEnd_toEndOf="#id/guidelineyazisag"
app:layout_constraintStart_toStartOf="#id/guidelineyazisol"
app:layout_constraintTop_toBottomOf="#id/guideline">
<TextView
android:id="#+id/activity_main_text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="simple Title Text"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/activity_main_did_u_know"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Category"
android:textColor="#5E4C4C"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/activity_main_image_view"
app:layout_constraintTop_toBottomOf="#id/activity_main_did_u_know">
<TextView
android:id="#+id/factTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:lineSpacingExtra="8dp"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
android:textColor="#000000"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<ImageView
android:id="#+id/activity_main_image_view"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:clickable="true"
android:src="#drawable/ic_share_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
<ImageView
android:id="#+id/activity_main_fav_button"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/ic_favorite_border_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/scrollview" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/LeftRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline2">
<ImageView
android:id="#+id/activity_main_left_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_previous"
android:elevation="3dp"
android:background="#drawable/okarka"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/activity_main_right_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:clickable="true"
android:src="#drawable/ic_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:background="#drawable/okarka"
app:layout_constraintStart_toEndOf="#+id/guideline3"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintLeft_toLeftOf="parent"
ads:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/LeftRight" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.076" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.07" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineyazisag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.93" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried adding guidelines to 0 points in the bottom but it also starts in that space. I gave -(minus) values to margin and padding but doesn't work. I don't know what else to do. You can see from screenshots how it looks.
public class MainActivity extends AppCompatActivity {
private ColorWheel colorWheel = new ColorWheel();
private TextView factTextView;
private TextView textViewId;
private TextView textViewTableName;
ImageView tableImageView;
AdView adView;
ImageView shareImageView;
ImageView favImageView;
float x1,x2,y1,y2;
int i = 1;
int adCounter = 0;
private ConstraintLayout constraintLayout;
String fact;
ArrayList<Fact> favList = new ArrayList<>();
private InterstitialAd mInterstitial;
AdRequest interAdRequest;
int favId;
String favTable;
Typeface typeface;
List<Fact> mData;
private DatabaseFacts db;
GradientDrawable shape;
#SuppressLint({"ClickableViewAccessibility", "SetTextI18n"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseFacts(this);
db.copyDbIfNotExists();
shape = new GradientDrawable();
shape.setCornerRadius(40);
mData = new ArrayList<>();
MobileAds.initialize(this, "ca-app-pub-2469721886989416~7390658870");
adView = findViewById(R.id.adView);
AdRequest bannerAdRequest = new AdRequest.Builder().build();
adView.loadAd(bannerAdRequest);
interAdRequest = new AdRequest.Builder().build();
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("ca-app-pub-2469721886989416/9441000894");
mInterstitial.loadAd(interAdRequest);
factTextView = findViewById(R.id.factTextView);
constraintLayout = (ConstraintLayout) findViewById(R.id.container_main);
textViewId = findViewById(R.id.activity_main_text_view_id);
favImageView = findViewById(R.id.activity_main_fav_button);
shareImageView = findViewById(R.id.activity_main_image_view);
textViewTableName = findViewById(R.id.activity_main_did_u_know);
// tableImageView = findViewById(R.id.img_user);
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// typeface = getResources().getFont(R.font.sourceserifproregular);
// }
factTextView.setTypeface(typeface);
Intent intent = getIntent();
final int choice = intent.getIntExtra("choice",0);
switch (choice) {
case 1:
textViewTableName.setText("General Facts");
factTextView.setText(db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).getFact());
textViewId.setText("Fact " + (readFromShared("defaultKey") - 1) + " of " + db.getFactsCount(DatabaseFacts.TABLE_GENERAL_NAME));
if (db.getFact((readFromShared("defaultKey") - 1), DatabaseFacts.TABLE_GENERAL_NAME).isFavorite() == 0) {
favImageView.setImageResource(R.drawable.ic_favorite_border_black_24dp);
} else {
favImageView.setImageResource(R.drawable.ic_favorite_black_24dp);
}
ImageView leftClick = (ImageView) findViewById(R.id.activity_main_left_button);
leftClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
leftClickMethod("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME);
}
});
ImageView rightClick = (ImageView) findViewById(R.id.activity_main_right_button);
rightClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rightClickMethod("defaultKey",DatabaseFacts.TABLE_GENERAL_NAME);
return;
}
});
favImageView.setOnClickListener(v -> handleFavorites("defaultKey", DatabaseFacts.TABLE_GENERAL_NAME));
break;
Remove app:layout_constraintBottom_toTopOf="#id/guideline" from Brand and app:layout_constraintTop_toBottomOf="#id/LeftRight" from adView. You can't put constraints for Top and Bottom because it will center your View between those two constraints. Same about left and right (start, end), but you have match_parent here, so this problem doesn't occur (visualy - if you set wrap_content, view will go to the center too).
I find that there is a quick way to create animation with ConstraintSet for ConstrainLayout Activity. Faster than use TransitionManager for RelativeLayout
ConstraintSet use two xml file for an Activity. One for the first position and next one for the destination.
I want to create something like this:
https://media.giphy.com/media/2UwXdWEoLWe9iQMFIY/giphy.gif
But there is no clearly instruction show how to use it in Java. Anyone had done this can show me the source code or link to some post like that.
Thanks for reading the post.
This is possible with ConstraintSet. They key is two have two layouts one layout has ui elements of the screen and the other has elements on the screen. Now you can use TransitionManager with interpolator and duration of your choice and animate the layout changes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/constraint"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#181818"
tools:context=".MainActivity">
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/mugello"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUNE 3, 2018"
app:layout_constraintRight_toRightOf="#+id/title"
app:layout_constraintBottom_toBottomOf="#+id/title"
android:textSize="12sp"
android:background="#d3d3d3"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:background="#F44336"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp"
android:text="Mugello Circuit"
android:textColor="#FFFF"
android:textSize="45sp"
app:layout_constraintRight_toLeftOf="#+id/backgroundImage"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/fadeBackgroudView"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:foreground="#drawable/gradient_variant"
app:layout_constraintBottom_toTopOf="#+id/description" />
<TextView
android:id="#+id/tap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Tap for info"
android:textSize="15sp"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Mugello is a historic region and valley in northern Tuscany, in Italy. It is located to the north of the city of Florence and consists of the northernmost portion of the Metropolitan City of Florence. It is connected to the separate Santerno river valley by the Futa Pass."
android:textSize="22sp"
android:textColor="#FFFF"
android:background="#181818"
android:gravity="center"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
app:layout_constraintTop_toBottomOf="#+id/backgroundImage"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
activity_main_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="#drawable/mugello"
app:layout_constraintBottom_toTopOf="#+id/description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:background="#F44336"
android:paddingBottom="8dp"
android:paddingEnd="24dp"
android:paddingStart="24dp"
android:paddingTop="8dp"
android:text="Mugello Circuit"
android:textColor="#FFFF"
android:textSize="45sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JUNE 3, 2018"
app:layout_constraintRight_toRightOf="#+id/title"
app:layout_constraintTop_toBottomOf="#+id/title"
android:textSize="12sp"
android:background="#d3d3d3"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"/>
<View
android:id="#+id/fadeBackgroudView"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:foreground="#drawable/gradient"
app:layout_constraintBottom_toTopOf="#+id/description" />
<TextView
android:id="#+id/tap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Tap for info"
android:textSize="15sp"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Mugello is a historic region and valley in northern Tuscany, in Italy. It is located to the north of the city of Florence and consists of the northernmost portion of the Metropolitan City of Florence. It is connected to the separate Santerno river valley by the Futa Pass."
android:textSize="22sp"
android:textColor="#FFFF"
android:gravity="center"
android:background="#181818"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private boolean show = false;
private ImageView backgroundImage;
private ConstraintLayout constraint;
private ConstraintSet constraintSet = new ConstraintSet();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraint = findViewById(R.id.constraint);
backgroundImage = findViewById(R.id.backgroundImage);
backgroundImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(show)
hideComponents(); // if the animation is shown, we hide back the views
else
showComponents() ;// if the animation is NOT shown, we animate the views
}
});
}
private void showComponents(){
show = true;
constraintSet.clone(this, R.layout.activity_main_detail);
Transition transition = new ChangeBounds();
transition.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(constraint, transition);
constraintSet.applyTo(constraint);
}
private void hideComponents(){
show = false;
constraintSet.clone(this, R.layout.activity_main);
Transition transition = new ChangeBounds();
transition.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
transition.setDuration(1000);
TransitionManager.beginDelayedTransition(constraint, transition);
constraintSet.applyTo(constraint);
}
}
Here's a slide share on ConstraintLayout https://speakerdeck.com/camaelon/advanced-animations-and-constraintlayout
I'm trying to generate form elements from a template when the user clicks on a button. I created the template and the container layout for the new forms with XML. Its successfully generating the first form where I'm telling it to generate, but when I try to generate more forms beyond the first one its giving me an error: "the specified child already has a parent. You must call removeView() on the child's first parent". Any clue as to what I should do so as to generate more than one element? I've tried changing the id of the newly created forms but that's giving me a null pointer exception and crashing. Thank you.
public class MakeQuestion extends Activity implements OnClickListener{
private static final int MY_BUTTON = 9000;
int templateID = 1;
Button b;
Button target;
View insertPoint;
Button testTemplate;
View v1;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.make_question);
Initialize();
}
public void Initialize(){
//button for adding new forms
b = (Button) findViewById(R.id.makeLayoutButton);
b.setOnClickListener(this);
//get the template form to be duplicated
vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = vi.inflate(R.layout.form_template, null);
//set the params for the element that will have dynamically generated content below it
templateParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//container where forms will be contained in
insertPoint = findViewById(R.id.questionsContainer);
//view where new form will go below
View belowContainer = findViewById(R.id.questionTemplateFake);
//set id for layout params of view
belowContainer.setId(1);
//set rule for new forms to go below view 'belowContainer'
templateParams.addRule(RelativeLayout.BELOW, belowContainer.getId());
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.makeLayoutButton:
v1 = vi.inflate(R.layout.form_template, null);
//add view to the insertPoint
((LinearLayout) insertPoint).addView(v1);
break;
}
}
}
Added forms should go in "questionsContainer" which is set to be below "questionTemplateFake"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/grey_background" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#8B459A"
android:baselineAligned="false"
android:clipToPadding="false" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/logo_small" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="#drawable/settings" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/search" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/relativeLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relative12"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="WHAT IS YOUR QUESTION?" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/save_button"
android:layout_alignBottom="#+id/save_button"
android:layout_alignRight="#+id/textView1"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" />
<Button
android:id="#+id/save_button"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/save_button"
android:layout_marginTop="25dp"
android:text="CREATE AN ANSWER" />
<RelativeLayout
android:id="#+id/questionTemplateFake"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
<Button
android:id="#+id/Button02eew"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<EditText
android:id="#+id/EditText02"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="#+id/Button02"
android:layout_alignParentLeft="true"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/Button02"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/questionsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_below="#+id/questionTemplateFake"
>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/makeLayoutButton"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/scrollView1"
android:layout_marginRight="17dp"
android:layout_marginTop="79dp"
android:background="#drawable/purplebutton"
android:text="MORE OPTIONS"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
</RelativeLayout>
You are trying to add the same instance over and over again. Thus the message that "specific child already exists". You would have to create a new template layout instance with different ID (I suppose) and then try to add it in.
So rather than:
((RelativeLayout) insertPoint).addView(v1, templateParams);
and adding v1 again. Create a new instance
v1 = vi.inflate(R.layout.form_template, null);
set the id for good measures and then add it again in view.
I suppose you don't need to set the id, but you can read more about how id's work here