I am trying to add my custom view to the main activity's xml.
I have created the following xml for the view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Testing custom view"
android:textSize="20dp" />
</LinearLayout>
And created the following class in which the above xml is inflated:
public class TestView extends LinearLayout {
public TestView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.test_name, null);
}
And in the xml of the main activity I have added the custom view like below:
<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="com.example.myapplication.MainActivity">
<com.example.myapplication.TestView
android:id="#+id/submit_area"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
When I am running the app, I am getting an empty screen. It's not showing the textview which displays "Testing custom view".
Any help would much appreciated.
Thanks
Your view inflation code in your custom view looks wrong.Try it the way given below.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_color_options, this, true);
Related
I'm trying to do something simple - I have a custom LinearLayout view that I want to move across the screen using the TranslateAnimation. To test out the base case, I put the code in the constructor of my custom view to translate it as soon as it is created. But nothing happens, and the view stays put. Any idea why? I have pasted the relevant code below:
My custom LinearLayout View:
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_view, this);
mText = (TextView) findViewById(R.id.my_text);
mImage = (ImageView) findViewById(R.id.my_picture);
//Test simple translate animation
TranslateAnimation translateMyView = new TranslateAnimation(10, 10, 5, 5);
translateMyView.setFillAfter(true);
translateMyView.setDuration(1000);
this.startAnimation(translateMyView);
}
The XML of this custom view:
<?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"
android:background="#drawable/border">
<TextView
android:id="#+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example Text"/>
<ImageView
android:id="#+id/my_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
And the way I have added this view in the main activity's view:
<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="${relativePackage}.${activityClass}"
android:id="#+id/main_view">
<com.pratikthaker.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
What am I missing?
I am trying to create a custom view, and I extends from LinearLayout:
public class CustomView extends LinearLayout {
private ImageView mPrevImgView;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
View root = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom, null);
this.addView(root);
mPrevImgView = root.findViewById(R.id.xx);
....
}
}
And the layout/custom.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/xx"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
.......
</LinearLayout>
Then I try to use it this way:
<CustomView android:layout_width.....>
...
</CustomView>
This works as expected.
However it seems that the CustomView itself is a LinearLayout, and it then add another LinearLayout in the constructor, this cause two LinearLayout nested which is unnecessary. I wonder if it is possible to avoid this?
// try this way
**custom.xml**
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"/>
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));
I am trying to create a compound viewgroup after inflating the group from an XML file.
The viewgroup is composed as: A LinearLayout Root, 2 child LinearLayouts. I am able to see the layout correctly in the layout editor; however, when I attempt to add a view (say a button) from the editor, the view does not show up and the application immediately force closes. I was told i may need to Override the onLayout method to correctly draw the view components but I'm am fairly confused.
My Class:
public class FocusBox extends LinearLayout {
private LinearLayout rootLayout,
contentLayout,
topLayout;
public FocusBox(Context context)
{
super(context, null);
}
public FocusBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.focus_box, this);
rootLayout = (LinearLayout)findViewById(R.id.focusBoxParent);
contentLayout = (LinearLayout)findViewById(R.id.focusBottom);
topLayout = (LinearLayout)findViewById(R.id.focusTop);
}
}
And the xml:
<?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:padding="4dp"
android:id="#+id/focusBoxParent"
android:orientation="vertical">
<LinearLayout
android:background="#drawable/gradients"
android:layout_weight=".1"
android:gravity="center"
android:id="#+id/focusTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="TextView"
android:id="#+id/focusTitle"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".9"
android:id="#+id/focusBottom"
android:background="#drawable/gradient2"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Generally, if you inflate a layout from a valid XML you shouldn't get an error. You should do a clean build and re-deploy the app again.
Also check if you're using the correct class in the import statement in other classes (you could be using a FocusBox from some 3rd-party library instead of the one you made)
I am trying to refer to a custom View in the helloWorld XML layout but I get the following exception:
Error inflating class acme.my.MyTextView.
However, I am able to instantiate the view and add it to the main content view manually. The custom View is built from it's own XML layout. How do I get this to work?
public class MyTextView extends LinearLayout {
MyTextView(Context context){
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
MyTextView(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_text_view,this);
}
}
<?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="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<view class = "acme.my.MyTextView"
android:id="#+id/myView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
The constructors aren't public.
:(
you have to call it like this
<com.example.MyLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Also make your constructors public