Peddiraju G
Peddiraju G

Reputation: 168

how to give positions to textview in linearlayout in android programmatically

I created a text view using this code, I want place this text view at the top of the lineaelayout(means like header)

TextView valueTV = new TextView(Schedule.this);
                valueTV.setText("Event in Progress");
                valueTV.setTextColor(Color.parseColor("#FFFFFF"));
                valueTV.setTypeface(TypeFaceConstant.getOpensasItalic(getApplicationContext()));
                valueTV.setId(0);
                valueTV.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                valueTV.setGravity(Gravity.TOP);
                valueTV.setPadding(20, 0, 0, 0);
                lnr_list_event.addView(valueTV);

My xml file is ... I want to place above text view at first positiov(top), with using above code it placed at last... pls help me

 <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lnr_list_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" android:paddingTop="5dp" android:paddingBottom="5dp" >

<LinearLayout
    android:id="@+id/lnr_list_progress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp" >

    <ImageView
        android:id="@+id/calender"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#00000000"
        android:src="@drawable/addevent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="4"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/event_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:text="dfdf"
            android:textColor="#000000"
            android:textSize="18sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/event_duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:singleLine="false"
                android:textColor="@color/event_titles"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/event_location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/event_titles"
                android:textSize="16sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/event_locationss"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/event_titles"
            android:textSize="16sp"
            android:visibility="gone" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="5dp"
        android:src="@drawable/right_arrow" />
</LinearLayout>

Upvotes: 0

Views: 869

Answers (3)

Exigente05
Exigente05

Reputation: 2211

Try This,

LinearLayout header = (LinearLayout)findViewById(R.id.header);

TextView valueTV = new TextView(Schedule.this);
            valueTV.setText("Event in Progress");
            valueTV.setTextColor(Color.parseColor("#FFFFFF"));
            valueTV.setTypeface(TypeFaceConstant.getOpensasItalic(getApplicationContext()));
            valueTV.setId(0);
            valueTV.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            valueTV.setGravity(Gravity.TOP);
            valueTV.setPadding(20, 0, 0, 0);
            header.addView(valueTV);

And xml file,

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lnr_list_event"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" android:paddingTop="5dp"
    android:paddingBottom="5dp" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
</LinearLayout>

<LinearLayout
    android:id="@+id/lnr_list_progress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp" >

<ImageView
    android:id="@+id/calender"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:background="#00000000"
    android:src="@drawable/addevent" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_weight="4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/event_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:text="dfdf"
        android:textColor="#000000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/event_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="10dp"
            android:singleLine="false"
            android:textColor="@color/event_titles"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/event_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/event_titles"
            android:textSize="16sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/event_locationss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/event_titles"
        android:textSize="16sp"
        android:visibility="gone" />
</LinearLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:src="@drawable/right_arrow" />

Upvotes: 0

theapache64
theapache64

Reputation: 11754

It'll be better if you declare the TextView in the xml it self and hide and show it from the code. So there's no need of initializing the same object in each drawing.

Hiding and showing can be done with the below code

textView.setVisibility(View.VISIBLE); //TO Show
textView.setVisibility(View.INVISIBLE); //TO Hide
textView.setVisibility(View.GONE); //To Hide and Release the space to otherview.

Upvotes: 2

Thomas R.
Thomas R.

Reputation: 8073

In your activity get the root linearlayout by id, i.e. lnr_list_event. Then use method addView on the root view with index.

LinearLayout root = (LinearLayout) findViewById(R.id.lnr_list_event);

root.addView(yourHeaderView, 0, layoutparams);

Upvotes: 1

Related Questions