I'm trying to inflate a LinearLayout to build a customized section on my screen.
String txt = "testing";
LinearLayout rowLink = (LinearLayout)getLayoutInflater().inflate(R.layout.additional_link, null);
TextView tvCsAddLink = (TextView)findViewById(R.id.tvCsAddLink);
tvCsAddLink.setText(txt);
// adding this inflated layout to an existing layout
myLinearLayout.addView(rowLink);
The content of my additional_link.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llCsAddLink"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/white_row"
android:clickable="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/youtube_icon" />
<TextView
android:id="#+id/tvCsAddLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10px"
android:paddingRight="20px"
android:textSize="16dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_gravity="center_vertical" />
</LinearLayout>
I'm getting a NullPointerException on the line:
tvCsAddLink.setText(txt);
Use this to get TextView from layout.
TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink);
you have inflated your XML on your linearLayout rowLink , and you try to find the TextView from the layout of your Activity ( usualy main.xml ) , so you need to find your textView on your layout rowLink like this :
TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink);
Call findViewById(R.id.tvCsAddLink) on your inflated LinearLayoutrowLink, now you are searching it in the current activity.
Related
This is the item I'm inflating:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/background_border"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account name"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balance: £61.43"
android:textSize="25sp"
android:paddingTop="40dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_keyboard_arrow_right_black_24dp"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
And I'm inflating it using the following code:
LinearLayout parent = (LinearLayout) findViewById(R.id.linearL);
view = getLayoutInflater().inflate(R.layout.menu_item, parent,false);
parent.addView(view);
Say if I inflated the following item three times, to create three menu items - how would I reference each item individually?
For example, if I wanted to change the text of the Account name in the first item of the menu, how would I reference that it's in the first item of the menu, instead of the second or third?
Say if I inflated the following item three times, to create three menu
items - how would I reference each item individually?
Use parent.getChildCount() and parent.getChildAt() for accessing Views from any R.layout.menu_item layout like:
Add layout multiple times:
//First time
parent.addView(view);
//Second time
parent.addView(view);
Get first added R.layout.menu_item layout view:
View firstView=parent.getChildAt(0);
In same way get other child View of parent layout.
Access child Views from firstView using :
View id if added in xml:
TextView textView=(TextView)firstView.findViewById(R.id.textViewID);
or using getChildAt method:
TextView textView=(TextView)firstView.getChildAt(0);
Access other views in same way.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/background_border"
android:padding="10dp"
>
<TextView
android:id="#+id/account_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account name"
android:textSize="25sp"/>
<TextView
android:id="#+id/balance_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Balance: £61.43"
android:textSize="25sp"
android:paddingTop="40dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_keyboard_arrow_right_black_24dp"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"/>
</RelativeLayout>
And in code you will have some thing like
TextView textview = (TextView)relativeLayoutFromXml.findById(R.id.balance_text_view);
textview.setText("sadsasa ");
If your layout is inside activity xml you should access the textview like this:
TextView textview = (TextView) findViewById(R.id.balance_text_view);
textview.setText("sadsasa ");
Dynamically adding TextViews to this layout results in text not being wrapped.
<!-- R.layout.description_card -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout_InfoShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/table_row_padding_bottom" >
<TableLayout
android:background="#drawable/card_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/TableRow_DescriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/DescriptionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/page_name_size"
android:text="Loading data..." />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/DescriptionTitle">
<LinearLayout android:id="#+id/LinearLayout_Description"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Dynamically added TextView here -->
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
I dynamically add the TextViews to this layout using these methods:
View view = inflater.inflate(R.layout.description_card, null);
TextView title = (TextView) view.findViewById(R.id.DescriptionTitle);
LinearLayout description = (LinearLayout) view.findViewById(R.id.LinearLayout_Description);
ArrayList<TextView> textViews = des.getText();
Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()){
TextView textView = iter.next();
description.addView(textView);
}
This results in my textviews not wrapping onto new lines, even though it works perfectly fine if I include the TextView directly in the XML file.
It results in this:
Edit:
TextView layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextViewDescription"
style="#style/AppTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="8dp"
android:text="Loading data..."
android:textSize="#dimen/description_size"
android:layout_weight="1"
android:ellipsize="none"
android:scrollHorizontally="false" />
You probably need to set it to multi line. Try
setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);
If you have other InputType attributes, you'll need to "or" them together.
At this part of your code, you are getting your TextView array:
ArrayList<TextView> textViews = des.getText();
And we don't see how those TextView's are created. But I assume you did not set single_line or max_lines to TextViews.
Try this code while adding your TextViews to your LinearLayout:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
while (iter.hasNext()) {
TextView textView = iter.next();
description.addView(textView, params);
}
Edit:
Use below layout while creating your TextViews(I've removed some attrs and style.):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="8dp"
android:text="Loading data..."
android:textSize="#dimen/description_size"/>
Edit2: Change your layout_width param to wrap_content. Also see updated param at code above. (This worked for me)
<LinearLayout android:id="#+id/LinearLayout_Description"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Dynamically added TextView here -->
</LinearLayout>
Try using something other than TableLayout. I'm not very experienced with TableLayout but I think it needs the child views to have relatively short and consistent widths.
Instead, try using LinearLayout, like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout_InfoShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/table_row_padding_bottom" >
<TextView
android:id="#+id/DescriptionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="#dimen/page_name_size"
android:text="Loading data..." />
<!-- Dynamically added TextView here -->
</LinearLayout>
And add the views with more-or-less the same code you had before. Note I changed the parent view for supporting the adds -- outerLayout.
View view = inflater.inflate(R.layout.description_card, null);
TextView outerView = (TextView) view.findViewById(R.id.LinearLayout_InfoShow);
ArrayList<TextView> textViews = des.getText();
Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()) {
TextView textView = iter.next();
outerView.addView(textView);
}
Found the reason.
The inflater needed a ViewGroup for the layout params to take effect.
I had to change the way I pass the text through to the iterator but this is basically what needed changing:
TextView textView = (TextView) inflater.inflate(text.getViewID(), description, false);
For the problem...
When you have
<TableLayout>
<TableRow>
<TextView>
</TextView>
</TableRow>
</TableLayout>
to make the text appear without truncation,
add
android:layout_weight="1"
inside the textview
<TextView>
</TextView>
I am working on an Android application.In my app I have change the textview alignment dynamically. The textview is in relativelayout.
the following is my sample xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/all"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
>
<TextView android:id="#+id/snt_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/Gold"
android:text="df"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="3dp"/>
</RelativeLayout>
And in java i did the following
View vi = inflater.inflate(R.layout.smsconversationsent_row, null);
RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all);
TextView message=(TextView)vi.findViewById(R.id.snd_txt);
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)message.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
message.setLayoutParams(params);
But I am getting nullpointer exception for message.getlayoutparams().
Reason of nullpointer exception is the following: You're trying to find R.id.snd_txt, but in layout You have snt_txt, so it's impossible to find view with snd_txt id, because it doesn't exists in the layout.
I'm trying to insert a LinearLayout inside another LinearLayout. I don't know if what I am doing is right or not. I need to try it this way, without using inflation.
LinearLayout address2;
address2 = new LinearLayout(this);
address2 = (LinearLayout)findViewById(R.id.sfsp2_layout);
LinearLayout teste3 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);
LinearLayout teste4 = (LinearLayout)teste3.findViewWithTag("teste");
teste4.addView(address2);
LinearLayout teste3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/se_contentAdressPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:background="#drawable/background_tile_address_postal"
android:orientation="vertical" >
<include
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/socio_form_structured_postal" />
LinearLayout teste4
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical"
android:tag="teste" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp_layout_horizontal"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:layout_gravity="right"
android:background="#drawable/background_tile"
android:orientation="horizontal"
android:tag="teste" >
<Button
android:id="#+id/sfsp_btStructuredPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:hint="#string/sfsp_btStructuredPostal" /> .......
LinearLayour address2 ( The layout that i need to insert in layout4)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfsp2_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_tile"
android:orientation="vertical" >
<EditText
android:id="#+id/sfsp2_etStructuredPostalApartado"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="55dp"
android:layout_marginTop="2dp"
android:layout_weight="0.14"
android:ems="10"
android:hint="#string/sfsp2_etStructuredPostalApartado"
android:inputType="textMultiLine"
android:scrollHorizontally="false" >
The LinearLayout "teste4" is inside "teste3". I need to insert LinearLayout "address2" inside "teste4". Can you tell me what I'm doing wrong?.
I think there is some misunderstanding here about how the findViewById works. I'll try to explain it below. Hope this helps, let me know if it doesn't.
There are two possible findViewById() that can be used.
Activity.findViewById(): this can be used to find a view in the current activity's main content view -- that is, the view that was set using setContentView(). It cannot be used for any other purpose. For example, it cannot find a view from any other layout file -- the function simply would have no way of knowing how to find it. As far as I can tell, this is the function you're trying to use. If the last XML file you have is not the main layout, then the line address2 = (LinearLayout)findViewById(R.id.sfsp2_layout) will fail.
View.findViewById(): this function can be used to find a view that is contained in some other view. E.g. if you have view1 that contains view2, and view2 has ID some_id, then you can find view2 by calling view1.findViewById(R.id.some_id). In order for this to work, view1 has to be fully initialized. E.g. if view1's description is in XML, it has to be fully inflated first.
In essence, if you want to work with a view that is described in a separate XML file, you have to inflate it first. I don't think there is a way around it.
Here is my Prent layout (main.xml)
<!-- ============================================================ -->
<!-- All Employees are added here (added and More then one) -->
<!-- ============================================================ -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<!-- ++++++++ here extra layout is added programmatically -->
</LinearLayout>
Here is my another layout file that I want as child, say child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_left"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:maxLines="2"
android:text="hello"/>
<TextView
android:id="#+id/list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:text="Photos"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn_right"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
Now in my Main activity I am calling the main.xml and based on the for loop condition I want to add the layout of the child.xml and every time I want to set the different value of the TextView of child.xml
So how it is Possible?
I have done like this:
private void doCalculationForMultipleEmployee() {
singleEmployee.setVisibility(View.GONE);
for (int i = 0; i<=tempEmployerList.size()-1; i++) {
View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);
((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);
// customize repeatedLayout with other data
myLinearLayout.addChild(repeatedLayout);
}
}
But after doing that I got syntax error at .inflate and at .addChild
So where have I gone wrong? Please help me by some code to add it by for loop.
Just create a loop like below:
LinearLayout parent=findViewById(R.id.myLayout);
for(int i=0;i<list.size();i++)
{
LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
TextView txt= (TextView)llItem.findViewById(R.id.title);
txt.setText(list.get(i));
parent.add(llItem);
}
Inflate the view then add to the parent
LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);
Repeat as many times as necessary or use a loop
If i understood your question correctly you need to display button, textview and button side by side in your layout.
If you want this you just take listview and use custom adapter, with this you can display button, text and button side by side