Can someone tell me if this is a bug in the SDK/IDE:
Any custom or extended layout I add to my layout XML causes the IDE to ignore the fact that there are any child views of that layout (they just disappear from the outline view/window), thus making them uneditable via the properties view/window. (I need to extend a layout to make onSetAlpha() public)
FYI: I'm developing for Android 1.5 and up, using all the latest plug-ins/updates in Eclipse
Here is a simple example of a layout XML and the extended Layout that causes this error.
[Extended Layout]
package com.test;
public class CustomLinearLayout extends LinearLayout
{
public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLinearLayout(Context context) {
super(context);
}
}
[Simple layout XML]
<?xml version="1.0" encoding="utf-8"?>
<com.test.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</com.test.CustomLinearLayout>
ImageView01 is not visible or editable in the properties or outline views/windows.
Thanks.
When you want to edit the properties of view just replace your com.test.CustomLinearLayout
with LinearLayout.
Then you can see the view in properties and can edit the properties. After you finish editing then revert the tag to original one that is from LinearLayout to com.test.CustomLinearLayout
Related
I am trying to create a custom preference used in a fragment. That preference will have an icon, a long text and a hyperlink to link somewhere.
For that I create a class that extends androidx.preference.Preference
The constructor is this:
public MyCustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWidgetLayoutResource(R.layout.my_custom_preference_layout);
}
I get it to work correctly and all that, but the problem is that its height is way too short. The preference size doesn't fit its content. It seems like it is always the same size. About two lines of text only.
I have tried with
android:singleLine="false"
android:minLines="50"
but nothing. The preference is displayed way too short.
Is there any limitation in the height of custom preferences?
I use a RelativeLayout for the layout and add the controls I need to it.
that Relative Layout have these values:
android:layout_width="match_parent"
android:layout_height="wrap_content"
I have tried setting match_parent to both, even setting a fixed value, for example 500dp, but it is always displayed in small height.
I have created a simple custom TextView. The layout designer however, won't let me preview those custom TextViews, only MockView-Blocks are being rendered.
public class MainTextView : TextView
{
public Typeface typeface;
public MainTextView (Context context) : base(context, null)
{
InitializeView(context);
}
...
}
Is how my class goes. I would like to preview my custom TextViews, but how can I do that?
If you are in the layout file, you can use:
<(insert your namespace for MainTextView here).MainTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world" />
That should work, you just have to put the namespace that your textview is a part of.
I would also like to note that I've had issues with Visual Studio failing to render my views no matter what I do, so in those situations there's not much you can do.
After a lot search I found a great article that describes how to make compound objects. The recipe works great, Eclipse shows it in the custom made widgets. Fantastic.
http://mobile.cs.fsu.edu/creating-a-simple-compound-component-in-android/
Just one problem, I need a few of them. When I added a few into a layout, the resource id for the enclosing layer/object bumps as expected; that is, the new object receives new ids auomagically. The issue is the components inside the main layout do not; so if I need to use finViewById to get the view for internal components, they all have the same id. Does anyone know how to automagically assign different id for the internal components of the complex widget? I would try to assign them manually, but Eclipse does not show the internal structure of a compound object; so no help there.
-- in response to Xaver answer
Yes, at least I think I did. If I follow the example in the link I have something alike; now I see what you are saying, I got the views as just by adding findviewbyId to custom class, as this
public class HorizontalNumberPicker extends LinearLayout {
public HorizontalNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.horizontal_number_picker, this);
View v = findViewbyID(R.id.btn_minus); //
}
}
What I cannot understand and I most likely have wrong is how to hook my code to two objects, that is following the example in the link
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<example.example.HorizontalNumberPicker
android:id ="#+id/horizontal_number_picker"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" />
<example.example.HorizontalNumberPicker2
android:id ="#+id/horizontal_number_picker"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" />
</FrameLayout>
How does HorizontalNumberPicker construtor know is linked to object horizontal_number_picker or horizontal_number_picker2?
if in in my activity I add
HorizontalNumberPicker obj1 = HorizontalNumberPicker (this,null);
what links to horizontal_number_picker or horizontal_number_picker2?
By the way how do I pass the Attributeset?
I've created a custom view which extends RelativeLayout, and I want to preview how it looks in the designer.
The java is something like this:
// The view for a snap in the search/browse fragment.
public class MyView extends RelativeLayout
{
public MyView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.the_layout, this);
}
public void setData(String text)
{
mText.setText(text);
}
// *** Views ***
private TextView mText;
#Override
protected void onFinishInflate()
{
super.onFinishInflate();
mText = (TextView)findViewById(R.id.text);
}
}
And the XML is like this:
<?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="match_parent" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- (And lots more views) -->
</RelativeLayout>
There are a few problems with this however:
This actually creates a RelativeLayout within a RelativeLayout which is pointless. It can be solved by changing the XML <RelativeLayout> to a <merge> but then I can't preview the layout at all!
I want to use the isInEditor() function (or whatever it is called) in the java to add some sample data for previewing purposes, but I can't find a way to tell the XML editor that it should display a preview of my class instead of the actual XML.
One unsatisfying solution I can think of is to create an empty preview.xml file with only <com.foo.bar.MyView/> in it... But that seems kind of silly. Is there a better way? (I don't really care about editing as much as previewing, since - let's face it - Eclipse/ADT are way too slow and flaky to make graphical layout editing usable.)
If I understand your problem correctly I would say that the solution is to just replace the "RelativeLayout" tags (or any other tag for that matter) in your xml layout with "your.packagename.MyView" as in:
<your.packagename.MyView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- (And lots more views) -->
</your.packagename.MyView>
If you'll get any exceptions regarding MyView class while running your app, add all the missing super/parent constructors.
I do this for almost all of my custom xml layouts. Extending your class with RelativeLayout, LinearLayout or any other GUI class also gives you great controll over how your GUI should behave (because you can also override parent methods etc.).
I am making a small app for Android, where I have a RelativeLayout, which amongst other things contains a custom ImageView. In my Java code I have this class:
package com.example.android.helloactivity;
class ArrowImageView extends ImageView {
public ArrowImageView(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(10,10,10,null);
}
}
Then in my RelativeLayout xml I have the following:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFF"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button ...... />
<TextView ......./>
<com.example.android.helloactivity.ArrowImageView
android:id="#+id/hello_activity_bearingarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
When I run my main class (not shown here) then my program crashes. If I omit the xml reference to ArrowImageView, then it does not crash.
Am I referring to my custom class te wrong way, or what is going on?
When extending the View widgets, if you plan to use them in XML layouts you need to also override the constructors that take the AttributeSet argument.
First don't extend ImageView but just View, unless there is something special about ImageView that you know and want to use.
Second, as Leffel said, you need to state attribute set like this
public ArrowImageView(Context context, AttributeSet set) {
super(context, set);
}
Also you may need to give some size to the custom view by setting witdh and height to something like 100dp. I am not sure what a custom View can "wrap" when there is no other views inside.
Thanks for the answers. I found a bug in the onPaint method itself which still made things crash. After implementing your suggestions and changing the onPaint into:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.MAGENTA);
canvas.drawCircle(10,10,10,paint);
}
everything worked!
Thank you very much for your help :-)