How to render custom view in designer? - android

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.

Related

Reading Android attributes on my custom view

I have created a custom layout class (extends RelativeLayout) and have a TextView as part of the layout.
I want to apply the properties declared in XML to my TextView, is there anyway I can read the android attributes (not my custom attributes, that part is already taken care of).
For example in my XML I'll have this:
<my.custom.MyLayout
android:layout_width="100dp"
android:layout_height="20dp"
android:text="SomeText" />
I want to read the text attribute and apply it to my TextView (currently it is being applied to the RelativeLayout) instead of creating my own attribute and reading it.
My custom layout is something like this :
public class MyLayout extends RelativeLayout {
private TextView textView;
public void MyLayout(final Context context, final AttributeSet attrs) {
/**Read android attributes and apply it to TextView **/
??
}
My current solution is creating custom attributes and reading them, but I feel that is not a good solution as I'll be duplicating every attribute declared to TextView.
More info about my current solution.
I have a custom attribute called myText which I use to apply the text declared in XML to my TextView.
In my layout XML :
myNameSpace:myText="SomeText"
And read it in my Java class :
String text= a.getString(R.styleable.MyStyleable_myText);
textView.setText(text);
I'm looking to get rid of my custom attributes and read "android:" attributes.
First of all I am not sure if setting android:text will be possible for view extending RelativeLayout. If it is possible do it like in TextView implementation:
final Resources.Theme theme = context.getTheme();
a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
text = a.getText(attr);
So basically it's the same way you get your custom properties but with different styleable.
If it's not working I would consider another approach. I did it for one of my custom views. Since you have a TextView in your custom view you could create it in XML and then get a reference to that child inside your custom view.
<YourCustomView>
<TextView android:text="someText"/>
<YourCustomView/>

Android Custom Controls with textviews inside them

I am making a Custom control and I want to add TextViews inside it. How Can I do that ?
Any ideas?
The custom controls will be used to display electronic programming guide(EPG). My application is for google tv and will be used for channels listing and playback and EPG, in EPG screen i will show the time intervals and programs in each intervals , I want to use Textviews for Program names and to give them custom fonts and style.
Any kind of help will be appreciated,
Many thanks,
Here is screen shot of my custo control ? to add the texts styles i want to use textviews. I hope its clear now?
Try to build further upon this example code:
public class CustomView extends LinearLayout {
public CustomView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomView(Context context) {
super(context);
}
public void addTextView(String text) {
TextView tv = new TextView(getContext());
tv.setText(text);
this.addView(tv);
this.invalidate(); //I think this is called implicitly, but just in case.
}
}
However, you have to do thing like margins, scrolling, layout and so on yourself, as your context does not give many clues.
EDIT: with the addition of some context, I suggest you use a TableLayout. Still, you can build that with the given example code.

How can I preview a custom android component layout?

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.).

Android -- SDK/IDE Layout Bug (w/Custom Layouts)?

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

Wrapping a LinearLayout from xml

I'm trying to make some Android view classes (which are just wrappers around layouts defined in an XML file). Is this correct:
public class MyViewWrapper extends LinearLayout {
private TextView mTextView;
public MyViewWrapper(Context context) {
super(context);
}
public constructUI() {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.myview, this);
mTextView = (TextView)findViewById(R.id.myview_textview);
}
}
so the idea is just that I can construct my views like that, and they have logic inside for modifying their child views etc. The layout looks like:
<LinearLayout>
<TextView />
</LinearLayout>
It just looks like I'm going to get an extra unnecessary LinearLayout. The wrapper class is itself a LinearLayout, and then it will attach the inner LinearLayout from the xml file.
Is that ok?
Thanks
You can try replacing the <LinearLayout> in your layout file with <merge>. I have not tried that recently, and I think I ran into problems when I last tried it, but in theory it should serve the purpose. <merge> basically means "take all my children and put them directly into whatever container I'm being inflated into".

Categories

Resources