In my foo_layout.xml file I have a subclassed RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.android.myapp.FooView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pegboard_table"
android:orientation="vertical"
android:scaleType="fitXY"
>
<ImageView
android:id="#+id/triangular"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/pegboard_board"
android:scaleType="fitXY"
android:gravity="center"
android:visibility="invisible"
/>
<Chronometer
android:id="#+id/timer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/timer_display"
android:textSize="40sp"
android:textColor="#000"
android:layout_alignParentTop="true"
android:gravity="center"
android:visibility="invisible"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/board_table"
android:visibility="invisible"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#drawable/tab_bar">
<!-- android:layout_alignLeft="#id/options_tab"-->
<ImageView
android:id="#+id/game_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/game_select" android:paddingLeft="15sp"/>
<ImageView
android:id="#+id/undo_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/game_select"
android:layout_weight="1"
android:src="#drawable/undo"
/>
<ImageView
android:id="#+id/settings_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/undo_select"
android:layout_weight="1"
android:src="#drawable/settings"
/>
<ImageView
android:id="#+id/info_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/settings_select"
android:layout_weight="1"
android:src="#drawable/info"
/>
</LinearLayout>
</com.android.myapp.FooView>
</FrameLayout>
Then I have a java file FooView.java with the class extending
RelativeLayout.
package com.android.myapp;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
public class FooView extends RelativeLayout {
public FooView(Context context) {
super(context);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs) {
super(context, attrs);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
fillTable();
// TODO Auto-generated constructor stub
}
public void fillTable() {
int board = this.getChildCount();
View boardView = findViewById(R.id.board_table);
if (board > 0)
;
}
}
board is always 0.
boardView is always null.
This doesn't seem like the right behavior. I've tried it with other views inside the FooView hierarchy and findViewById() always returns null.
For the life of me I can't figure out what I'm doing wrong.
First, your widget is attempting to reference widgets outside of itself. Calling findViewById() will get you children of your widget, not widgets elsewhere in the layout.
Second, you are trying to do this from your constructor. That is not safe. Please wait until onFinishInflate().
Third, you may not want to hardwire in R.id values for those other widgets, but allow those to be configurable, either through view attributes or setters. To access those widgets, given the configured IDs, you would call findViewById() not on yourself, but on your activity, obtained from getContext().
The image view and chronometer view are not part of the custom view until you add them using view.setlayout in the constructor.
Related
I am writing an app on Android Studio and I am trying to create a custom view with a specific form I need, something like:
Custom View Example
(I used different colors to show how the layout is supposed to go)
Here's the xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="2"
android:background="#android:color/holo_red_dark">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/hourLbl"
android:layout_gravity="center"
android:background="#android:color/holo_blue_bright"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/minuteLbl"
android:layout_gravity="center"
android:background="#android:color/holo_orange_dark"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/actionName"
android:layout_weight="1"
android:gravity="center"
android:background="#android:color/darker_gray"/>
</LinearLayout>
The problem comes when I try to add this view to another layout in the app.. It comes as a blank square. I have been trying to look for an answer on how to properly do this, but nothing helps.
This is what I have in the xml of the activity where I want the view (ActionView):
<RelativeLayout 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=".HomeActivity">
<...ActionView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="#+id/firstAction" />
</RelativeLayout>
Here's the ActionView's constructors I have (not sure what to do with them to be honest, just copied them from a tutorial):
public ActionView(Context context) {
super(context);
}
public ActionView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ActionView,
0, 0);
try {
mHourLbl = a.getString(R.styleable.ActionView_hourLbl);
mMinuteLbl = a.getString(R.styleable.ActionView_minuteLbl);
mActionName = a.getString(R.styleable.ActionView_actionName);
} finally {
a.recycle();
}
}
public ActionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
I think you're missing the most important part. You're not tying your layout to your ActionView object...
Add:
LayoutInflater.from(context).inflate(R.layout.<xml_of_action_view>, this, true);
inside your ActionView constructor.
I am trying CardsUI, I have following code: Including NowActivity Code
NowActivity.java where I define the behaviour
package com.example.rushdesktop.testingweather;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
public class NowActivity extends LinearLayout implements OnGlobalLayoutListener {
public NowActivity(Context context, AttributeSet attrs)
{ super(context, attrs); initLayoutObserver(); }
public NowActivity(Context context)
{ super(context); initLayoutObserver(); }
private void initLayoutObserver()
{ // force vertical orientation and add observer
setOrientation(LinearLayout.VERTICAL);
getViewTreeObserver().addOnGlobalLayoutListener(this); }
#Override public void onGlobalLayout()
{
getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int heightPx =getContext().getResources().getDisplayMetrics().heightPixels;
boolean inversed = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++)
{
View child = getChildAt(i);
int[] location = new int[2];
child.getLocationOnScreen(location);
if (location[1] > heightPx) { break; }
if (!inversed) {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_left));
}
else {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_right));
}
inversed = !inversed;
}
}
I use it in my main activity xml:
<ScrollView 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" android:fillViewport="true">
<com.example.rushdesktop.testingweather.NowActivity android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e3e3e3" android:orientation="vertical" >
<TextView android:id="#+id/txtView" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView1" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView2" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView3" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView4" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
</com.example.rushdesktop.testingweather.NowActivity>
</ScrollView>
As in the code I use the Java class in my main activity xml. But when I build my code I get error showing the class was not found in the XML. Please help me with the issue.
Here is the link to my full android studio code:
https://drive.google.com/file/d/0B6P-8t-14MK-dEZ0bHJqRzBvUmVaSW5ySTR3My1mN0U1OThV/edit?usp=sharing
Thanks in advance.
Be sure that the only child of the scroll layout (com.example.rushdesktop.testingweather.NowActivity) inherits some sort of layout (i.e. RelativeLayout, FrameLayout...) does it?
in your xml layout you need to use the View, not the activity.. the activity will set the layout it uses via setContentView(R.layout.[your layout])
in your xml layout, if you switch to graphical editor view you should be able to see the view you want listen in the custom & library views tab (in eclipse), providing you have included the library in the project build
I'm having an issue where I have a ScrollView content inside of it. In the event that the content doesn't all fit on screen, I want it to scroll. However, if it all fits I don't want it to scroll.
As is, the scrollviewer scrolls quite a bit and all the content can be scrolled way off screen.
How does one get the scrollview to not scroll when all the content fits on the screen?
<ScrollView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/productImageView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:id="#+id/detailsScrollView"
android:paddingRight="24dp"
android:paddingLeft="24dp"
android:clipChildren="true"
android:fillViewport="true"
android:measureAllChildren="true">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1"
android:paddingTop="24dp"
android:paddingBottom="24dp">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/patternTitleTextView" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/designerNameTextView"
android:textColor="#color/default_gray" />
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/descriptionTextView"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:minLines="200"
android:scrollbars="none"
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none" />
</LinearLayout>
</ScrollView>
You probably need to create a custom ScrollView:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
}
Then you can do something similar to this in your code:
CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
myScrollView.setEnableScrolling(false); // disable scrolling
myScrollView.setEnableScrolling(true); // enable scrolling
Of course, it will be up to you to decide when exactly you need to enable/disable scrolling.
The problem in my case was with one of the TextViews.
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/descriptionTextView"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:minLines="200"
android:scrollbars="none"
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none" />
I shouldn't have set minLines or set the layout_weight. After removing those two things the scrollview did exactly what I expected it to.
I am trying to create a custom button layout. I created an xml file "custom_button.xml" which I then inflate in the "ButtonLanguageSelection" class. In this class I added the onClickListener. I added this class to the "main_activity.xml". It behaves like it should except it won't receive any touch events. Then I copied the code from "custom_button.xml" and added it directly without inflating it, I just added the android:onClick parameter and in this way it worked. I can't find out what would be the problem, the layouts are the same.
Has some of you have similar problems? What could be the problem? I attached the code if it helps someone to find the problem.
Thank you for any help!
custom_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
ButtonLanguageSelection
public class ButtonLanguageSelection extends LinearLayout
{
private TextView introductoryTextView;
private TextView language;
private final String TAG = "ButtonLanguageSelection";
public ButtonLanguageSelection(Context context) {
super(context);
}
public ButtonLanguageSelection(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.button_language_selection, this);
introductoryTextView = (TextView)findViewById(R.id.textview_select_language);
language = (TextView)findViewById(R.id.textview_language);
this.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onclick");
}
});
}
public ButtonLanguageSelection(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setIntroductoryTextView(String s)
{
introductoryTextView.setText(s);
}
public void setLanguage(String s)
{
language.setText(s);
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/screen_welcome_bg">
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Slovene"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Italian"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<!--<defaultpackage.ButtonLanguageSelection-->
<!--android:id="#+id/ButtonLanguageSelection_English"-->
<!--android:layout_width="341px"-->
<!--android:layout_height="260px" />-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="341px"
android:layout_height="260px"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
android:onClick="sendMessage"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
I think the problem is writing all the init code in only one constructor. Consider making an init function and call it from all the constructors. Also consider setting the onClick events listeners in your activity rather than than the layout constructor itself.
Why do some views in a linearlayout within a scrollview fill the parent when fill_parent is set when inflated from xml directly but don't when added via a custom component that inflates the same xml?
The result of inflating the same xml in two different places:
The top item of the two is as a result of inflating the xml in a class that extends linearLayout and then adding that class to the linearLayout inside the scroll view. This view isn't filling the parent and I can't work out why.
The bottom item is the result of inflating the xml directly in the activity that this is all contained within. It appears as I expected.
This is the code that adds the two views in the activity:
scrollList.addView(new CommunityTaskListItem(this, null));
scrollList.addView(View.inflate(getBaseContext(), R.layout.communitytask, null));
This is the code that inflates the activity within a custom component class "CommunityTaskListItem":
View communityTask = View.inflate(context, R.layout.communitytask, null);
addView(communityTask);
I assume the xml is okay because it works fine when inflated directly in the activity. My question is why the fill_parent property seems to be lost when the xml is inflated in a custom component's class?
For good measure, here is the xml being inflated: EDIT: I gave you the wrong one before!!! Here's the right one:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:background="#drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
<TextView android:layout_height="wrap_content" style="#style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="#+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="#style/CommunityTaskText" android:id="#+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
</LinearLayout>
</LinearLayout>
I'd like to keep the custom component and not inflate the xml within my activity if I could.
EDIT: Code where CommunityTaskListItem is inflated:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CommunityTaskListItem extends LinearLayout {
TextView textViewCommunityTaskTimes;
TextView textViewCommunityTaskDescription;
public CommunityTaskListItem(Context context, AttributeSet attrs) {
super(context, attrs);
View communityTask = View.inflate(context, R.layout.communitytask, null);
addView(communityTask);
textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
}
...
EDIT: All working now! Here's the final code for anyone else incase they have a similar issue:
Contructor for the custom object:
public CommunityTaskListItem(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
addView(inflate(context, R.layout.communitytask, null));
textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
}
Custom object xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:background="#drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
<TextView android:layout_height="wrap_content" style="#style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="#+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="#style/CommunityTaskText" android:id="#+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
</LinearLayout>
</LinearLayout>
To be honest I don't know exactly what the mistake I was making was so if someone could make that clear I'd be very grateful.
The first line should be:
scrollList.addView(new CommunityTaskListItem(this, null)
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
You call inflate() inside CommunityTaskListItem class and inflated view becomes a child view of the CommunityTaskListItem view. But you don't set width and height for this view so it doesn't match its parent's width.
EDIT: I think your XML should look like this:
<?xml version="1.0" encoding="utf-8"?>
<packagename.CommunityTaskListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:background="#drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
<TextView android:layout_height="wrap_content" style="#style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="#+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="#style/CommunityTaskText" android:id="#+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
</LinearLayout>
</packagename.CommunityTaskListItem>
where packagename is the name of the package of the CommunityTaskListItem class.
In this case you should change the CommunityTaskListItem:
public class CommunityTaskListItem extends LinearLayout {
TextView textViewCommunityTaskTimes;
TextView textViewCommunityTaskDescription;
public CommunityTaskListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onFinishInflate() {
textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
}
// ....
}
This class cannot be created using its constructor. It can be only inflated.
The second way is to inflate children in the constructor like you do. But the XML should differ:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:background="#drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
<TextView android:layout_height="wrap_content" style="#style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="#+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="#style/CommunityTaskText" android:id="#+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
</LinearLayout>
</merge>
The CommunityTaskListItem will be:
public class CommunityTaskListItem extends LinearLayout {
TextView textViewCommunityTaskTimes;
TextView textViewCommunityTaskDescription;
public CommunityTaskListItem(Context context) {
this(context, null);
}
public CommunityTaskListItem(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
inflate(context, R.layout.communitytask);
textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
}
// ....
}
This class can be created using its contructor and inflated from an XML. But if you want to inflate it, you should create a separate XML file. Let's call it task.xml.
<?xml version="1.0" encoding="utf-8"?>
<packagename.CommunityTaskListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
And that's how you can use it:
scrollList.addView(new CommunityTaskListItem(this),
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scrollList.addView(View.inflate(getBaseContext(), R.layout.task, null));