I want to display a videoview in fullscreen mode when the user press a full screen button
I have searched on the internet but all solutions I have found doesnt' fix the issue
here is my current code which doesnt' work too :
public void setAnchorView(final View view) {
super.setAnchorView(view);
Button fullScreen = new Button(VideoDetails.this);
fullScreen.setText("FullScreen");
Log.e("media controller","Set anchorView");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(view.getWidth(), 0, 5, 20);
params.gravity = Gravity.RIGHT;
addView(fullScreen, params);
fullScreen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoview.getLayoutParams();
params.leftMargin = 0;
videoview.setLayoutParams(params);
}
});
}
and here is the xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#818181" >
<RelativeLayout
android:id="#+id/video_details_header"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#000000" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:clickable="true"
android:onClick="returnback"
android:text="Back"
android:textColor="#ffffff" />
<TextView
android:id="#+id/video_details_text_header_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/video_details_header"
android:layout_centerInParent="true" >
<RelativeLayout
android:id="#+id/layout_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/video_details_video"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<ImageView
android:id="#+id/video_details_thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:onClick="showvideo"
android:padding="11dp"
android:visibility="visible" />
<VideoView
android:id="#+id/video_details_videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="11dp"
android:visibility="gone" />
</FrameLayout>
which contains the videoview
do you have any idea
Try requestLayout() call after setting the layout params
...
params.leftMargin = 0;
videoview.setLayoutParams(params);
videoview.requestLayout();
try to change the width of the view too:
WindowManager wm = (WindowManager)videoview.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int displayWidth = display.getWidth();
params.leftMargin = 0;
params.width = displayWidth;
videoview.setLayoutParams(params);
videoview.requestLayout();
Not sure because can't all code, but for me it seems like you don't need setting margin at all
you may just manipulate with view's width and gravity.
You might have forgotten this line of code for it to fit the full screen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);
Related
I have a screen ButtonPress that shows a button on random positions, it is working for most of the iterations but in some, it is going out of the screen and the button is not visible. I tried to follow this example but that does not seem to help as the button is still invisible for some iterations in the ButtonPress activity. Here's how I am generating the random positions:
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_press);
button = findViewById(R.id.imageView5);
button.setClickable(true);
button.setOnClickListener(this);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
params.leftMargin = button.getWidth()
+ new Random().nextInt(metrics.widthPixels - 2*button.getWidth());
params.topMargin = button.getHeight()
+ new Random().nextInt(metrics.heightPixels - 3*button.getHeight());
button.setLayoutParams(params);
}
And here's the layout file for the button:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#03B0F5"
>
<TextView
android:id="#+id/button_press"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:gravity="center"
android:background="#FFFFFF"
android:text="#string/button_press"
android:textSize="50sp"
android:textColor="#03B0F5"
android:layout_marginStart="0dp"
/>
<ImageView
android:id="#+id/imageView5"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="#drawable/ic_button"
tools:ignore="VectorDrawableCompat" />
</LinearLayout>
I removed all the padding and margins from this resource file but it's still not working.
Try with the below code it will help you.
new Handler(this.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
int width = (getResources().getDisplayMetrics().widthPixels)/2;
int height = (getResources().getDisplayMetrics().heightPixels);
Random random = new Random();
int randomXPos = random.nextInt(width-(button.getWidth())/3);
int randomYPos = random.nextInt(height-(button.getHeight())/3);
button.animate().x(randomXPos).y(randomYPos).start();
button.setLayoutParams(params);
}
},100);
I am new in android development and I have searched this question, but not getting any solution.I want to ask how to make the cardview with recyclerview divide into three pieces in one screen?
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:id="#+id/card"
android:layout_height="250dp"
app:cardCornerRadius="4dp"
app:cardElevation="1dp"
app:cardMaxElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.siyamed.shapeimageview.RoundedImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and here is the code I try in Activity
View rootLayout = findViewById(R.id.root_layout);
double height =rootLayout.getLayoutParams().height/3.0;
CardView card= (CardView)findViewById(R.id.card);
CardView.LayoutParams layoutParams= (CardView.LayoutParams) card.getLayoutParams();
layoutParams.height=(int)height;
layoutParams.width=MATCH_PARENT;
Thanks.
try getting height of window and divide it by 3,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card= (CardView)findViewById(R.id.card);
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) card.getLayoutParams();
layoutParams.height= height;
layoutParams.width=MATCH_PARENT;
card.setLayoutParams(layoutParams);
Just as #V-rund suggested
Get the Screen/Window height and divide it by 3 using,
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels/3;
CardView card = (CardView)findViewById(R.id.card);
but instead of using CardView.LayoutParams use the LayoutParams for the parent layout of your CardView which in your case will be a LinearLayout.
LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams)
card.getLayoutParams();
layoutParams.height = height;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
card.setLayoutParams(layoutParams);
I hope this helps solve your problem.
I have an ImageView and a TextView within a RelativeLayout that is nested inside a ScrollView. I can't get the Top of the ImageView to line up with the top of the TextView. It hovers about 150dp down from the top of the text view top edge.
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transitionForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backgroundcolor"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/svTutorial"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:splitMotionEvents="true"
android:layout_above="#+id/ImageBottom"
android:layout_below="#+id/ImageTop"
android:scrollbars="vertical"
>
<RelativeLayout
android:id="#+id/layoutTransitionPicWidgets"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/lblQuestionText"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:layout_marginBottom="30px"
android:layout_marginLeft="50px"
android:layout_marginTop="10dp"
android:layout_marginRight="25px"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="get text from db."
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/slidetest"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I'm trying to programmatically change the width of the Image based on its size, so I am setting all the parameters programmatically.
Here is the code:
imgPic = (ImageView)findViewById(R.id.imgPic);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
final float density = getResources().getDisplayMetrics().density;
final float dpHeight = outMetrics.heightPixels / density;
final float dpWidth = outMetrics.widthPixels / density;
ViewTreeObserver vto = imgPic.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
imgPic.getViewTreeObserver().removeOnPreDrawListener(this);
final int finalHeight = imgPic.getMeasuredHeight();
final int finalWidth = imgPic.getMeasuredWidth();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = Math.round(.4f * dpWidth * density);
params.leftMargin = 50;
params.topMargin = 0;
params.rightMargin = 25;
//params.gravity= Gravity.TOP;
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.lblQuestionText);
imgPic.setLayoutParams(params);
return true;
}
});
I've tried doing this with a LinearLayout and get the same results. What am I missing?
You can add this to your ImageView
android:layout_alignTop="#+id/lblQuestionText"
but I think there's more going on than that. You don't specify any alignment attributes on the ImageView, so it may not layout where you think it's going to layout.
It seems like maybe you want the ImageView to be to the right of the TextView, in which case adding this to the ImageView should help:
android:layout_toRightOf="#+id/lblQuestionText"
I creating an activity with an Dialog theme
Manifest:
<activity
android:name="com.example.app.sendActivity"
android:theme="#android:style/Theme.Dialog"
android:excludeFromRecents="true">
</activity>
I want to the dialog themed Activity to have an icon.
Here is part of the code to create the dialog:
//Setup Dialog Activity Parameters
this.setFinishOnTouchOutside(false);
setContentView(R.layout.widget_dialog);
LayoutParams params = getWindow().getAttributes();
params.x = -30;
params.height = 350;
params.width = 550;
params.y = -30;
getWindow().setAttributes(params);
Log.i(TAG,"In onCreate");
setContentView(R.layout.widget_dialog);
Layout of Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">"
<TextView
android:id="#+id/tvStatus01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#FFF" />
<TextView
android:id="#+id/tvStatus02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#FFF"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/btnWidgetDialogCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginTop="5dp"
/>
<Button
android:id="#+id/btnWidgetDialogClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Close"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
Screen Shot
Here's what you need. In this you can set your height and width as you wish giving that dialog feel. Just add this in your onCreate():
Window window = getWindow();
DisplayMetrics metrics = getResources().getDisplayMetrics();
window.setGravity(Gravity.CENTER);
int width = (int) (metrics.widthPixels * 1);
int height = (int) (metrics.heightPixels * .85);
window.setLayout(height, width);
And in your manifest, in the application level, set the theme as;
android:theme="#android:style/Theme.Dialog"
This works great for me ...Should work for you too. :)
I created a gallery with horizontalscrollview. In the imageview LinearLayout I've put a color background. I want the LinearLayout spans the width of the screen with or without images. How I can do?
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/estandar"
android:padding="2dp"
/>
</HorizontalScrollView>
</LinearLayout>
main.xml
LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = linear2 .getLayoutParams();
params.height = 100;
params.width = 300;
but my code does not work.
EDIT:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int anchoPantalla = metrics.widthPixels;
if (listaImagenes.length > 0) {
for (String nombreImagen : listaImagenes) {
InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
final Bitmap bitmap = BitmapFactory.decodeStream(is);
final ImageView imageView = new ImageView(getApplicationContext());
alto = bitmap.getHeight();
ancho = bitmap.getWidth();
final float calculo = ancho / (alto / ALTO_IMAGEN);
imageView.setLayoutParams(new ViewGroup.LayoutParams((int)calculo, ALTO_IMAGEN));
imageView.setImageBitmap(bitmap);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundColor(colorResources);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
linear2.addView(imageView);
}
} else {
// PAINT LINE WIDTHSCREEN x 120dp
}
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = galeria2.getLayoutParams();
params.height = 100;
params.width = 300;
galeria2.setLayoutParams(params);
You have to dynamically add width to each element of HorizontalScrollView. I have modified your code have a look. Its working fine.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FF0040"
android:padding="2dp"
/>
</HorizontalScrollView>
Now in code add element like (see the highlited line)
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.ic_launcher);
**iv1.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
ImageView iv2 = new ImageView(this);
**iv2.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
galeria2.addView(iv1);
galeria2.addView(iv2);