0

I have a class CustomView which extends LinearLayout. I have another class CustomElement which also extends LinearLayout. When i try to use my class in XML nothing shows up.

This is my class CustomView:

private static int NUMBER_OF_ELEMENTS = 4;

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

private void init(Context context) {
        // get size for each element
    int width = getWidth() / NUMBER_OF_ELEMENTS;
    int height = getHeight() / NUMBER_OF_ELEMENTS;

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        CustomElement element = new CustomElement(context);
        addView(element, width, height);
    }
}

This is my class CustomElement:

public CustomElement(final Context context) {
    super(context);
    m_context = context;
    init(context);
}

private void init(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.custom_elem, this);
}

When i now try to add my CustomView in XML it doesn't show anything. Here is my XML Code:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/layout_mainleft"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <com.package.views.CustomView
      android:id="@+id/layout_elements"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true" />

 </RelativeLayout>

Am I missing something? Any help is appreciated!

X.X_Mass_Developer
  • 717
  • 1
  • 8
  • 23
  • First of all, `CustomElement` can't extend `View` in your current code. Second, where you add the `CustomElement` use the constructor that only takes a `Context`. Third, how does the entire layout where you use `CustomView` looks like? – user Dec 15 '12 at 17:39
  • reading the previous comment, apparently you have changed your code so your views don't extend View. But your CustomView and CustomElement must extend some View or ViewGroup, or they will not be displayed. You do call super, so you're assuming there is a super class. And, usually there's three different constructors. – Christine Dec 15 '12 at 23:54
  • @Luksprog First you are right! I was confused myself after changing it. It extends LinearLayout. If I change that, I cannot use the inflater.inflate anymore. What to use instead? Second, I changed that with the constructor. Third, I edited the XML to the entire layout. – X.X_Mass_Developer Dec 16 '12 at 13:22
  • @Christine Both classes extend LinearLayout, I didn't write it into my question, to reduce code. As far as i figured out, you only need to extend at least one Constructor and not all three. – X.X_Mass_Developer Dec 16 '12 at 13:25
  • What is the error when using the `inflater.inflate()`? That should work because now your `CustomElement` is a `ViewGroup`. What does the xml layout `R.layout.custom_elem` contains? Also if you change the orientation of `CustomView` to vertical, things change? – user Dec 16 '12 at 13:34
  • @Luksprog no there is no error, because I'm extending from `LinearLayout`. I just wanted you to know that you were right on me not extending `View`. The XML Layout contains two `TextView` elements and an `ImageView`. If I try to use only the `CustomElement` in XML it also works. It's the CustomView that makes troubles. I do however not get any error messages. – X.X_Mass_Developer Dec 16 '12 at 13:42
  • Did you tried adding the proper `LayoutParams`: `CustomElement element = new CustomElement(context); addView(element, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));`. Also I hope you have some text on those `TextViews` or an image to see something. Again set the orientation for `CustomView` to vertical: `super(context, attrs); setOrientation(LinearLayout.VERTICAL); init(context);`. – user Dec 16 '12 at 13:47
  • @Luksprog yes I tried it with LayoutParams as well. I was able to figure out a mistake. I was overwriting the method `onLayoutChanged()` and not calling the super method. This in a first step let my CustomView show `NUMBER_OF_ELEMENTS` times the CustomElement. However I need to use `addView(element, width, height)` with a custom width and height. But when using this method instead of `addView(element)` no more elements show up. Is this because I have to overwrite some method to resize my `CustomElement` ? – X.X_Mass_Developer Dec 16 '12 at 13:54
  • I edited what I need to do in the question above! – X.X_Mass_Developer Dec 16 '12 at 13:55
  • At that moment in time the `getWidth()` and `getHeight()` return `0`(so nothing on the screen) because the view isn't yet measured(which is done in the `onMeasure` method). Try to override the `onMeasure()` method, call the super class method and then using the `getWidth()` or `getMeasuredWidth()` method modify the `LayoutParams` of each of the children to the desired values. – user Dec 16 '12 at 14:02
  • @Luksprog thank you! that was the problem! But now another problem accured. My `CustomElement` is not resizing correctly. Do I have to create all my `TextViews` needed in Code? (The `ImageView` Is resizing after there was sufficent space for the `TextViews`) – X.X_Mass_Developer Dec 16 '12 at 14:12
  • Yeap, my bad, the chidlren need to be remeasured with the new set values. See this snippet of code https://gist.github.com/4307766 , also have a look at my answer here for something similar http://stackoverflow.com/questions/12512773/set-the-height-of-view-to-make-it-square-by-knowing-only-its-width-weight – user Dec 16 '12 at 14:24
  • @Luksprog I tried to resize my children. Also with your code snippet (thanks for that). The problem is the `TextViews` of the `CustomElement` do not resize even when I get them via `TextView textView = (TextView) getChildAt(0)`. (I made sure with debugging that I get the correct `TextView`) It simply does not change anything when I then call textView.setWidth() or textView.setHeight() Do you know a solution to that? The official documentation states that those two methods set the exact size of the TextView in pixels. – X.X_Mass_Developer Dec 16 '12 at 15:35
  • I don't see how that would happen. Keep in mind that with the code I used your CustomElement will have the same width/height dimensions because you set those dimensions in the onMeasure method which will be called every time there is a change in the layout. Also how do you change those TextViews(witch which values)? What are you trying to do with them by resizing? – user Dec 16 '12 at 16:19
  • @Luksprog as far as i could figure out, setWidth() is useless with a `TextView`, you have to change the textSize. Anyway, thank you very much, I got a pretty good idea now how i will get it to work, with your help! – X.X_Mass_Developer Dec 16 '12 at 18:08

2 Answers2

1

I was able to resolve the question with the help of Luksprog!

The problem was, that I was overwriting the onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) method without calling the super method! Therefore it didn't work!

Thanks again to Luksprog at this point.

X.X_Mass_Developer
  • 717
  • 1
  • 8
  • 23
0

As far as i know to create a custom layout you should extend ViewGroup and you have to override the method protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4). Here you implement your logic and place your customView. If you just extend the LinearLayout and don't change its behaviour in some way why do u extend it?

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
  • I changed the behaviour of the class, but my mistake was that I was Overwriting the `onLayout()` an not calling the super method! The code above had nothing wrong in it! But thanks anyway – X.X_Mass_Developer Dec 17 '12 at 19:18