Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 25 May 2016

Get child view at run-time, getChildAt()


In this exercise show how to create child view at run time, using getChildCount() and getChildAt().

actvity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.samset.threadexcersize.MainActivity">
    <ScrollView
        android:id="@+id/scroll_view_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linear_layout_root_button"
        android:padding="20dp">

        <LinearLayout
            android:id="@+id/linear_layout_row_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/buttonLayout"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linear_layout_root_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:weightSum="10"
        android:padding="5dp">

        <Button
            android:onClick="onClick"
            android:layout_margin="8dp"
            android:id="@+id/button_add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="Add New" />
<Button
    android:onClick="onClick"
    android:layout_margin="8dp"
    android:id="@+id/button_show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    android:text="Show Data" />
    </LinearLayout>

</RelativeLayout>

row.xml

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="New Item"
        android:textColor="@android:color/holo_blue_light"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5" />

</LinearLayout>


ActvityMain.java

package com.samset.threadexcersize;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private LinearLayout linearLayout;
    private ScrollView mainScrollView;

    private String strKey[] = {"Name", "Email", "Contact", "Address", "Pin", "State"};
    private String strvalue[] = {"Sam", "sam@gmail.com", "9811054745", "Noida", "110097", "UP"};

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

        linearLayout = (LinearLayout) findViewById(R.id.linear_layout_row_container);
        mainScrollView = (ScrollView) findViewById(R.id.scroll_view_main);
        views();

    }

    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button_add:
                addView();
                break;

            case R.id.button_show:
                show();
        }

    }

    private void show() {
        String strdata = "";

        int childCount = linearLayout.getChildCount();
        strdata += "childCount: " + childCount + "\n\n";

        for (int i = 0; i < childCount; i++) {
            View childView = linearLayout.getChildAt(i);
            EditText childTextView = (EditText) (childView.findViewById(R.id.et));
            String childTextViewText = (String) (childTextView.getText().toString());

            strdata += i + ": " + childTextViewText + "\n";
        }
        Toast.makeText(MainActivity.this, strdata, Toast.LENGTH_LONG).show();
    }

    private void views() {
        for (int i = 0; i < strKey.length; i++) {
            addView();

            LinearLayout currentLayout = (LinearLayout) linearLayout.getChildAt(i);
            ((TextView) currentLayout.getChildAt(0)).setText(strKey[i]);
            if (null != strvalue[i])

                ((EditText) currentLayout.getChildAt(1)).setText(strvalue[i]);

        }
    }

    private void addView() {
        linearLayout.addView(getLayoutInflater().inflate(R.layout.row, null));
        findViewById(R.id.scroll_view_main).post(new Runnable() {
            @Override
            public void run() {
                mainScrollView.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

}

Thank you

Live Sample









No comments:

Post a Comment