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
Solution : Here we need to store customer layouts in "View" objects in "oncreate()" method. Then set new layout using "setContentView()" method like
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..
You’re inflating a layout (
custome_layout.xml
) and setting a click listener on itsTextView
, 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
inactivity_layout
:A problem here is you’re using
setContentView
with layout resource IDs, likesetContentView(R.layout.activity_layout)
. That takes the XML file, inflates its layout (i.e. creates theView
objects described in the XML file and connects them up), and finally displays that final hierarchy ofView
objects.When you find
button
and set a click listener on it, you’re setting it on that specificView
object. If you callsetContentView(R.layout.activity_layout)
again (like in your ‘back’ function) then it inflates a new set ofView
objects. TheButton
with thebutton5
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
inonCreate
) and then callsetContentView(view1)
etc, i.e. passing a set ofViews
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 likeshowMainLayout()
that has thesetContentView
andsetOnClickListener
code, and call that when you want to display that layout.Or you could use
Fragment
s and just swap between those – this kind of thing is a big part of what they’re for.