4

i would like to create a custom View (subclass of the View class) and use a layout xml resource.

I want something like this:

public class CustomView extends LinearLayout {

    public CustomView(Context context) {
          super(context);
          LayoutInflater  mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          mInflater.inflate(R.layout.tweet, this, true);
}

This actually creates a View with the right height and width (a ScrollView with a list of those has exactly the expected length and scrollbars) but they are empty (black) even though the xml layout contains a lot of TextViews.

This is my xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tweet"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:background="#ffffff">
    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:orientation="horizontal"
            android:background="#ffffff">
            <TextView 
                android:text="User" 
                android:id="@+id/tvusername" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold">
            </TextView>
            <View
                android:layout_width="5dip"
                android:layout_height="1dip">           
            </View>
            <TextView 
                android:text="userscreenname" 
                android:id="@+id/tvuserscreenname" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>    
        </LinearLayout>
        <TextView
            android:text="0s"
            android:id="@+id/tvtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true">
        </TextView>
    </RelativeLayout>
    <TextView 
        android:text="Tweet Content" 
        android:id="@+id/tvcontent" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textColor="#000000">
    </TextView>
    <View 
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#ffffff">
    </View>
    <TextView 
        android:text="Retweet by" 
        android:id="@+id/tvrtby" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

This is my main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >

<ScrollView 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/scrollView1" 
    android:orientation="vertical"
   android:layout_alignParentTop="true">
    <LinearLayout 
        android:layout_width="match_parent" 
        android:id="@+id/timeline" 
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ffffff">           
            <TextView  
            android:id="@+id/tvoutput"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
            />    
    </LinearLayout>
</ScrollView>
</LinearLayout>

This is how I add the custom view to my main view:

Not working (what I try to achieve):

TweetView ctweet = new TweetView(getApplicationContext(),tweet);
timeline.addView(ctweet);

My current solution (works, but uses no custom view):

            View vtweet = View.inflate(getApplicationContext(), R.layout.tweet,null);

            TextView tvusername = (TextView) vtweet.findViewById(R.id.tvusername);
            TextView tvuserscreenname = (TextView) vtweet.findViewById(R.id.tvuserscreenname);
            TextView tvcontent = (TextView) vtweet.findViewById(R.id.tvcontent);
            TextView tvtime = (TextView) vtweet.findViewById(R.id.tvtime);

            tvusername.setText(tweet.getUser().getName());
            tvuserscreenname.setText('@' + tweet.getUser().getScreenName());
            tvcontent.setText(tweet.getText()); 
            //tvtime.setText(ctweet.tvtime.getText()); 
            timeline.addView(vtweet);
Toast
  • 596
  • 2
  • 19
  • 39
  • The posted xml it's the custom View's content? – user Jul 17 '12 at 14:54
  • Yes, its the content of the resource R.layout.customview – Toast Jul 17 '12 at 14:57
  • And I guess you inflate the layout as in the code you posted? Do you use the custom view in the xml layout or do you instantiate it? – user Jul 17 '12 at 14:58
  • I want to instantiate it by calling the constructor and then adding it as a child to a LinearLayout in my main activity. – Toast Jul 17 '12 at 15:00
  • I can't see what could possible go wrong so you should add more details, especially the layout where you add the custom view and the code you use to add the custom view. – user Jul 17 '12 at 15:11
  • I put it all in the question. Also I wonder why the first answer that someone posted an hour ago has disappeared (even though didnt solve the problem) – Toast Jul 17 '12 at 15:19
  • I've tested the layouts and they work just fine. If you inflate the layout like in the code you posted, then your custom view should work and show up. – user Jul 17 '12 at 16:27
  • Inflating it without a custom class also works for me. But once I use my own class, it becomes invisible. Also, I cant find any example of this on Google. Maybe this is the wrong way to display a list of things? – Toast Jul 17 '12 at 16:50
  • You didn't understood how I've tested the layout. See my test here ( https://gist.github.com/3131139 ), this works just fine, I can see the layout. Also, you could always replace that `ScrollView` with a `ListView` and add the *tvoutput* `TextView` as a header view for the list. – user Jul 17 '12 at 18:33
  • At first, thanks a lot for your effort! Unfortunately, I don't get this code to run. It says "Layoutflater cannot be resolved as a type" and I can't import it either. When I change it to LayoutInflater, it compiles and runs but I get the same result as always. – Toast Jul 17 '12 at 20:12
  • I can't help anymore without seeing your full code. If you can put your full code somewhere maybe I could take a look at it. – user Jul 18 '12 at 04:52
  • I just startet a new Android project with the code from your github post. But it doesnt compile because of the Layoutflater (cannot be resolved as a type). I did not change anything with your code. – Toast Jul 18 '12 at 12:41
  • That is referring to the `LayoutInflater` class, I don't see where exactly did you see **Layoutflater** in the code I posted on github(?!?). – user Jul 18 '12 at 13:42
  • Im so sorry! I just figured out I have a browser plugin that removes the sequence 'In' inside every word. I got your example working now and I will try to achieve the same with my main project. I will report whether it works. Thanks again for your help! – Toast Jul 18 '12 at 14:24

2 Answers2

3

you can use this in your xml just like any other view like <TextView

try <packageName.ClassName ie something like <com.example.CustomView

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

Probably you have to set the Layout Params of your TweetView object.

LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
ctweet.setLayoutParams(fieldparams);
simoneL
  • 602
  • 1
  • 7
  • 23