Set Relativelayout height inside listview - java

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.

Related

Why does dynamically creating a Layout be different from creating it in xml?

This is my xml created file for a layout and it displays as I want it to.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#android:drawable/dialog_holo_light_frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/imageSectionCustomRowL"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:id="#+id/titleCustomRowL"
android:paddingRight="10dp"
android:textSize="30sp"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#959595"
android:paddingBottom="10dp"
android:paddingTop="5dp"
android:id="#+id/subTitleCustomRowL"
android:paddingLeft="20dp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And this is more or less the same thing but created dynamically via code
LinearLayout parentLL = (LinearLayout)findViewById(R.id.newsMainPopulationLL);
LinearLayout parentRowLL = new LinearLayout(news.this);
parentRowLL.setOrientation(LinearLayout.VERTICAL);
Drawable bg = news.this.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame);
//Drawable bbg = getDrawable(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
parentRowLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertDpToPx(120)));
LinearLayout parentRowChildHLL = new LinearLayout(news.this);
parentRowChildHLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
parentRowChildHLL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout containerForPic = new LinearLayout(news.this);
containerForPic.setLayoutParams(new LinearLayout.LayoutParams(convertDpToPx(75), convertDpToPx(75)));
containerForPic.setOrientation(LinearLayout.VERTICAL);
ImageView picInList = new ImageView(news.this);
picInList.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
LinearLayout contentVerticalLL = new LinearLayout(news.this);
contentVerticalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
contentVerticalLL.setOrientation(LinearLayout.VERTICAL);
TextView headingTV = new TextView(news.this);
headingTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
headingTV.setPadding(convertDpToPx(10), convertDpToPx(10), convertDpToPx(10), 0);
headingTV.setTextColor(Color.parseColor("#000000"));
headingTV.setTextSize(30);
TextView contentTV = new TextView(news.this);
contentTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentTV.setPadding(convertDpToPx(10), convertDpToPx(5), convertDpToPx(10), convertDpToPx(10));
contentTV.setTextColor(Color.parseColor("#959595"));
contentVerticalLL.addView(headingTV);
contentVerticalLL.addView(contentTV);
parentRowChildHLL.addView(contentVerticalLL);
containerForPic.addView(picInList);
parentRowChildHLL.addView(containerForPic);
parentRowLL.addView(parentRowChildHLL);
parentLL.addView(parentRowLL);
Here convertDptoPx is a int() that works properly. However for some reason the Linear layout parentRowChildLL even though set to LinearLayout.HORIZONTAL for orientation for some reason displays vertically (According to the layout boundaries I see using the developer options setting)
Is there something basic I'm missing out yet? When I see the boundaries I have 3 rows. And also I tried encasing my image view in the layout because otherwise it is not even visible.
Thank You.

Android how to set 2 TextView side by side programmatically?

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>

Insert a TextView under an others in a RelativeLayout in a ScrollView

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

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);

Adding editText to customView

I want to add EditText to canvas in specific coordinates.
Something like this :
I tried to use code :
LinearLayout layout = new LinearLayout(context);
EditText textView = new EditText(context);
textView.setVisibility(View.VISIBLE);
textView.setText("Hello world");
layout.addView(textView);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
layout.setGravity(Gravity.BOTTOM);
layout.draw(canvas);
But this EditText wasn't show keyboard on click. Can you help me?
Assuming you really need to draw it on a canvas...
Create a new canvaslayout.xml inside your res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="5" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="Edit Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Change code too
LinearLayout layout = findViewById(R.layout.canvaslayout);
//LinearLayout layout = youractivity.findViewById(R.layout.canvasLayout);
layout.draw(canvas);
Programmaticly:
If you don't want to use XML (I don't see why not) you can use this method. You change the linear layout to a relative layout and use something like this to put views to the right off another view.
RelativeLayout.LayoutParams relativeLayoutParams=
new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView.getId());//to align the textview side by side

Categories