skip to Main Content

OnClickListener does not work on custom imported views using "setContentView"

The android app has activity and its activit_layout. Then I placed three button on activity_layout and set "onclicklistener" for each buttons in order to open different custom views. Each button set specific layout file for the activity using "setContentView" methode inside "onclicklistener" methode and they work well. Then, those each custom layouts have sperate textview on them. Once agian, I want set "setOnClickListener" for these each textviews that are on custom layout files, seperatelty and perform different tasks. Thing is when I click on the textview, nothing happens at all. I have no idea about weather text is recognized or "onclicklistener" methode doesn’t work.
Here I’m attaching my code file below.

Activity java file :-



public class beverbs extends AppCompatActivity
{
    private int layout = 0; // interger for custom layout file
    TextView text1;  // textview is on the custom layoutfile
    View view1;   // custom Layout file

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);


        view1 = LayoutInflater.from(getBaseContext()).inflate(R.layout.custome_layout,null);

        text1 = view1.findViewById(R.id.textView12);

        text1.setOnClickListener(new OnClickListener()   // this part does not work
        {
            @Override
            public void onClick(View view)
            {
                // do something
            }
        });

    }

    public void open_new_layout(View view)  // onclick methode  
    {
        layout = 1;
        setContentView(R.layout.custome_layout);  // button on main layout set  custom layout the activity 
    }

    @Override
    public void onBackPressed()
    {
        if (layout==1)
        {
            layout = 0;
            setContentView(R.layout.activity_layout);  // set original layout again
        }
        else
        {
            super.onBackPressed();
        }
    }
}

Activity XML file :-

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="open_new_layout"
        android:text="Open Layout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Custom layout XML file:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Perform Task"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Pls anyone help me with this weather there’s another way to do my task.

I dug the internet for any solution but got nothing regarding with this.

2

Answers


  1. Chosen as BEST ANSWER

    Solution : Here we need to store customer layouts in "View" objects in "oncreate()" method. Then set new layout using "setContentView()" method like

    setContentView(view);
    

    Then initiate a child object on the view like textview = view.findViewById(R.id.id_of_element);

    then set the "setOnClickListener" on that element .. It works..


  2. You’re inflating a layout (custome_layout.xml) and setting a click listener on its TextView, but you’re not actually displaying that inflated layout – so the user can’t click on it!

    I’m guessing you actually want to set the click listener on the Button in activity_layout:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // inflate and display the main activity layout
        setContentView(R.layout.activity_layout);
    
        // find the button on the displayed layout
        Button button = findViewById(R.id.button5);
        
        // set a click listener on it
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // call openNewLayout or whatever
            }
        };
    }
    

    A problem here is you’re using setContentView with layout resource IDs, like setContentView(R.layout.activity_layout). That takes the XML file, inflates its layout (i.e. creates the View objects described in the XML file and connects them up), and finally displays that final hierarchy of View objects.

    When you find button and set a click listener on it, you’re setting it on that specific View object. If you call setContentView(R.layout.activity_layout) again (like in your ‘back’ function) then it inflates a new set of View objects. The Button with the button5 ID won’t have a click listener set on it until you set it up again, because it’s a completely new object.


    You have two options here. You can manually inflate each view hierarchy and store them (like you’re doing with R.layout.custome_layout in onCreate) and then call setContentView(view1) etc, i.e. passing a set of Views for display. That way, since you’re using the same objects, any click listeners will still be set.

    The other option is to do what you’re doing, and call setContentView(R.layout.activity_layout), but run your setup code (adding click listeners etc) every time you call it – you’re inflating new objects, so you need to set them up every time. The easiest way to do that would be creating a method like showMainLayout() that has the setContentView and setOnClickListener code, and call that when you want to display that layout.

    Or you could use Fragments and just swap between those – this kind of thing is a big part of what they’re for.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search