Inflated items are gone when device orientation changes - java

Hi i'm using eclipse for deveoping this android app. I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. The application seems like it has been restarted. But some values are still remaining.. Please help..
public void addNewItem() {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = li.inflate(R.layout.activity_order_items, null);
orderItemFrameView.add(item);
itemsLayout.addView(item);
setAdditionalFonts(item);
}
private void setAdditionalFonts(View view) {
TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo);
AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);
ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
TextView total = (TextView)view.findViewById(R.id.tak_Total);
qty.totTextView = total;
qty.rateTextView = rate;
qty.orderTake = this;
qty.index = orderItemFrameView.size() - 1;
qty.sqlDb = SqlDb;
qty.itemName = txtNameTak;
setupBarcodeScan(barScan);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");
Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
lblNameTak.setTypeface(tf3);
txtNameTak.setTypeface(tf2);
lblRate.setTypeface(tf2);
lblQty.setTypeface(tf2);
lblTotal.setTypeface(tf2);
lblNameTak.setText("Item " + (qty.index + 1));
DbAdapterItem dbItem;
TextAdapterItem txtItem;
dbItem = new DbAdapterItem(this, SqlDb);
txtItem = new TextAdapterItem(dbItem, this, qty);
txtNameTak.setAdapter(txtItem);
txtNameTak.setOnItemClickListener(txtItem);
txtNameTak.requestFocus();
}
all these items created by addNewItem() are vanished...
here is the layout from which i'm inflating...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom ="22dp"
tools:context=".OrderItems" >
<FrameLayout
android:layout_width="match_parent"
android:background= "#888888"
android:layout_height="98dp" >
<TextView
android:id="#+id/tak_itemNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="2.7dp"
android:text="Item 1"
android:layout_gravity="right"
android:textColor="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="22dp"
android:orientation="vertical"
android:background= "#FFFFFF"
android:layout_height="75dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/tak_itemName"
android:imeOptions="actionUnspecified"
android:imeActionLabel="search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_weight="1.06"
android:completionThreshold="1"
android:ems="10"
android:hint="Item Name / Barcode"
android:inputType="textCapWords"
android:singleLine="true"
android:textColorHint="#DDDDDD" />
<ImageView
android:id="#+id/barcode_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="6dp"
android:src="#android:drawable/ic_menu_camera" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:id="#+id/tak_lblRate"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Rate" />
<TextView
android:id="#+id/tak_Rate"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="#+id/tak_lblQty"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Qty" />
<com.sayka.ordergadget.EditText2
android:id="#+id/tak_Qty"
android:layout_width="match_parent"
android:layout_marginBottom="-4dp"
android:layout_marginTop="4dp"
android:inputType="numberDecimal"
android:imeOptions="actionNext"
android:layout_marginLeft="12dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:text="0.0" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<TextView
android:id="#+id/tak_lblTotal"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Total" />
<TextView
android:id="#+id/tak_Total"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>

It's normal behaviour and you are correct. the activity is recreated from scratch on rotation.
If you want something to persist. store it during onSaveInstanceState in the provided bundle.
then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again.
Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes.

Related

Android - How do I expand the height of a view from bottom to top?

I'm trying to make a visualization of some data in Android using a vertical bar chart, but when I increase or decrease the height of a View programatically by clicking a button, it changes from top to bottom, as in the image below (default value is 300).
How do I change the height from bottom to top, ie keeping the base of the view fixed?
Expected result:
My code in Java:
public final class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout layoutXAxis = (LinearLayout)this.findViewById(R.id.layoutX);
LinearLayout layoutYAxis = (LinearLayout)this.findViewById(R.id.layoutY);
LinearLayout layoutZAxis = (LinearLayout)this.findViewById(R.id.layoutZ);
TextView txtXAxis = (TextView)layoutXAxis.findViewById(R.id.txtXAxis);
TextView txtYAxis = (TextView)layoutYAxis.findViewById(R.id.txtYAxis);
TextView txtZAxis = (TextView)layoutZAxis.findViewById(R.id.txtZAxis);
View graphXAxis = (View)layoutXAxis.findViewById(R.id.XAxis);
View graphYAxis = (View)layoutYAxis.findViewById(R.id.YAxis);
View graphZAxis = (View)layoutZAxis.findViewById(R.id.ZAxis);
LinearLayout layoutBtnAdd = (LinearLayout)this.findViewById(R.id.layAdd);
Button btnAddX = (Button)layoutBtnAdd.findViewById(R.id.btnXAdd);
Button btnAddY = (Button)layoutBtnAdd.findViewById(R.id.btnYAdd);
Button btnAddZ = (Button)layoutBtnAdd.findViewById(R.id.btnZAdd);
LinearLayout layoutBtnSub = (LinearLayout)this.findViewById(R.id.laySub);
Button btnSubX = (Button)layoutBtnSub.findViewById(R.id.btnXSub);
Button btnSubY = (Button)layoutBtnSub.findViewById(R.id.btnYSub);
Button btnSubZ = (Button)layoutBtnSub.findViewById(R.id.btnZSub);
txtXAxis.setText(String.valueOf(graphXAxis.getLayoutParams().height));
txtYAxis.setText(String.valueOf(graphYAxis.getLayoutParams().height));
txtZAxis.setText(String.valueOf(graphZAxis.getLayoutParams().height));
btnAddX.setOnClickListener((OnClickListener)(new OnClickListener() {
public final void onClick(View it) {
View graph = graphXAxis;
LayoutParams params = graph.getLayoutParams();
params.height += 10;
graph = graphXAxis;
graph.setLayoutParams(params);
TextView text = txtXAxis;
View graph = graphXAxis;
text.setText(String.valueOf(graph.getLayoutParams().height));
}
}));
... // onClickListener of the other buttons
}
My code in XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/layoutY">
<TextView
android:id="#+id/txtYAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Y AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_blue_light"/>
...
<View android:id="#+id/YAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_blue_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutX"
android:layout_toLeftOf="#id/layoutY"
android:layout_toStartOf="#id/layoutY"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="#+id/txtXAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="X AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"/>
...
<View android:id="#+id/XAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutZ"
android:layout_toRightOf="#id/layoutY"
android:layout_toEndOf="#id/layoutY"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp">
<TextView
android:id="#+id/txtZAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Z AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_green_light"/>
...
<View android:id="#+id/ZAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
...
</RelativeLayout>
You might consider constructing your layout like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button_layout"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtYAxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Y AXIS"
android:textColor="#android:color/holo_blue_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/YAxis"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtXAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_red_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/XAxis"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutZ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtZAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Z AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_green_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/ZAxis"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the output.
Hope that helps!

Why do i always see the last id in View.OnClickListener?

Why do I always see the last id no matter which text I click?
I would like to see the right fragment displayed in the layout that i created according to the relevant text clicked. I always see the 'alternatives' fragment.
I understood that tags could be helped, but I could not figure out how to use them.
In addition, I tried to use diffrent versions of FragmentManager and FragmentTransaction and even to remove the switch and call each setOnClickListener of textview separately but nothing helped.
This is my Activity file:
IngredientsFragment ingredientsFragment;
FavouritesFragment favouritesFragment;
FeedbacksFragment feedbacksFragment;
Write_Feedback_Fragment writeFeedbackFragment;
AlternativesFragment alternativesFragment;
TextView ingredients;
TextView favourites;
TextView feedbacks;
TextView alternatives;
TextView writeFeedback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
window=this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
ingredients= (TextView) findViewById(R.id.ingredients_option);
favourites= (TextView) findViewById(R.id.favorites_option);
feedbacks= (TextView) findViewById(R.id.watch_feedback_option);
writeFeedback= (TextView) findViewById(R.id.add_feedback_option);
alternatives= (TextView) findViewById(R.id.alternatives_option);
ingredients.setOnClickListener(this);
favourites.setOnClickListener(this);
feedbacks.setOnClickListener(this);
writeFeedback.setOnClickListener(this);
alternatives.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ingredients_option:
ingredientsFragment= new IngredientsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,ingredientsFragment).commit();
break;
case R.id.favorites_option:
favouritesFragment= new FavouritesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,favouritesFragment).commit();
break;
case R.id.watch_feedback_option:
feedbacksFragment= new FeedbacksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,feedbacksFragment).commit();
break;
case R.id.add_feedback_option:
writeFeedbackFragment= new Write_Feedback_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,writeFeedbackFragment).commit();
break;
case R.id.alternatives_option:
alternativesFragment= new AlternativesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,alternativesFragment).commit();
break;
}
}
}
This is my XML file:
<RelativeLayout 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=".ProductDetails">
<RelativeLayout
android:id="#+id/recommendation_template"
android:layout_width="match_parent"
android:layout_height="142dp"
android:background="#android:color/white">
<ImageView
android:id="#+id/registartion_arrow"
android:layout_width="45dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_arrow_forward_black_24dp" />
<TextView
android:id="#+id/rec_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="Recommendation:"
android:textSize="25dp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
<ImageView
android:id="#+id/vi_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rec_tv"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="26dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_check" />
<TextView
android:id="#+id/vi_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_marginLeft="4dp"
android:layout_marginTop="-29dp"
android:layout_toRightOf="#id/vi_image"
android:text="This is for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
<ImageView
android:id="#+id/not_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="3dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_do_not" />
<TextView
android:id="#+id/not_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/not_image"
android:layout_marginLeft="5dp"
android:layout_marginTop="-27dp"
android:layout_toRightOf="#id/not_image"
android:text="Not for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/recommendation_scrollView"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_below="#id/recommendation_template"
android:layout_marginTop="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recommendation_template"
style="?android:attr/borderlessButtonStyle">
<TextView
android:id="#+id/ingredients_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ingredients"
android:text="Ingredients" />
<TextView
android:id="#+id/favorites_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_star"
android:paddingLeft="80dp"
android:text="Favorites" />
<TextView
android:id="#+id/watch_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/customer_review"
android:paddingLeft="145dp"
android:text="Feedbacks" />
<TextView
android:id="#+id/add_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/write_feedback"
android:paddingLeft="220dp"
android:text="Write feedback" />
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />
</FrameLayout>
</HorizontalScrollView>
<ScrollView
android:id="#+id/scrollView_container"
android:layout_width="match_parent"
android:layout_height="499dp"
android:layout_below="#id/recommendation_scrollView">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="463dp">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Your code should work like that. The problem is your layout. See the textviews in your FrameLayout. Your defining a paddingLeft and layout_width so, that your textview alternatives_option is overlaying all the other textviews. Thats why in the onclick you always get the id of that view.
I suggest doing a tutorial about XML layout.
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />

RadioButton switching between layouts

So I would like to have a calculator in my app. User will first click on RadioButton which would be for Imperial and Metric units, and depending on which one does user click, one of the layouts would appear.
As you know imperial units has feets and inches, for which I need two EditTexts, but metric unit needs only one.
So that's what I want, switching between layouts in same activity to avoid using fragments.
Any ideas?
EDIT
xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
tools:showIn="#layout/activity_kalkulatori_bfman"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout28"
android:layout_marginBottom="20dp"
android:layout_marginTop="55dp"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_uvodno"
android:id="#+id/textView140"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tekst_upute"
android:id="#+id/textView141"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Drugo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout27"
android:layout_below="#+id/linearLayout28"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_radio_naslov"
android:id="#+id/textView3"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneCmKg"
android:text="#string/bf_radio2"
android:onClick="radioZeneCmKgClicked"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneFtLb"
android:text="#string/bf_radio1"
android:onClick="radioZeneBFFtLbClicked"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_tezina"
android:id="#+id/textView133"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTezinaBF"
android:hint="#string/bf_edit_tezina_hint_z"
android:layout_marginBottom="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaMetric">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_visina"
android:id="#+id/textViewVisinaMetric"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVisinaBF"
android:hint="#string/bf_edit_visina_hint_z"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaImperial">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_feetinch_naslov"
android:id="#+id/textViewVisinaImperial"
android:textStyle="bold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaFT"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaINC"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint2" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_struk"
android:id="#+id/textView135"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editStrukBF"
android:hint="#string/bf_edit_struk_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_bokovi"
android:id="#+id/textView136"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editBokoviBF"
android:hint="#string/bf_edit_bokovi_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_vrat"
android:id="#+id/textView137"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVratBF"
android:hint="#string/bf_edit_vrat_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_godine"
android:id="#+id/textView138"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editGodineBF"
android:hint="#string/bf_edit_godine_hint_z"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout27"
android:layout_marginBottom="20dp"
android:id="#+id/linearLayout29">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bf_btn_izracunaj"
android:id="#+id/btnIzracunajBF"
android:layout_gravity="center_horizontal"
android:background="#424242"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_rezultat"
android:id="#+id/textView139"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textRezultatBF"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_rezultat"
android:id="#+id/textView152" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#+id/linearLayout27"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_tumacenje"
android:id="#+id/textView142"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tumacenje_rezultat_z"
android:id="#+id/textView143"
android:gravity="center_horizontal"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
java
public class KalkulatoriBFMan extends AppCompatActivity {
EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
Button btnIzracunajBF;
TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
LinearLayout imperial, metric;
public void radioZeneCmKgClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioButtonBFZeneCmKg:
if (checked)
editVisina.setVisibility(View.VISIBLE);
imperial.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisina.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
public void radioZeneBFFtLbClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButtonBFZeneFtLb:
if (checked)
editVisinaFT.setVisibility(View.VISIBLE);
metric.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_bfwoman);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
editTezina = (EditText) findViewById(R.id.editTezinaBF);
editVisina = (EditText) findViewById(R.id.editVisinaBF);
editStruk = (EditText) findViewById(R.id.editStrukBF);
editBokovi = (EditText) findViewById(R.id.editBokoviBF);
editVrat = (EditText) findViewById(R.id.editVratBF);
editGodine = (EditText) findViewById(R.id.editGodineBF);
editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);
btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);
textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);
}
}
Note that I solved blank space problem with setting visibility to GONE, but when I open activity both EditTexts appear, imperial and metric. I want them both to be invisible until user selects one RadioButton
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here are a couple RadioButton objects:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.imperial:
if (checked)
EditBox1.setVisibility(Visible)
break;
case R.id.metrics:
if (checked){
EditBox1.setVisibility(Visible)
EditBox2.setVisibility(Visible)
}
break;
}
}
Source: http://developer.android.com/guide/topics/ui/controls/radiobutton.html

Error when generating dynamic content

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

ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

I have some problems with this code.
I'm trying to create a dynamically list of news with this format:
|image| - title
|image|- subtitle
this is my little cicle code (a example with random data)
void setListNews(List<Map<String,String>>l){
listaNewsPagina = l;
final Iterator ite = listaNewsPagina.iterator();
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
LinearLayout lineare = (LinearLayout)findViewById(R.id.lineare);
while(ite.hasNext()){
Map<String,String> map = (Map<String, String>) ite.next();
ImageView imm = (ImageView)findViewById(R.id.immagine);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relative);
TextView titolo = (TextView)findViewById(R.id.titolo);
TextView sottoTitolo = (TextView)findViewById(R.id.sottoTitolo);
titolo.setText("titolooooo");
sottoTitolo.setText("sottoTitoloooooooooooo");
rl.addView(titolo);
rl.addView(sottoTitolo);
lineare.addView(imm);
lineare.addView(rl);
}
lin.addView(lineare);
setContentView(lin);
and this is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_sezione_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_homepage" >
<LinearLayout
android:id="#+id/lineare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aadd99"
>
<ImageView
android:id="#+id/immagine"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:background="#abfc99">
<TextView
android:id="#+id/titolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="#+id/sottoTitolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/titolo"
android:textSize="13dp" />
</RelativeLayout>
</LinearLayout>
<ListView
android:id="#+id/listViewNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ImageButtonPrecc"
android:layout_alignParentLeft="true"
android:layout_below="#+id/lineare"
android:alpha="0.8"
android:background="#aadd99"
android:clickable="true"
android:padding="10dp"
android:textAlignment="center" >
</ListView>
<ImageButton
android:id="#+id/ImageButtonPrecc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:src="#drawable/bottone_prev"
android:text="#string/load_news" />
<ImageButton
android:id="#+id/ImageButtonSucc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/bottone_next"
android:text="#string/load_news" />
</RelativeLayout>
when I launch the activity with this code I have this problem:
05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
I'm trying to create a layout with only Java code...but nothing XD
where I'm wrong?
Change
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
to
RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news);
The id linear_sezione_news is assigned to a RelativeLayout so you can't cast it to LinearLayout

Categories