Search code examples
androidslidingdrawer

How to add TextView in SlidingDrawer using code?


I am new to Android and this is my first question on Android.

I am trying to add TextView in Sliding Drawer dynamically but nothing get displayed after clicking handle button while running on emulator.

This what I have done so far :-

Java code:-

SlidingDrawer drawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1);

    for (int i = 0; i < 10; i++) {
        TextView view = new TextView(this);
        view.setLayoutParams(new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        view.setHeight(10);
        view.setWidth(40);
        view.setId(i);
        view.setText("Text " + String.valueOf(i));
        drawer.addView(view);   
    }

There is no error on logcat.

Please help.


Solution

  • Try this layout..add your text views to Linear layout inside Sliding drawer not directly inside sliding Drawer

      <SlidingDrawer
            android:id="@+id/SlidingDrawer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:content="@+id/contentLayout"
            android:handle="@+id/slideButton"
            android:orientation="vertical"
            android:rotation="180"
            android:padding="10dip"
             >
    
            <Button
                android:id="@+id/slideButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:rotation="180"
                android:text="Open" >
            </Button>
    
    
             <LinearLayout
                android:id="@+id/contentLayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:background="#99FF0000"
                android:cacheColorHint="#00000000"
                android:padding="10dip"
                >
    
    
    
               <ListView
                  android:id="@+id/list"
                  android:rotation="180"
                  android:layout_height="150dp"
                  android:layout_width="match_parent">
               </ListView>
    
                <ImageView
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:src="@drawable/ic_launcher"
                     android:rotation="180"
    
                     android:layout_gravity="center_horizontal|top"/>
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello"
                    android:gravity="center_horizontal"/>
    
    
               </LinearLayout>
        </SlidingDrawer>