Can't display custom View in Android Studio Layout Editor - android

I am struggling to get my Custom View to be displayed in Android Studio's Layout Editor.
My view is a simple subclass of RecyclerView with custom parameters. I use View.isInEditMode() to use the given attrs only when actually running the app:
public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
return;
}
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomRecyclerView, 0, 0);
/* ... */
}
However, previewing a layout containing this View results in an empty Preview pane (not even the phone's frame):
If I replace my View by a RecyclerView, the Preview works as expected:
I don't see what I should do here: if the RecyclerView is correctly displayed and all I do in my subclass's constructor is call super() and return if isInEditMode is true, then what could explain my View not being displayed?
What can I do to get my custom View displayed in Android Studio's Layout Editor?

As of today, opening the same project with Android Studio 2.1.2 does successfully display the UI in the Preview Pane. I can only guess this was a bug in the IDE and has been fixed since I asked this question.

For solving this problem use lower
API levels like 26
then rebuild the project.

Related

How to set an Android-X custom preference height?

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.

Is custom views context always an Activity?

I have a custom View that will be inflated from xml and has some custom xml attributes.
This view would setup some things in the ActionBar, if a xml attribute has been set to true.
Therefore I need a reference to the activities action bar.
My question is:
Can I assume that the context passed in the constructor of
class MyView extends View {
public MyView(Context context, AttributeSet attrs, int defStyle){
Activity a = (Activity) context;
}
}
I have tested that with different devices and different android versions, and it seems t that the context is an Activity.
Does anybody know that for sure?
No. If you theme the layout containing your view you will end up with a ContextThemeWrapper.

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.

Compound Controls forwarding State

Does anyone know what the appropriate mechanism is when creating a custom compound control to apply state changes from the container down to all the children? It would seem to me there should be a straightforward way to set up a ViewGroup to forward all state changes (pressed, enabled, etc.) to each child.
For example, if I create a custom widget along the lines of:
public class MyWidget extends RelativeLayout {
private TextView mTitleView, mValueView;
private ImageView mImageView;
public ValueButton(Context context) {
this(context, null);
}
public ValueButton(Context context, AttributeSet attrs) {
super(context, attrs);
//View setup, etc.
}
}
In many cases, there are drawable or color state lists attached to the children that I want to toggle when changes apply to the overall widget as a whole. What do I need to add to a widget such as this so that when, for example, I call MyWidget.setEnabled() or when MyWidget is pressed those state changes filter down the hierarchy?
Thanks!
Add android:duplicateParentState="true" to each child (perhaps iterating through the childs using getChildAt(int index))
http://developer.android.com/reference/android/view/View.html#attr_android:duplicateParentState
Edit: you will need to set it up in the xml
Note: in the current implementation, setting this property to true after the view was added to a ViewGroup might have no effect at all. This property should always be used from XML or set to true before adding this view to a ViewGroup.
Not sure if I'm going to be much help here, but it's an interesting problem for me to think about because I think I'm going to want to do something quite similar.
In terms of cascading state information down to child Views, could one possible approach be to only contain that state information in the parent ViewGroup, and in the child Views make use of getParent() to access that information?

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

Categories

Resources