I have an ImageView that displays an image in fullscreen in the background. This is done in this way.
public void handleImage(String imageLocation) {
LinearLayout layout = (LinearLayout) findViewById(R.id.mediaView);
ImageView image = new ImageView(this);
layout.addView(image);
Uri imageLocUri = Uri.parse(imageLocation);
image.setImageURI(imageLocUri);
image.setScaleType(ImageView.ScaleType.FIT_XY);
}
Now I need to do the same for video, but VideoView does not have .setScaleType().
The video needs to be playable on click, and I will be displaying buttons and stuff on top of the video.
Any ideas?
Related
The first picture is SOFT_INPUT_ADJUST_RESIZE.
The screen does not go up, but the edittext is not visible.
The second picture is for SOFT_INPUT_ADJUST_PAN.
The editText is visible, but the screen is cut off and raised.
How do I make the screen not cut and the edittext visible?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_chat);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
layoutParams.dimAmount = 0.5f;
getWindow().setAttributes(layoutParams);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = (int) (display.getWidth() * 0.85);
int height = (int) (display.getHeight() * 0.85);
getWindow().getAttributes().width = width;
getWindow().getAttributes().height = height;
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//or resize
you can add this line to your AndroidManifest.xml
android:windowSoftInputMode="adjustPan|adjustResize"
OR
also you can add it programmatically in java file like below
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
OR
You can also manage these things by using design layout in vertical scroll view so once user click on the edit text then the content of the page will be scroll vertically and you can see whole page.
I have a CardView (with multiple items like imageView, textView, Buttons) in a RecyclerView. I want to get bitmap from this cardview. I tried a simple method of converting cardView into bitmap (convert a cardview to bitmap) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver.
CardView extend view, so you probably can convert it as any other view,
try to pass the CardView into that function:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view
I have had no trouble adding a web view to my layout on click with a button, but when it comes to adding a web view on create of my main activity it just does nothing. I need the program to create certain web views based on data stored when I run it. It just really gets me that the same exact code runs perfectly when I put it inside an onClick button, but in the main method it does absolutely nothing. No error, no anything.
tickerLinearLayout = (LinearLayout) findViewById(R.id.TickerLinearLayout);
currency = new WebView(this);
currency.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, btcUp.getMeasuredHeight()));
tickerLinearLayout.addView(currency);
currency.getSettings().setJavaScriptEnabled(true);
disableScroll(currency);
currency.loadUrl("file:///android_asset/btc.html");
To create a Webview programatically in an Activity, you will first have to add your Webview into a RelativeLayout. I am choosing a RelativeLayout because my use case needs the Webview to be positioned on different locations on the Parent View time to time.
public class CustomRelativeLayout extends RelativeLayout {
private WebView mWebView;
public CustomRelativeLayout(Context context) {
super(context);
mWebView = new WebView(context);
mWebView.setId(View.NO_ID);
mWebView.setScrollContainer(false);
mWebView.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
context.getResources().getDisplayMetrics().widthPixels, context.getResources().getDisplayMetrics().heightPixels);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mWebView.setLayoutParams(params);
addView(mWebView);
}
public WebView getTheWebView() {
return mWebView;
}
}
You can modify the CustomRelativeLayout class to accept URL.
In your MainActivity class, you will have to add a new instance of CustomRelativeLayout and add it to the contentView as shown below:
setContentView(new CustomRelativeLayout(this));
To make adjustments to the size of the webview play around with the width and height of the LayoutParams added to the Webview.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
<width>, <height>);
So I have two objects on my activity, a TextView, and a Button. I did change the font of both of these using a font style from my assets folder. The project would load fine no problems. However now all of a sudden changing the TextView font makes the game crash and not load. I can't understand what could have caused this and how to solve it other than maybe using a button to display text, which is in practical.
My java code:
public class Main_Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
Button n=(Button) findViewById(R.id.start_button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
n.setText("Start");
n.setTypeface(typeface);
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
TextView title = (TextView) findViewById(R.id.title);
title.setTypeface(typeface2);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.sound1);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
}
I did use the typeface to change both texts fonts but I tried just making it again with typeface2 and it still crashes. Not sure if I need to show any other part of my project but will if you wish to see it. Thank you for the help.
It seems like you forgot "setText" to the title
I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed.
I know I can swap out the ImageView image when the state changes through drawables, but I cannot seem to find the ideal way to affect another view in the layout while isPressed is true on this specific image view.
Ultimately, I am trying to create a bottom ActionBar and simulate the regular ActionBar highlight box (that is, when you press a menu item in the ActionBar you get the highlight box). Right now I have the ImageView a LinearLayout with a small (8dp) padding on the top and bottom. I can replace the image in the ImageView with one that has a 50% white background, but I cannot do it this way if I want to keep the images device density independent. Instead, I'd like to have a square layout the button exists in that I would change the color of as needed.
Ideas?
"I have an ImageView inside a LinearLayout view. I want to change the color of the LinearLayout view while the ImageView is pressed."
for this :
you define your ImageView in XML as clickable android:clickable="true"
you affect an OnClickListener to this ImageView in your Activity onCreat() :
ImageView yourImage = (ImageView) findViewById(R.id.your_image);
yourImage.setClickable(true); // if you want to define it here
yourImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.your_linear_layout).setBackgroundColor(your_color);
}
});
Else if you want to change the color only when it is clicked and restore the old color after the click, you can implement OnTouchListener :
yourImage.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(final View v, MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
// when the click begins
findViewById(R.id.your_linear_layout).setBackgroundColor(your_click_color);
return true;
} else {
// when the click finishs
findViewById(R.id.your_linear_layout).setBackgroundColor(your_init_color);
return true;
}
return false;
}
});
I hope I helped...