Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 11 May 2016

How to add view dynamically


How to add view dynamically

ActivityMain.java


public class MainActivity extends AppCompatActivity {
    TextView titleTxtView, txtleftcancel;
    TextView txtplus;
    LinearLayout addlayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_media_activity);

   
        addlayout = (LinearLayout) findViewById(R.id.addlayout);
        txtplus = (TextView) findViewById(R.id.plus);
 
        txtplus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater =(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.plusmodel, null);
                TextView textOut = (TextView)addView.findViewById(R.id.minus1);
                textOut.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        ((LinearLayout)addView.getParent()).removeView(addView);
                    }});

                addlayout.addView(addView);
            }});


    }
}

actvity_main.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#e7e7e7"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e7e7e7"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/youtubelayoutt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e7e7e7"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#fff"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="#fff"
                        android:padding="10dp"
                        android:paddingLeft="10dp"
                        android:text="Youtube" />

                    <TextView
                        android:id="@+id/plus"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_marginRight="10dp"
                        android:background="@drawable/plus"
                        android:gravity="right|center"

                        />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:background="#fff"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#fff"
                    android:hint="URL link"
                    android:padding="10dp" />

                <TextView
                    android:id="@+id/minus"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/minus"
                    android:gravity="right|center" />
            </LinearLayout>
        <LinearLayout
            android:id="@+id/addlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="#fff"
            android:orientation="vertical">

            </LinearLayout>
</LinearLayout>
 </RelativeLayout>

 </ScrollView>


addnew_view.xml


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

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#e7e7e7"></View>

    <LinearLayout
        android:id="@+id/youtubelayoutt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="horizontal">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#fff"
            android:hint="URL link"
            android:padding="10dp"
            android:paddingLeft="10dp" />

        <TextView
            android:id="@+id/minus1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/minus"
            android:gravity="right|center"

            />
    </LinearLayout>
</LinearLayout>


Thank you

Live Sample






No comments:

Post a Comment