Search code examples
androidonclicklistenerlayout-inflater

onClickListener() is not working after I inflate the current view(activity_main)


what i'm trying to do is to run a while loop and print the text inside a textview, however it's working only for the first time then in the second time onClickListener() is not working.

This is my MainActivity code.

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.Button;
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 {

    Button inflate_Btn;
    EditText Get_number;
    ScrollView scrollView;

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

       inflate_Btn=findViewById(R.id.inflate_Btn);
       Get_number=findViewById(R.id.Get_number);
       scrollView=findViewById(R.id.scrollView);
       inflate_Btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer number=Integer.parseInt(Get_number.getText().toString());
                if (number<=0 ||number==null){
                    Toast.makeText(getApplicationContext(),"Insert Number First!!",Toast.LENGTH_SHORT);
                    Get_number.requestFocus();
                }else {              
                    LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View v=inflater.inflate(R.layout.activity_main,null);
                    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView);
                    LinearLayout ll = new LinearLayout(getApplicationContext());
                    ll.setOrientation(LinearLayout.VERTICAL);
                    Integer i=1;
                    while(i<=number)
                    {
                        TextView tv = new TextView(getApplicationContext());
                        tv.setText("my text"+i);
                        ll.addView(tv);
                        i++;
                    }
                    sv.addView(ll);
                    setContentView(v);
                }   
            }
        });
    }
}

My activity_main.xml layout is here. I have created a simple layout. Please help

<?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="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/Get_number"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Enter Number"
        android:inputType="number"/>

    <Button
        android:id="@+id/inflate_Btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Inflate"></Button>

       <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

Solution

  • Try this. It works like a charm

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            inflate_Btn=findViewById(R.id.inflate_Btn);
            Get_number=findViewById(R.id.Get_number);
            scrollView=findViewById(R.id.scrollView);
            inflate_Btn.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    Integer number=Integer.parseInt(Get_number.getText().toString());
                    if (number<=0 ||number==null){
                        Toast.makeText(getApplicationContext(),"Insert Number First!!",Toast.LENGTH_SHORT);
                        Get_number.requestFocus();
                    }else {
                        scrollView.removeAllViews();
                        LinearLayout ll = new LinearLayout(getApplicationContext());
                        ll.setOrientation(LinearLayout.VERTICAL);
                        Integer i=1;
                        while(i<=number)
                        {
                            TextView tv = new TextView(getApplicationContext());
                            tv.setText("my text"+i);
                            ll.addView(tv);
                            i++;
                        }
                        scrollView.addView(ll);
                    }
                }
            });