Issue
I have a main activity layout, which has a CollapsingToolbarLayout
, inside which I have a custom layout/view. I added this custom layout to the CollapsingToolbar
using the 'include' tag, and it displays fine, but I'm failing inflating the custom layout from the activity. This:
RelativeLayout r = (RelativeLayout) findViewById(R.id.create_quote_toolbar_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.create_quote_toolbar, r, false);
TextView T = (TextView)v.findViewById(R.id.lineTotal);
T.setText("sdrsfLineTotal");
does not seem to work.
main_activity_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".CreateQuoteActivity" >
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="100dp"
app:expandedTitleMarginStart="48dp" >
<include layout="@layout/create_quote_toolbar"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:clipToPadding="false" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
custom_toolbar_layout.xml:
<?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="400dp"
android:id="@+id/create_quote_toolbar_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/deliverToAddress"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/phoneNumber"
android:layout_below="@+id/deliverToAddress"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/name"
android:layout_below="@+id/phoneNumber"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/date"
android:layout_below="@+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valid Until:"
android:id="@+id/textView5"
android:layout_below="@+id/date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp"
android:textSize="16dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/expirationDate"
android:layout_below="@+id/date"
android:layout_toRightOf="@+id/textView5"
android:layout_toEndOf="@+id/textView5"
android:hint="dd/mm/yyyy"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/total"
android:layout_below="@+id/expirationDate"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="16dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/itemDescription"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Item Description"
android:textSize="16dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quantity"
android:layout_alignTop="@+id/itemDescription"
android:layout_toRightOf="@+id/itemDescription"
android:layout_toEndOf="@+id/itemDescription"
android:hint="Quantity"
android:textSize="16dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unitPrice"
android:layout_below="@+id/quantity"
android:layout_toRightOf="@+id/itemDescription"
android:layout_toEndOf="@+id/itemDescription"
android:hint="Unit Price"
android:textSize="16dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/discount"
android:layout_below="@+id/unitPrice"
android:layout_alignLeft="@+id/unitPrice"
android:layout_alignStart="@+id/unitPrice"
android:hint="Discount"
android:textSize="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/lineTotal"
android:textSize="16dp"
android:layout_below="@+id/discount"
android:layout_alignRight="@+id/discount"
android:layout_alignEnd="@+id/discount"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="@+id/addItemButton"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/expirationDate"
android:layout_toStartOf="@+id/expirationDate"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Quote"
android:id="@+id/respondButton"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/total"
android:layout_alignEnd="@+id/total"/>
</RelativeLayout>
Solution
If you have set the root view in the activity with setContentView there is no reason for you to inflate the view. When you use the tag in the xml it will automatically inflate the view when you set it in the Activity.
If you want to find a view you would then simply do a findViewById
on the id as normally for the other ids.
For example, you would in your onCreate method do:
setContentView(R.layout.main_activity_layout)
After this you can simply do
findViewById(create_quote_toolbar_layout)
And that will find the RelativeLayout in the view you included.
Answered By - Henrik Gyllensvärd
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.