Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing elements of dynamically generated view in android

I'm generating dynamic views in android via predefined XML. The Code is given below. I am able to assign actions to each element in the different layout using card_identifier sting. The problem is I am unable to access the elements in the dynamic layout e.g. if I want to set switch 1 in dynamic view 1 with sw1.setCheched(true), then how to approach that element?

The layout is like this: Layout View

Code:

public void threeSwitchLayout(String card_name, String card_identifier) {

    // Generate dynamic layout
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Use demo.xml to create layout
    final View addView = layoutInflater.inflate(R.layout.demo, null);

    final TextView cardTitle = (TextView)addView.findViewById(R.id.cardTitle);
    cardTitle.setText(card_name);

    final TextView cardIdentifier = (TextView)addView.findViewById(R.id.cardIdentifier);
    cardIdentifier.setText(card_identifier);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 0, 0, 15);
    addView.setLayoutParams(layoutParams);

    SwitchCompat sw1 = (SwitchCompat)addView.findViewById(R.id.switch1);
    SwitchCompat sw2 = (SwitchCompat)addView.findViewById(R.id.switch2);
    SwitchCompat sw3 = (SwitchCompat)addView.findViewById(R.id.switch3);

    sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
        }
    });

    sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
        }
    });

    sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
        }
    });

    container.addView(addView);
}

The demo.xml is as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/rounded_corners"
    android:layout_margin="5dp"
    android:layout_marginBottom="5dp"
    android:padding="5dp">

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/cardTitle"
            android:textStyle="normal|bold"
            android:textSize="25sp"
            android:padding="5dp"
            android:textAlignment="center"
            android:textColor="@android:color/background_dark"
            android:layout_weight="1" />

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cardIdentifier"
            android:textSize="12sp" />

    </RelativeLayout>

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:id="@+id/switch1"
            android:text="SW 1 "
            android:theme="@style/SCBSwitch" />

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:id="@+id/switch2"
            android:text="SW 2 "
            android:theme="@style/SCBSwitch"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <android.support.v7.widget.SwitchCompat
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:id="@+id/switch3"
            android:text="SW 3 "
            android:theme="@style/SCBSwitch"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

</LinearLayout>
like image 932
Plochie Avatar asked Sep 10 '25 15:09

Plochie


1 Answers

Please find below code snippets

MainActivity

public class Main2Activity extends AppCompatActivity {

    LinearLayout container;
    View addView;
    RelativeLayout switchLay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        container = (LinearLayout) findViewById(R.id.container);
    }

    public void addLayout(View v) {
        threeSwitchLayout("Try", "first");
    }

    public void ChangeState(View v) {
        try {
            SwitchCompat toChng = (SwitchCompat) switchLay.findViewById(R.id.switch1);
            toChng.setChecked(true);
        } catch (NullPointerException npe) {
            Toast.makeText(this, "please add view first", Toast.LENGTH_SHORT).show();
        }
    }

    public void threeSwitchLayout(String card_name, String card_identifier) {
        // Generate dynamic layout
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Use demo.xml to create layout
        addView = layoutInflater.inflate(R.layout.demo, null);
        final TextView cardTitle = (TextView) addView.findViewById(R.id.cardTitle);
        cardTitle.setText(card_name);
        final TextView cardIdentifier = (TextView) addView.findViewById(R.id.cardIdentifier);
        cardIdentifier.setText(card_identifier);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, 0, 15);
        addView.setLayoutParams(layoutParams);

        switchLay = (RelativeLayout) addView.findViewById(R.id.switchLay);
        SwitchCompat sw1 = (SwitchCompat) addView.findViewById(R.id.switch1);
        SwitchCompat sw2 = (SwitchCompat) addView.findViewById(R.id.switch2);
        SwitchCompat sw3 = (SwitchCompat) addView.findViewById(R.id.switch3);

        sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });

        sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });

        sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
            }
        });

        container.addView(addView);
    }
}

activity_main2.xml (layout file for MainActivity)

<?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=".MainActivity">

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:onClick="addLayout"
        android:text="add layout" />

    <Button
        android:id="@+id/btnChangeState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="ChangeState"
        android:text="Change switch state" />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btnAdd"
        android:orientation="vertical"></LinearLayout>


</RelativeLayout>

Explaination What I did is, I simply gave an ID to the parent Layout of the switch view and then referred them in the java class globally( RelativeLayout switchLay), similarly declared your "addview" also and then accessed the switch which we want to update via these global views.

Although this is just a snippet, you might need to update the code as per your exact requirement.

like image 51
Ankit Purwar Avatar answered Sep 12 '25 03:09

Ankit Purwar