Question

I want to put a videoview and play button over this videoview programmaticaly

to do that I have created a framelayout in which I put both of them

here is the code :

FrameLayout frm_video = new FrameLayout(this);                                      

                FrameLayout.LayoutParams params_frm_video = new FrameLayout.LayoutParams(
                        150,90);
                params_frm_video.gravity = Gravity.CENTER;
                frm_video.setLayoutParams(params_frm_video);                                        


                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(f.getFile().getPath());

                Bitmap b = retriever.getFrameAtTime();

                BitmapDrawable b1 = new BitmapDrawable(
                        getResources(), b);                                                             

                RelativeLayout.LayoutParams layoutParams = 
                        new RelativeLayout.LayoutParams(50,50);
                    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        150,90);
                params.gravity = Gravity.CENTER;

                VideoView overview_video = new VideoView(this);                 
                overview_video.setBackground(b1);                                   

                frm_video.addView(overview_video);

                ImageButton play_btn = new ImageButton(this);
                play_btn.setBackgroundResource(R.drawable.btn_play);

                play_btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        seeItem(liste.get(v.getId()-1300));

                    }
                });     

                LinearLayout.LayoutParams params_play_btn = new LinearLayout.LayoutParams(
                        50,50);
                params_play_btn.gravity = Gravity.CENTER;
                play_btn.setLayoutParams(params_play_btn);
                frm_video.addView(play_btn);
                play_btn.setId(1300+i);

                item_layout.addView(frm_video,params_frm_video);

and I want the play button to be in the center of the framelayout and videoview but I canno't do that

and I managed to do that with xml but programmaticaly the play button is in the left top corner of the framelayout

here is a capture of this code :

enter image description here

how can I fix this issue

Was it helpful?

Solution

I just find the solution, I have to use :

FrameLayout.LayoutParams params_play_btn = new FrameLayout.LayoutParams(
                        50,50);

as layoutparams because I am using FrameLayout instead of

LinearLayout.LayoutParams

I hope it helps

OTHER TIPS

It's not programmaticaly but you can set

 android:layout_centerHorizontal="true" 
 android:layout_centerVertical="true"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top