Dynamically adding component in relative layout - java

I have an xml structure like,
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:minWidth="20dp"
android:text="05"
android:background="#drawable/rounded_corners"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txt2"
style="android:textViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:layout_toRightOf="#+id/txt1"
android:background="#drawable/rounded_corners"
android:minWidth="20dp"
android:text=""
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:layout_toRightOf="#+id/txt2"
android:background="#drawable/rounded_corners"
android:minWidth="20dp"
android:text=""
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
As I need this TextView to be dynamically added to View. I am trying to convert same in onCreate() method like below,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable rounded_corners = getResources().getDrawable(R.drawable.rounded_corners);
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.mainrelativeactivity);
TextView tv = new TextView(this);
RelativeLayout.LayoutParams rel = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(rel);
tv.setText("ABC");
tv.setBackground(rounded_corners);
relativeLayout.addView(tv);
tv = new TextView(this);
rel = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(rel);
tv.setText("DEF");
tv.setBackground(rounded_corners);
relativeLayout.addView(tv);
}
But I don't understand how to set layout_alignParentLeft,layout_marginTop and most concern is layout_toRightOf part?
If some one helpful to direct me to example like this then it will be great...

You'll have to add the rule with the RelativeLayout.LayoutParams to accomplish what u want..
Something like this
For aligning a view to right of an another view
rel.addRule(RelativeLayout.RIGHT_OF, tv.getId());
For aligning a view to the left in parent
rel.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
For giving margins
rel.setMargins(50, 10, 0, 0);

Related

Giving the same id to two different components in an xml file Android

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()
}

Add Defined TextInputLayout Dynamically and Programatically

I am trying to add a TextInputLayout with an EditText based on a number that the user selects from a Spinner. I already have the TextInputLayout attributes defined in XML and was hoping to simple just add them programmatically based on the number that the user selects from the spinner:
XML:
<FrameLayout
android:background="#drawable/image_border"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525">
<Button
android:id="#+id/add_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".475">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_question_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="#string/create_poll_question" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/how_many_answers_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_many_answers_text"
android:textColor="#color/black"
android:textSize="16sp" />
<Spinner
android:id="#+id/number_of_answers_spinner"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_height="24dp"
android:background="#android:drawable/btn_dropdown" />
</LinearLayout>
<!--Want to Add Programatically -->
<android.support.design.widget.TextInputLayout
android:id="#+id/create_poll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_answer_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here is the code I am currently using, but it does not add dynamically based on the view I already created:
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
EditText editText = new EditText(getActivity());
editText.setHint("Poll Answer");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
}
Easiest way to do such dynamic injections of view is to use ButterKnife library by JakeWharton
http://jakewharton.github.io/butterknife/
You can use it like in your activity's declaration part:
#BindView(R.id.title) TextInputLayout textInputLayout;
Then bind it inside onCreate() like:
ButterKnife.bind(this);
This will roughly be equivalent to inflating and finding the view by id.
Moreover, the library helps to dynamically set drawables,etc. with ease as well
Add
android:id="#+id/create_poll_linearlayout"
to your root LinearLayout.

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

How to add a copy of XML dynamically

I am making an Android app, and I want to copy some XML code in a Linear Layout, and re-insert it into the Linear Layout so that there are two of the Relative Layouts in the Linear Layout. I would like to do this dynamically by taking this code below:
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
And then simply turning it into this:
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
See how there would be TWO RelativeLayout sections... I would like to basically make a copy of one and then add it back in (I don't really know how many times I might have to do this in my program, that's why I am not literally inserting it into the XML, I would like to do it from the Java code).
This is what I have so far, but whenever I run it, the layout is wrong. What might be wrong with my code?
LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);
RelativeLayout m2 = new RelativeLayout(this);
m2.setLayoutParams(m.getLayoutParams());
m2.setGravity(m.getGravity());
m2.setLayoutDirection(m.getLayoutDirection());
TextView et1 = (TextView) findViewById(R.id.bottom1);
TextView et2 = (TextView) findViewById(R.id.left1);
TextView et3 = (TextView) findViewById(R.id.right1);
TextView et4 = (TextView) findViewById(R.id.top1);
TextView tv1 = new TextView(et1.getContext());
TextView tv2 = new TextView(et2.getContext());
TextView tv3 = new TextView(et3.getContext());
TextView tv4 = new TextView(et4.getContext());
tv1.setLayoutDirection(et1.getLayoutDirection());
tv2.setLayoutDirection(et2.getLayoutDirection());
tv3.setLayoutDirection(et3.getLayoutDirection());
tv4.setLayoutDirection(et4.getLayoutDirection());
tv1.setGravity(et1.getGravity());
tv2.setGravity(et2.getGravity());
tv3.setGravity(et3.getGravity());
tv4.setGravity(et4.getGravity());
tv1.setText(et1.getText());
tv2.setText(et2.getText());
tv3.setText(et3.getText());
tv4.setText(et4.getText());
m2.addView(tv4,et4.getLayoutParams());
m2.addView(tv3,et3.getLayoutParams());
m2.addView(tv2,et2.getLayoutParams());
m2.addView(tv1,et1.getLayoutParams());
m3.addView(m2);
I don't see what's wrong with my code, any suggestions.....
THIS IS THE FULL XML DATA FILE:
<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="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<HorizontalScrollView
android:id="#+id/scrollHORIZON"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/linearLayout1" >
<LinearLayout
android:id="#+id/tileContainerME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/tilesAreHERE"
android:layout_width="207dp"
android:layout_height="151dp" >
<TextView
android:id="#+id/bottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/top1"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="1"
/>
<TextView
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="2"
/>
<TextView
android:id="#+id/right1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left1"
android:layout_alignBottom="#+id/left1"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="3"
/>
<TextView
android:id="#+id/top1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="4"
/>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Inflating the same layout inside it once again might help you achieve this.
Try this code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
LinearLayout main_layout = (LinearLayout)findViewById(R.id.tileContainerME);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.main_layout, null);
main_layout.addView(child, 1);
}
You'll get something like this:
You are probably getting an error message that the View you are trying to add already has a parent. You would need to call removeView() on the parent of your TextViews or removeAllViews() to just remove them all before adding them to your new RelativeLayout. Something like
m.removeAllViews();
m2.addView(et1);
m2.addView(et2);
m2.addView(et3);
m2.addView(et4);
However, if you are adding them multiple times then you probably want to create new instances of them as you do with your RelativeLayout (m2). Then you can use the params that your original TextViews have. If this isn't your current error then please post your logcat but this will cause an exception.
Edit
TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);
//Create the new TextViews
TextView tv1 = new TextView(m.getContext()); //if inside Activity you can use this instead of m.getContext()
// set params which you can get from the above TextViews
m2.addView(tv1);
...
I figured it out!
LinearLayout m3 = (LinearLayout)findViewById(R.id.tileContainerME);
RelativeLayout m = (RelativeLayout)findViewById(R.id.tilesAreHERE);
RelativeLayout m2 = new RelativeLayout(m.getContext());
m2.setLayoutParams(m.getLayoutParams());
TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);
TextView tv1 = new TextView(et1.getContext());
TextView tv2 = new TextView(et2.getContext());
TextView tv3 = new TextView(et3.getContext());
TextView tv4 = new TextView(et4.getContext());
tv1.setText(et1.getText());
tv2.setText(et2.getText());
tv3.setText(et3.getText());
tv4.setText(et4.getText());
tv1.setLayoutParams(et1.getLayoutParams());
tv2.setLayoutParams(et2.getLayoutParams());
tv3.setLayoutParams(et3.getLayoutParams());
tv4.setLayoutParams(et4.getLayoutParams());
m2.addView(tv1);
m2.addView(tv2);
m2.addView(tv3);
m2.addView(tv4);
m3.addView(m2);
This will make a copy of the previous layout and then add it back into the current layout!
I FOR SURE got it working this time! I used this code:
LinearLayout item = (LinearLayout)findViewById(R.id.tileContainerME);
View child = getLayoutInflater().inflate(R.layout.activity_main, null);
item.addView(child);
It would copy the 'activity_main' class which holds solely the XML data for the tile. Then it copies it into the Horizontal Scroll View! Here is a screenshot of my solution:
http://i.stack.imgur.com/WxoJG.png

Android dynamically add custom component to LinearLayout

i want add a custom component on a LinearLayout whenever i touch a button.
This is my code:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini);
View custom = vi.inflate(R.layout.custom_list_folder, null);
TextView textView = (TextView) custom.findViewById(R.id.label_pathFolder);
textView.setText(pathDirectory);
Spinner spinnerLevel = (Spinner) custom.findViewById(R.id.spinner_level);
try{
root.addView(custom);
}catch(Throwable e) {
Log.e("BurgerClub", "MEX: " + e.getMessage());
e.printStackTrace();
}
In this way, only first custom component is added, why?
Thanks
edit:
I've modified my code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);
This is my custom component:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom"
android:layout_marginBottom="2dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
style="#style/Theme.BurgerClubStyle" >
<TextView
android:id="#+id/label_pathFolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center"
android:padding="20dp"
android:textSize="25sp"
style="#style/customText" />
<Spinner
android:id="#+id/spinner_level"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_show_desc_img"
android:entries="#array/n_level_array"
android:padding="20dp"
android:prompt="#string/n_level_prompt"
android:textAlignment="3"
style="#style/customText" />
<ImageButton
android:id="#+id/btn_show_desc_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/btn_remove_folder"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_desc" />
<ImageButton
android:id="#+id/btn_remove_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/button_press"
android:contentDescription="#string/showDescImg"
android:src="#drawable/ic_delete" />
</RelativeLayout>
The first time that i press the button, custom component was added, but all other times NOT.
The first custom element is not overwrite, it's the only visible!
Where's your layout_cartelle_immagini declared?
Probably it's a horizontal LinearLayout, just set orientation to vertical and your code should work (considering the code to add a custom view is inside a onClickListener).
Try this. It will help you.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
root.addContentView(custom, params);

Categories