I want to insert a TextView into a LinearLayout that's in a different XML file that is being used as layout for a ListView. But it seems that I can't access the LinearLayout that's in the other XML file.
I run a loop that gets category titles, and for each title I wanna create a TextView with the title.
How I insert the TextView
LinearLayout Layout = (LinearLayout) findViewById(R.id.itemDesign); //When debugging I can see "Layout" is just null. itemDesign is also not in main.xml.
TextView title = new TextView(this);
title.setText(CatName);
Layout.addView(title);
XML used by ListView "single_list_item.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/itemDesign"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Name Label -->
<TextView android:id="#+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/>
<!-- Description Label -->
<TextView android:id="#+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"/>
</LinearLayout>
It sounds like you need to inflate your view. In getView() of adapter class
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.your_layout, parent, false);
Then to access your TextViews
TextView firstTV = (TextView) rowView.findViewById(R.id.some_id);
All about LayoutInflater
I guess, you are not setting the activity content view before using findViewById():
setContentView(R.layout.myLayout);
After that initialize your text view, and add width, height params to that. Then there should be no problems in adding that text view to the layout.
Maybe you should try invalidating the layout after you add the TextView:
Layout.addView(title);
Layout.invalidate();
I'm still not entirely sure what you want to achieve, maybe you could give us some more information regarding what you want to accomplish?
If you want to make a new ListView item based on an xml file, you need to inflate it first. Then you can call findViewById() on the resulting view to get the children.
LayoutInflater inf = LayoutInflater.from(getContext());
LinearLayout listItemLayout = (LinearLayout)inf.inflate(R.layout.single_list_item, null);
TextView nameLabel = (TextView) listItemLayout.findViewById(R.id.name_label);
Related
We have MainActivity.class , activity_main.xml and firstlayout.xml.
In firstlayout.xml there is a listview. I am in MainActivity.class and I want to access my listview. How can I do that when my listview is in another .xml file.
Something like
ListView lstview = (ListView) findViewById(R.id.lstview)
does not work, because I get a
null reference error
because my listview is not in mainactivity.xml. I also tried
setcontentView(R.layout.firstlayout)
but it didn't work either.
How can I access a xml file which is NOT in main_activity.xml ?
There are 2 possible way for useing ListView in your mainActivity.
1) You can simply include that layout (firstlayout.xml) into you activity_main.xml file.
example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/firstlayout"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
2) The second option is Layout Inflater.You can simply add other layout file directly into your java file.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.firstlayout, this);
You can inflate the view with the layout inflater:
getLayoutInflater().inflate(R.layout.firstlayout,
parentView, //the view that logically contains the list view, or just the base view in the main layout
false // false if you want to attach the list view to the parent, true otherwise
);
You can inflate your own:
View firstLayout = getLayoutInflater().inflate(R.layout.firstlayout, null, false);
ListView listView = (ListView) firstLayout.findViewById(R.id.lstView);
Just ensure that firstlayout has a ListView with id lstview.
If I had two layouts, layout1.xml and layout2.xml, that are both just empty LinearLayouts, and I set layout1 as an activity's content view:
setContentView(R.layout.layout1);
Is it possible to add the second layout right under the first?
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout2 = (LinearLayout) inflater.inflate(R.layout.layout2);
View parent = // parent of current layout root
parent.addView(layout2);
So the hierarchy would look like:
<parent>
<layout1 />
<layout2 />
</parent>
Basically I'm wondering how to get that parent view if it exists.
I think the easiest way to accomplish what you want is to create a third layout (parent.xml) that includes the other two layouts using include tags. Then setContentView(R.layout.parent);
Have a look here:
http://developer.android.com/training/improving-layouts/reusing-layouts.html
As an example, consider the following parent layout that pulls in your other two layouts:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include android:id="#+id/layoutOne" layout="#layout/layout1" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<include android:id="#+id/layoutTwo" layout="#layout/layout2" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
View parent= getWindow().getDecorView().findViewById(android.R.id.content)
I think you want something like this :
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutFirst = (RelativeLayout) inflate.inflate(R.layout.abc, null);
RelativeLayout layoutSecond = (RelativeLayout) inflate.inflate(R.layout.xyz, null);
layoutMain.addView(layoutFirst, 100, 100); // width=100, height=100
layoutMain.addView(layoutSecond, 100, 100);
You can read the documetation here.
I want to add a title bar in the list view.
I have added a text view inside the listview which can act as title:
Layout File :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_rounded"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="30px"
android:layout_below="#id/linlaypay"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="#+id/rectran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout >
Here is the code used to add the title listview dynamically:
listView = (ListView) findViewById(R.id.rectran);
TextView textView = new TextView(getApplicationContext());
textView.setText("header view");
listView.addHeaderView(textView);
The problem is ,an emty text view isadded to the listview without the text "header view".
Why?Am I missing anything?
I don't believe you can just add a view like this. The view being passed into listView needs to be inflated.
create a header.xml layout file with the text view in it with the text set.. and then run the code below.
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
If I were you, I would add a title to the ActionBar, assuming that your ListView is the whole Activity. If it isn't the whole Activity or it doesn't take up the whole screen, you could add the title to the layout rather than dynamically adding it.
http://developer.android.com/reference/android/app/ActionBar.html
Check out the Android Developers documentation on the ActionBar
My activity holds 3 small listviews and I wanted to give title to each of the view by adding textview.
However the text in the Textview was not visible because the size of the text was too small.
setting the text size of the textview resolved the issue.
Thanks !!
I have looked around, but most of them don't make any sense. Some have a third parameter that does not exists in the Android docs, at least anymore. Anybody have any idea how to accomplish this? I have this so far:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.day_view_item, parent, false);
}
((TextView)convertView.findViewById(R.id.day_hour_side)).setText(array[position]);
if(count == 1) {
LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.day_event_layout);
TextView create = new TextView(parent.getContext());
create.setLayoutParams(new LayoutParams(0, 30));
create.setBackgroundColor(Color.BLUE);
create.setText("Test");
layout.addView(create);
count = count -1;
}
return convertView;
}
I am trying to add it to a LinearLayout in a ListView, hence the method you see in the code. One problem is that the TextView is not showing up when I run it. So I was thinking I need the layout_weight. Though, I am confused about one thing. What values are the width and height parameters of the LayoutParams constructor (dp or px)? I will also add the xml of where I am trying to add it at incase that is helpful:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/day_hour_side"
android:layout_width="wrap_content"
android:layout_height="62dp"
android:text="12AM"
android:background="#bebebe"
android:layout_weight="0"
android:textSize="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
<LinearLayout
android:id="#+id/day_event_layout"
android:layout_width="0dp"
android:layout_height="62dp"
android:layout_weight="1"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:orientation="horizontal" ></LinearLayout>
Firstly, the LayoutParams constructors use pixels (according to the documentation).
To set the layout_weight programmatically, you need to use LinearLayout.LayoutParams. ViewGroup.LayoutParams does not have a third argument, as you pointer out, but the former does.
Try this:
create.setLayoutParams(new LinearLayout.LayoutParams(0, 30, 1.0f));
ViewGroup.LayoutParams is a set of layout parameters for any View within any kind of layout (since these layouts are all ViewGroups). When you have a specific layout, for example a LinearLayout, you can use LinearLayout.LayoutParams to get access to things specific to that type of layout. In this case, the layout_weight is particular to LinearLayouts, therefore you must be using the LinearLayout.LayoutParams to access this weight parameter.
You should also remove the layout_weight="0" from the TextView in your XML. If this still doesn't fix it, give your LinearLayout a background color and see if it's even visible at all, then edit your OP with your findings and any changed/new code.
I have a UI, I build it dynamically. I should want to put some component in a xml resource file. So I do :
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
... in a file res/layout/titreitem.xml as I see anywhere. But I don't understand how get it to put inside my UI. So, inside activity.onCreate, I want to do something like :
RelativeLayout myBigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);
Use a LayoutInflater....The Entire Layout can be inflated, without dynamically creating it....
LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);
Approach seems to be little incorrect. You should put RelativeLayout to the xml as your TextView made, and inflate the whole xml. Afterwards, you will be free to add views to your layout. So, do this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+androi:id/relLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
In your activity:
setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);