Faced a very strange problem. I have a ViewPager that scans images. In the layout with `ViewPager I added a button that closes this activation and switches to another. On the emulator (Genymotion) everything works fine, but on a real device the button stops responding to pressing.

I thought that the problem is in the difference of versions, but this is the usual Button , it is the same everywhere. I also checked if ViewPager does not override these buttons, but when I set wrap_content , the situation has not changed.

 public class FullActivityGallery extends AppCompatActivity { ViewPager viewPager; SlideImageAdapter adapter; Button exit_btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.full_image); initButtons(); getMyIntent(); } private void getMyIntent(){ Intent intent = getIntent(); position = intent.getExtras().getInt("id"); viewPager = (ViewPager) findViewById(R.id.myViewPager); adapter = new SlideImageAdapter(this, position); viewPager.setAdapter(adapter); viewPager.setCurrentItem(position); } private void initButtons(){ exit_btn = (Button) findViewById(R.id.exit_btn); exit_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FullActivityGallery.this, ActivityGallery.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); 

Markup Code:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:text="exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/exit_btn" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="80dp" /> <android.support.v4.view.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myViewPager" > </android.support.v4.view.ViewPager> </RelativeLayout> 

UPDATE: on version 4.4, Android does not work on either the emulator or on a real device. On version 5 works everywhere. Who can tell what exactly is so different that the buttons stop working?

  • The first thing that comes to mind: the minimum SDK version (read - Android version) which one is installed? - Alexey Sarovsky
  • @AlekseySarovsky minimum is set - 15, if Google doesn't lie, then it is 4.0.3 - ivanovd422
  • @ AlekseySarovsky, try changing the pager button in places. - Yuriy SPb
  • @YuriySPb turned out that in version 5 the pager does not overlap the buttons, and in 4.4 it overlaps with the same layout. Very strange, but this is not written anywhere. I wonder how many more such mommont with markup? - ivanovd422
  • I support @YuriySPb. The pager view is described after the button, which means that if it visually overlaps the button, then there will not be a click to miss, what works on the 5th android is an accident. - xkor

1 answer 1

In different versions, this may work in different ways - when you put logically to -l markup elements lower in the hierarchy than another. On some versions it will be blocked and will not receive click events, on some it will work.

The correct solution here is to put the logical element of the hierarchy on top if it should lie on top of the other elements. Those. in your case, you need to swap the button and the ViewPager in the markup