Ananya Srivastav Ananya Srivastav - 1 month ago 6
Android Question

how to add imagebuttons on horizontal scroll view of tabs so as to scroll on left and right by those image buttons in android studio?

Please help
Can anyone help me out plz

public class FragmentForScrollBar extends Fragment {
private HorizontalScrollView hsv;
Button left, right;
int currentScrollX = 0;
Toolbar Toolbar;
RelativeLayout RelativeLayout;
ImageButtonhome,player,download,playlist,stream,wish,hit,instrument,news,extras;
TextView t;
FragmentTransaction fragmentTransaction;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragmentforscrollbar,null);

MyPlayer playerFragment = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, playerFragment, null);
fragmentTransaction.commit();

hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);

t = (TextView)view.findViewById(R.id.toolbartitle);
home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
player =(ImageButton)view.findViewById(R.id.imageButtonaa);
playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
download =(ImageButton)view.findViewById(R.id.imageButtonba);
stream =(ImageButton)view.findViewById(R.id.imageButtonda);
wish =(ImageButton)view.findViewById(R.id.imageButtonea);
hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
news =(ImageButton)view.findViewById(R.id.imageButtonha);
extras =(ImageButton)view.findViewById(R.id.imageButtonia);

home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Home");
Fragment Home = new Home();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Home);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

}
});
player.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Player");
Fragment Myplayer = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Myplayer);
fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();


}
});
playlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Fragment yourPlayList = new Your_Playlist();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, yourPlayList);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("MyDownload");
Fragment myDownload = new Downloads();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,myDownload);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
stream.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Streaming");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
wish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Your Wishes");
Fragment sendWishes = new wishes();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,sendWishes);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}
});
hit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Hit Parade");
Fragment hitParade = new hits();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,hitParade);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
instrument.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Uzbek Instruments");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
news.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("News");
Fragment news = new news();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,news);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
extras.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t.setText("Extras");
Fragment extras = new extras();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, extras);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;

}
}


sorry code was too long so i captured the image of the xml module file

Please see the above code and let me know the solution for scrolling the mini scroll layout on left and right by image buttons.
I am using two arrow buttons and tell me this also that what to do if i want to hide them in between the scrolling. Now can someone help me out please

Answer

For anyone who might be puzzled up with the same issue:

public class FragmentForScrollBar extends Fragment {


SeekBar verticalSeekBar=null;

private HorizontalScrollView hsv, hsv2;

Toolbar Toolbar;


ImageButton home,player,download,playlist,stream,wish,hit,instrument,news,extras, bP,bN;
ImageButton homea,playera,downloada,playlista,streama,hita,wisha,instrumenta,newsa, extrasa;
TextView t;
FragmentTransaction fragmentTransaction;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view =  inflater.inflate(R.layout.fragmentforscrollbar,null);

    MyPlayer playerFragment = new MyPlayer();
    fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment, playerFragment, null);
    fragmentTransaction.commit();

hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);

    t = (TextView)view.findViewById(R.id.toolbartitle);
    home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
    player =(ImageButton)view.findViewById(R.id.imageButtonaa);
    playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
    download =(ImageButton)view.findViewById(R.id.imageButtonba);
    stream =(ImageButton)view.findViewById(R.id.imageButtonda);
    wish =(ImageButton)view.findViewById(R.id.imageButtonea);
    hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
    instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
    news =(ImageButton)view.findViewById(R.id.imageButtonha);
    extras =(ImageButton)view.findViewById(R.id.imageButtonia);


    playera =(ImageButton)view.findViewById(R.id.imageButtonaab);
    playlista =(ImageButton)view.findViewById(R.id.imageButtoncab);
    downloada =(ImageButton)view.findViewById(R.id.imageButtonbab);
    streama =(ImageButton)view.findViewById(R.id.imageButtondab);
    wisha =(ImageButton)view.findViewById(R.id.imageButtoneab);
    hita =(ImageButton)view.findViewById(R.id.imageButtonfab);
    instrumenta=(ImageButton)view.findViewById(R.id.imageButtongab);
    newsa =(ImageButton)view.findViewById(R.id.imageButtonhab);
    extrasa =(ImageButton)view.findViewById(R.id.imageButtoniab);


    verticalSeekBar=(SeekBar)view.findViewById(R.id.seekBar);


    ImageButton bP = (ImageButton) view.findViewById(R.id.left);
    ImageButton bN = (ImageButton) view.findViewById(R.id.right);

    bP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //value 500 is arbitrarily given. if you want to achieve
            //element-by-element scroll you should get the width of the
            //previous element dynamically or if the elements of the
            //list have uniform width just put that value instead
            hsv.scrollTo(0,-500);
            //if it's the first/last element you can bPrevoius.setEnabled(false)

        }
    });


    bN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hsv.scrollTo(500,0);

        }
    });
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Home");
            Fragment Home = new Home();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, Home);
            fragmentTransaction.addToBackStack(null);
            // fragmentTransaction.popBackStack();
            fragmentTransaction.commit();
            //  t.setText("Player");
        }
    });
    player.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Player");
            Fragment Myplayer = new MyPlayer();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, Myplayer);
            fragmentTransaction.addToBackStack(null);
           // fragmentTransaction.popBackStack();
            fragmentTransaction.commit();


        }
    });
    playlist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //  DrawerNavigation mainActivity = new DrawerNavigation();
          //  mainActivity.drawerLayout.closeDrawers();
            Fragment yourPlayList = new Your_Playlist();
            fragmentTransaction = getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.containerView, yourPlayList);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

          // t.setText("PlayList");
        }
    });
    download.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("MyDownload");
            Fragment myDownload = new Downloads();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,myDownload);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    stream.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Streaming");
            Fragment uzBek1 = new UzBek();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,uzBek1);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    wish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Your Wishes");
            Fragment sendWishes = new wishes();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,sendWishes);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });
    hit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Hit Parade");
            Fragment hitParade = new hits();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,hitParade);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    instrument.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Uzbek Instruments");
            Fragment uzBek1 = new UzBek();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,uzBek1);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    news.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("News");
            Fragment news = new news();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment,news);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    extras.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.setText("Extras");
            Fragment extras = new extras();
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment, extras);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

    return view;

}

}

Just add image buttons on both ends and seOnclickListener() on them then scroll them up to a desired value as shown in the code.