I have a LinearLayout with some Views in it. Then I want to treat this element as a View, so I created a new class extending from LinearLayout. Now when I dynamically add a new instance of this class into the layout I see nothing. I believe I have to get the View somehow from my class, but don't know how. Is it possible to somehow assocciate this new class with an xml?
Update:
public class Task extends LinearLayout {
public Task(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
public Task(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
public Task(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
}
Then:
Task newTask = new Task(getActivity());
someLinearLayout.addView((View) newTask); // happens nothing
You can use different approaches like:
Inflating it into a View
Using <include> tag
Inflating:
public class InflatedView extends LinearLayout
{
public InflatedView(Context c)
{
super(c);
LayoutInflater.from(this).inflate(R.layout.your_other_layout, this);
}
//override other constructors too.
}
Now you can use this in your xmls like this:
<com.your.package.InflatedView android:layout_height="etc" other:attribute="here" />
Include:
Very simple, use include tag:
<include layout="#layout/your_other_layout"/>
Here are RomainGuy's layout tricks.
Related
I'm having trouble understanding why my extended layout isn't working. I made a class which goes
public class MyLayout extends RelativeLayout {
public MyLayout(Context context) {
super(context);
}
}
I wrote the XML as
<package.MyLayout
. . .
</package.MyLayout>
The method where I get the error is in the activity where I have called setContentView(R.layout.layout_relative).
I don't understand what I'm doing wrong because surely you inflate in the activity from the XML layout which is building on the custom class where I can make my overrides?
you need the other constructor, the one that takes two parameters:
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
which is used when the layout is inflated from the layout
You have specified the wrong constructors!
Android xml inflation uses
public RelativeLayout (Context context, AttributeSet attrs, int
defStyleAttr)
public RelativeLayout (Context context, AttributeSet
attrs, int defStyleAttr, int defStyleRes) since api 21
i want use all the component in android which having the same font type face, for that i am creating a individual custom class for each component like CustomTextView, CustomEditText, etc,..
So instead of creating a individual class for each component can i create a view CustomView class that will automatically apply style for all the components in android
Just declare your own TextView and use it in your XML, it should appear in the custom Views
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "chalk_board.ttf"));
}
Oh dam u want it globally for all views, so this is the wrong approach.... Sorry for that
You have at least 2 ways:
create your own TextView class and set fontFace in constructor
you can use custom LayoutInflater. And every time view gets inflated check that it is textView (or other view not extending textView but having font settings) and set correct fontFace settings.
does anybody know, how to get a referenced xml layout, programmatically (in code) for my custom widget. I have already created a custom declare-styleable, with my desired attributes and I know how to get ohter xml attribute values, like string or integers.
What I want to do is something like this:
<MyCustomView
xmlns:my="http://schemas.android.com/apk/res-auto"
id="#+id/view"
my:headerLayout="#layout/my_fancy_layout"
/>
So I want to retrieve my_fancy_layout programmatically and inflate that layout in the code of MyCustomView.
Any idea how to do that?
Edit: I guess I can retreive the resource id with
int resId = attrs.getAttributeResourceValue(androidns, "headerLayout", 0);
But whats the correct namespace if I MyCustomView is a library project and if I would like to use
xmlns:my="http://schemas.android.com/apk/res-auto"
Ok, i found the solution by myself:
you have to retrieve a TypedArray from yout AttributeSet.
than you can access your desired resource id with something like this:
TypedArray attrs = ... ;
int headerRes = attrs.getResourceId(R.styleable.MyCustomWidget_headerLayout, -1);
than you can inflate like usually:
LayoutInflater.from(context).inflate(headerRes, this);
You can indeed inflate your layout in the constructor of your custom view:
public class MyCustomView extends /* LinearLayout, RelativeLayout, etc. */ {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context, attrs);
}
protected void initView(Context context, attrs) {
LayoutInflater.from(context).inflate(attrs.getAttributeResourceValue("http://schemas.android.com/apk/res-auto", "headerLayout", 0), this, true);
}
}
This has been bothering me for a while, and none of my searching has yielded results. If I have a custom GUI element, I can use a LayoutInflater to inflate it as I would a normal component. The inflation call results in a call to my custom GUI element's constructor, and all is well.
However, what if I want to add a custom parameter to my element's constructor? Is there a way I can pass this parameter in using LayoutInflater?
For example:
In main xml, I have a holder for my layout:
<LinearLayout
android:id="#+id/myFrameLayoutHolder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
and a MyFrameLayout.xml file:
<com.example.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyFLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1 >
<!-- Cool custom stuff -->
</com.example.MyFrameLayout>
and an inflater call:
LayoutInflater MyInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myFLayoutHolder = (LinearLayout) findViewById(R.id.myFrameLayoutHolder);
MyFrameLayout L = ((MyFrameLayout) MyInflater.inflate(R.layout.MyFLayout, myFLayoutHolder, false));
myFLayoutHolder.addView(L);
If, in my class that extends FrameLayout, I add a parameter to my constructor, I get a crash:
public class MyFrameLayout extends FrameLayout {
private int myInt;
public MyFrameLayout(Context context) {
this(context, null);
}
public MyFrameLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0, 0);
}
public MyFrameLayout(Context context, AttributeSet attrs, int defStyle, int myParameter) {
super(context, attrs, defStyle);
myInt = myParameter;
//Amazing feats of initialization
}
}
Now, it's easy enough to work around this issue by defining a custom init method that I call right after layout inflation, but that seems clumsy to me. Is there a better way?
You cant define a constructor with your own parameter because your constructor signature conflicts with FrameLayout's own constructor signature and you are not calling super(context, attrs, defStyle);, instead you are calling super(context, attrs); which is incomplete for this constructor.
You must need to define all three native constructors exactly as they are:
FrameLayout(Context context)
FrameLayout(Context context, AttributeSet attrs)
FrameLayout(Context context, AttributeSet attrs, int defStyle)
What you can do is to use your own (custom) attributes in xml and then retrieve them in your MyFrameLayout's attrs object
If the custom component is inflate by XML file or inflate method. You dont´t pass elemnts in the construct because this is not support in android.
I want to create a custom view TestView class for which I can create object via new TestView().
A new view class however needs a AttributeSet object. From where do I get that AttributeSet and what does it have to include?
It's not mandatory, and most times you don't even have to worry about it as long as you provide constructors from View that pass them along to super().
public CustomView(Context context) // No Attributes in this one.
{
super(context);
// Your code here
}
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Your code here
}
public CustomView(Context context, AttributeSet attrs, int default_style)
{
super(context, attrs, default_style);
// Your code here
}
View takes care of doing the heavy lifting for dealing with all of the android:* attributes that you'd usually pass in when adding the view to a layout. Your constructors could make use of those attributes or your own if you've defined them:
<com.blrfl.CustomView
android:id="#+id/customid"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
blrfl:foo="bar"
blrfl:quux="bletch"
/>
Either of 3 constructor provided by view class can be implemented.. so providing constructor with attributeset is not mandatory.