Reputation: 42
I am developing an app which consists of a view-pager which has just 2 pages and LinearLayouts in each of those pages. I want to set click listeners for those linearlayouts.
I implemented the view pager by using PagerAdapter. I tried
view.findViewById(R.id.add_new_tut_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();
}
});
But it doesn't work at all
HomePagerAdapter.java
public class HomePagerAdapter extends PagerAdapter {
Context context;
HomePagerAdapter(Context context){
this.context = context;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
final ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), container, false);
View view = inflater.inflate( modelObject.getLayoutResId(), container, false);
if ( position == 0) {
view.findViewById(R.id.add_new_tut_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();
}
});
}
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public enum ModelObject{
HOME_PAGE("Home", R.layout.home_btns_layout),
FILES_PAGE("Files", R.layout.files_download_layout);
private int mLayoutResId;
private String mTitleResId;
ModelObject(String titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public String getTitleResId() {
return mTitleResId;
}
public int getLayoutResId()
{
return mLayoutResId;
}
}
}
home_btns_layout.xml
<?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"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/files_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:id="@+id/add_new_tut_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:id="@+id/upcoming_tut_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upcoming"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
files_download_layout.xml
<?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"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Networks"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Theories"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Questions"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 92
Reputation: 42
It was a foolish mistake. I inflated the view twice, set the listener on one and appended another to the ViewGroup container. Thank you for pointing it out @IcedLance
Upvotes: 0
Reputation: 4658
You need to explicitly tell android that you require your linearlayout to be clickable! Because it is a layout and not a widget.
Setting the LinearLayout clickable attribute to true.
In XML
android:clickable="true"
Or in Code
yourLinearLayoutButton.setClickable(true);
Upvotes: 0
Reputation: 79
Can i suggest to use Fragments for each screen of your ViewPager ? then inside your Fragment you can do whatever you want, with all ClickListeners tat you need.
I have created a gist for yougist ViewPagerAdapter
Upvotes: 1