Search code examples
androidandroid-viewpagerimageviewandroid-tablayoutfloating-action-button

Android Floating Button different jobs in every Tab


i need to use floating button as camera and image view it on every tab : tab 1 Image view 1 tab 2 image view 2 tab 3 image view 3 and after i press it and captured the first pic breaks then shows the picture then on the tab 2 once again i press and capture a pic and display it on image view 2 my biggest problem i don't know how to switch(position) of tabs or something like this to do different On click listener in every tab

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

    imageView1 = (ImageView) findViewById(R.id.img1);
    imageView2 = (ImageView) findViewById(R.id.img2);
    imageView3 = (ImageView) findViewById(R.id.img3);

    tabLayout = (TabLayout) findViewById(R.id.tablayout1);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));

    tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.viewpager1);
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setAdapter(adapter);
    tabLayout.setOnTabSelectedListener(this);
    viewPager.getCurrentItem();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tabLayout.getSelectedTabPosition();
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            String pictureName= getPictureName();
            File imageFile = new File(file,pictureName);
            Uri pictureUri = Uri.fromFile(imageFile);
            viewPager.getCurrentItem();
            i.putExtra(MediaStore.EXTRA_OUTPUT,pictureUri);
                startActivityForResult(i, 0);
        }
    });

}
public String getPictureName() {
    SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
    String timestamp = sdf.format(new Date());
    return "image" + ".jpg";
    // yyyyMMdd_HHmmss
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        imageView1 = (ImageView) findViewById(R.id.img1);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.buildDrawingCache();
                Bitmap image = imageView1.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);
            }
        });

        if (requestCode == 0 && resultCode == RESULT_OK) {

            String photoPath = Environment.getExternalStorageDirectory() + "/Download/image.jpg";
            Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
            imageView1.setImageBitmap(b);
        }
    }

            @Override
            public void onTabSelected (TabLayout.Tab tab){
            }

            @Override
            public void onTabUnselected (TabLayout.Tab tab){
            }

            @Override
            public void onTabReselected (TabLayout.Tab tab){
            }
        }    

Solution

  • You could use the getSelectedTabPosition() method of TabLayout to perform your actions depending on the currently selected tab.

    For example:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = tabLayout.getSelectedTabPosition();
    
            switch(position) {
                case 0:
                    // first tab is selected
                    break;
                case 1:
                    // second tab is selected
                    break;
                case 2:
                    // third tab is selected
                    break;
            }
        }
    });