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);
Related
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.
How i can display two textview side by side in Java ?
I succeeded to do in xml !
My code :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>
Make a LinearLayout in my activity_layout.
LinearLayout lm=(LinearLayout) findViewById(R.id.Linearlayout1);
Make LinearLayout of type horizontal, dynamically.
LinearLayout llh=new LinearLayout(context);
llm.setOrientation(LinearLayout.HORIZONTAL);
Then created two TextView dynamically
TextView tv1=new TextView(context);
TextView tv2=new TextView(context);
finally add these two TextView's to the horizontal LinearLayout (that we created dynamically) and later the same layout to the xml layout.
llh.addView(tv1);
llh.addView(tv2);
lm.addView(llh);
I hope it was helpful.
Try this,
Java Code,
layout = (LinearLayout)findViewById(R.id.mainLay);
TextView tv1 = new TextView(ActivityName.this);
tv1.setText("TextView 1");
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv1.setLayoutParams(childParam1);
TextView tv2 = new TextView(ActivityName.this);
tv2.setText("TextView 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv2.setLayoutParams(childParam2);
layout.addView(tv1);
layout.addView(tv2);
and xml code,
<LinearLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:weightSum="1"
android:id="#+id/mainLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
This should work.
First Solution:
use online converter
[http://www.xmltojava.com/][1]
Second Solution:
inflate your layout
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layoutView = mInflater.inflate(R.rowLayout, null);
TextView tvOne=(TextView) layoutView.findViewById(R.id.tvOne);
this is what I try to do
final LinearLayout ll = (LinearLayout) findViewById(R.id.container_LV);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
//ll.setOrientation(orientation);
// params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
LinearLayout llh=new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < fields.length(); i++)
{
final TextView txtLabel = new TextView(getApplicationContext());
final TextView txtValue = new TextView(getApplicationContext());
jsonLabel = fields.getJSONObject(i).getString(TAG_LABEL) ; //+ "\n";
jsonValue = fields.getJSONObject(i).getString(TAG_VALUE) ; //+ "\n";
//txtLabel.setBackgroundColor(Color.YELLOW);
//txtValue.setBackgroundColor(Color.YELLOW);
//txtLabel.setWidth(pixels);
txtLabel.setLayoutParams(params);
txtValue.setLayoutParams(params);
txtValue.setGravity(Gravity.RIGHT);
txtLabel.setGravity(Gravity.LEFT);
//params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
txtLabel.setText(jsonLabel);
txtValue.setText(jsonValue);
txtLabel.setTextColor(Color.WHITE);
txtValue.setTextColor(Color.WHITE);
//txtLabel.
llh.addView(txtLabel);
llh.addView(txtValue);
}
ll.addView(llh);
The key is to use android:layout_weight only after setting android:layout_width to "0dp", try this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>
I want to set a relativeLayout height inside a ListView.
This is my code in MenuListAdapter.java
RelativeLayout background = (RelativeLayout) convertView.findViewById(R.id.background_color);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setTypeface(typeface);
if(navDrawerItems.get(position).getType() == MenuType.HEADER)
{
background.setBackgroundColor(context.getResources().getColor(R.color.header_color));
background.setClickable(false);
background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
}else{
background.setBackgroundColor(color.transparent);
}
I'm adding xml for more information of my code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/transparent"
android:layout_height="wrap_content"
android:id="#+id/background_color">
<ImageView
android:id="#+id/icon"
android:layout_width="#dimen/spacing_layout_image"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/spacing_margin_text"
android:layout_marginRight="#dimen/spacing_margin_text"
android:src="#drawable/menu_btn"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:textColor="#drawable/menu_text"
android:paddingRight="#dimen/spacing_scrollview"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/notifTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/spacing_margin_text"
android:background="#android:color/holo_red_dark"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:paddingLeft="#dimen/radius_default"
android:paddingRight="#dimen/radius_default"
android:visibility="gone"/>
but i get an error saying that "E/AndroidRuntime(11406): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"
Can someone tell me what's wrong with my code ?
Thank's
It's working, use AbsListView instead of RelativeLayout.
Thank you for Mr.Piyush Kukadiya
Try this :
Change
background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
to
background.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
UPDATE :
RelativeLayout.LayoutParams and AbsListView.LayoutParams are two different classes of android.widget Package.
Here you are replacing existing layout params of correct type AbsListView.LayoutParams with more generic ViewGroup.LayoutParams i.e. RelativeLayout.LayoutParams.
I would like to know, how can i add a lot of number of TextView to an RelativeLayout in a ScrollView (for textview can scroll up, of course)
In my XML code, it's easy :
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/bde" >
<RelativeLayout
android:id="#+id/layout_"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_below="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
</ScrollView>
Except i would like add in Java code, for that each TextView from a DB (no problems with db), add himself in layout and the more important, the one after the other.
Now, i have :
//Layout in ScrollView
RelativeLayout r = (RelativeLayout) findViewById(R.id.layout_);
//for add TextView
for(int i = 0 ; i < nbr_limite ; i++){
TextView v1 = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
r.addView(v1);
}
In fact, i would like a : android:layout_below="#id/textView1" but in Java and replace the : #id/textView1 by a getLastId() roughly said.
And more roughly, it will be something like : r.addLastView(v1)
I waiting for your answer or your comment :)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
params.addRule(RelativeLayout.BELOW, tv.getId()-1);
r.addView(tv);
also this is an answer for your question .... Programatically add view one below other in relative layout
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);