I have an ImageView, which will fill the entire width of the screen and will also be animated up and down like a wave at the bottom of the screen. I have used scaleType="matrix" since it should not stretch to fit within the screen.
However, once it has been layouted, Android crops everything away that was outside at the time of layout-update, so once it starts to animate the bottom part is missing.
So my question is, how can I prevent Android from cropping my ImageView?
I wrote a class that seems to solve the problem, thought I´d share it if someone else faces this problem..
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
public class NoCropImageView extends ImageView
{
private int fixedWidth = 0;
private int fixedHeight = 0;
private int[] attrsArray = new int[] { android.R.attr.layout_width, android.R.attr.layout_height };
public NoCropImageView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
int layout_width = ta.getDimensionPixelSize(0, ViewGroup.LayoutParams.MATCH_PARENT);
int layout_height = ta.getDimensionPixelSize(1, ViewGroup.LayoutParams.MATCH_PARENT);
ta.recycle();
fixedWidth = layout_width;
fixedHeight = layout_height;
}
#Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{
this.setMeasuredDimension(fixedWidth, fixedHeight);
}
}
Related
I've been trying to get an app idea of mine to come to fruition for some time now. The idea is a weather app in which users can choose the content they want to see, and it is represented by cards (CardViews). For example, if they want to see only current weather and a 3 day forecast, they can enable that, and they'll see only those two cards in the MainActivity in a Google Now sort of fashion.
I've run into a few problems, and I need some help.
Firstly, how can I create the CardViews dynamically? My first attempt had the views completely defined in XML, but that didn't work very well because it doesn't seem very intuitive to hide/show them all the time. So I decided to extend the CardView class into things like a CurrentWeatherCardView and create the card completely programatically.
Here's the class I created from that. It's a child of the WeatherCardView class, which just extends CardView and doesn't do a whole lot else at this point.
package com.photonfighterlabs.particleweather.weathercardviews;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.photonfighterlabs.particleweather.MainActivity;
import com.photonfighterlabs.particleweather.weatherobjects.CurrentWeather;
import com.photonfighterlabs.particleweather.weatherobjects.Weather;
import java.io.File;
import static android.widget.LinearLayout.HORIZONTAL;
import static android.widget.LinearLayout.VERTICAL;
public class CurrentWeatherCardView extends WeatherCardView {
private ViewGroup viewGroup;
private LinearLayout linearLayout_600, linearLayout_604, linearLayout_434;
private TextView cw_title, temp_text_view;
private ImageView icon_image_view;
private int is_day;
private CurrentWeather currentWeather;
public CurrentWeatherCardView(CurrentWeather currentWeather, Context context, Activity activity, ViewGroup viewGroup) {
super(context, activity);
this.currentWeather = currentWeather;
this.currentWeather.initialize(context, activity, MainActivity.API_KEY);
this.context = currentWeather.getContext();
this.viewGroup = viewGroup;
this.currentWeather.doOnResponse(() -> {
setupLayout();
setCw_title(currentWeather.getName(), currentWeather.getText());
Weather.setIconDrawable(currentWeather.getIcon(), icon_image_view, currentWeather.getIs_day());
setTemperature(currentWeather.getTemp_f());
});
}
public CurrentWeatherCardView(Context context) {
super(context);
}
public CurrentWeatherCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CurrentWeatherCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void setupLayout() {
LayoutParams cardParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, pixels(200));
cardParams.setMargins(
pixels(15),
pixels(15),
pixels(15),
0
);
super.setLayoutParams(cardParams);
super.setRadius(pixels(4));
super.setUseCompatPadding(true);
linearLayout_604 = new LinearLayout(context);
linearLayout_604.setBaselineAligned(false);
linearLayout_604.setOrientation(VERTICAL);
LayoutParams layout_170 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout_604.setLayoutParams(layout_170);
this.addView(linearLayout_604);
linearLayout_600 = new LinearLayout(context);
linearLayout_600.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams layout_676 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, pixels(50));
linearLayout_600.setLayoutParams(layout_676);
linearLayout_604.addView(linearLayout_600);
cw_title = new TextView(context);
cw_title.setTypeface(Typeface.SANS_SERIF);
cw_title.setTextSize(pixels(5));
LayoutParams layout_914 = new LayoutParams(pixels(250), pixels(25));
layout_914.setMarginStart(pixels(10));
layout_914.topMargin = pixels(10);
cw_title.setLayoutParams(layout_914);
linearLayout_600.addView(cw_title);
icon_image_view = new ImageView(context);
LayoutParams layout_501 = new LayoutParams(pixels(75), pixels(40));
layout_501.setMarginStart(pixels(45));
layout_501.topMargin = pixels(10);
icon_image_view.setLayoutParams(layout_501);
linearLayout_600.addView(icon_image_view);
linearLayout_434 = new LinearLayout(context);
linearLayout_434.setOrientation(HORIZONTAL);
LayoutParams layout_204 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout_434.setLayoutParams(layout_204);
temp_text_view = new TextView(context);
temp_text_view.setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
temp_text_view.setTextSize(pixels(8));
LayoutParams layout_725 = new LayoutParams(pixels(100), pixels(100));
layout_725.setMarginStart(pixels(25));
layout_725.topMargin = pixels(10);
temp_text_view.setLayoutParams(layout_725);
linearLayout_434.addView(temp_text_view);
linearLayout_604.addView(linearLayout_434);
viewGroup.addView(this);
}
private void setCw_title(String title, String condition) {
cw_title.setText(title + " - " + condition);
}
private void setTemperature(double temp) {
temp_text_view.setText(String.valueOf((int) temp) + '\u00b0');
}
}
It's messy, but is there a better way? Doing it this way allows me to create this CardView by just instantiating the class, but I'm running into issues with figuring out what kind of view I should put these in. RecyclerView doesn't really seem like the right choice because as far as I can tell it's creates cards from a dataset. In my case, the CurrentWeatherCardView will always be the same.
That leads me to my next question. In what way can I have a scrollable, side-swipeable set of cards? RecyclerView? ListView? I'm pretty lost, and any guidance would help.
CardView is extended from FrameLayout, so you can just use a xml to design what your card looks like, and use findViewById in this CurrentWeatherCardView. For example:
public CurrentWeatherCardView(Context context) {
super(context);
init();
}
public CurrentWeatherCardView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CurrentWeatherCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
LayoutInflater.from(this).inflate(R.layout.xxx, this, true);
xxx = findViewById(R.id.xxxxx);
...
xxx.setText(xxxx)
// if you want to change visibility, just call:
setVisibility(View.GONE/View.VISIBILE);
}
Hello I'm a beginner in Android and developing a game. Currently I am trying to make some images appear in the screen. I set the images in the layout to an array and I want indexes to generate randomly in order to display images. My problem is that In the beginning I want the images to be hidden then after the game start they appear randomly. Here is my code. Thanks in advance.
package com.example.evo;
import android.app.Activity;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
public class Play extends Activity {
private ImageView iv1, iv2, iv3,iv4,iv5,iv6;
private ImageView[] IMGS = { iv1, iv3, iv3, iv4, iv5, iv6 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.play_game);
iv1 = (ImageView) findViewById(R.drawable.player);
iv2 = (ImageView) findViewById(R.drawable.player);
iv3 = (ImageView) findViewById(R.drawable.player);
iv4 = (ImageView) findViewById(R.drawable.player);
iv5 = (ImageView) findViewById(R.drawable.player);
iv6 = (ImageView) findViewById(R.drawable.player);
IMGS[0] = iv1;
IMGS[1] = iv2;
IMGS[2] = iv3;
IMGS[3] = iv4;
IMGS[4] = iv5;
IMGS[5] = iv6;
while(true) {
Random random = new Random();
int rndIndex = random.nextInt(IMGS.length);
}
}
Regarding the part you want the images to be hidden from the beginning, you can in your xml add a tag for each imageView android:visibility = "gone"
In your code while(true) will make the layout unresponsive you should make a repeated thread or a timer which helps you to call the function repeatedly
About hiding and showing the imageViews
IMGS[0].setVisibility(View.VISIBLE); // to show the image
IMGS[0].setVisibility(View.GONE); // to hide the image
And i think you are using drawable instead of id to get the imageView from xml, and you are using the same image
Use the setVisibility() method with the values VISIBLE/GONE.
iv1.setVisibility (View.GONE)
You can use .setVisiblity(int visibility) to hide or show your ImageViews (as per http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29).
In your case, you should be able to do:
Random random = new Random();
int rndIndex = random.nextInt(IMGS.length);
IMGS[rndIndex].setVisibility(View.VISIBLE);
To show the view. To hide the last one, I would suggest defining rndIndex with your array so that you could do
IMGS[rndIndex].setVisibility(View.GONE);
before the chunk of code above, meaning it would look more like:
IMGS[rndIndex].setVisibility(View.GONE);
Random random = new Random();
rndIndex = random.nextInt(IMGS.length);
IMGS[rndIndex].setVisibility(View.VISIBLE);
Good luck!
My question is: why, if i use LinearLayout, instead of Object, as "Result" of my AsyncTask (TableWithinExpListTask<Params, Progress, LinearLayout>) , Eclipse give me many errors like Cannot instantiate the type LinearLayout?
It seems to me that it doesn't recognise LinearLayout anymore in createFormattedCell() and i can't undertstand why.
In the AsyncTask declaration LinearLayout has yellow underline and Eclipse says: The type parameter LinearLayout is hiding the type LinearLayout.
Please someone can explain this to me?
Here is the code of the class:
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TableWithinExpListTask<Params, Progress, LinearLayout> extends AsyncTask<Params, Progress, LinearLayout> {
private final int TABLE_BORDER = 1;
private final int TABLE_TEXT_PADDING = 10;
private Context context = null;
private String str = null;
private boolean tableHeader = false;
private LinearLayout column = null;
public TableWithinExpListTask(Context context, String str, boolean tableHeader, LinearLayout column) {
this.context = context;
this.str = str;
this.tableHeader = tableHeader;
this.column = column;
}
#Override
protected LinearLayout doInBackground(Params... arg0) {
return this.createFormattedCell(this.tableHeader, this.str);
}
#Override
protected void onPostExecute(LinearLayout result) {
this.column.addView(result);
}
private LinearLayout createFormattedCell(boolean tabHeader, String str) {
// Layout che circonda le textView necessario per disegnare il bordo
// delle celle
LinearLayout container = new LinearLayout(this.context);
container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
container.setBackgroundColor(Color.BLACK);
TextView textView = new TextView(this.context);
textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
textView.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
if (tabHeader) {
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setBackgroundColor(this.context.getResources().getColor(R.color.light_grayish_orange));
}
textView.setText(str);
container.addView(textView);
return container;
}
}
I see other question about this but i don't fully understand this behavior in this case.
Having the generic parameters (the stuff in angle brackets) attached to the name of your class is telling Java that you want users of your class to be able to specify the types involved, and you're using those names as the "variable names" for the types those users choose. See, for example, Map<K,V>, where K and V represent the types of the Map's keys and values. When you listed LinearLayout as a type parameter, the compiler thought that you were just using it as a placeholder for some other class that users would pick, and it didn't know how to construct one.
You're wanting your concrete class to extend a class that uses generics, but you know the specific types that you want to fill in there, so you don't put type parameters on your own class, just on the one you're using. For example, if you were writing a custom Map class that only mapped Strings to Integers, you would say public class MyMap implements Map<String, Integer>.
Change the whole code to this:
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TableWithinExpListTask extends
AsyncTask<Void, Void, LinearLayout> {
private final int TABLE_BORDER = 1;
private final int TABLE_TEXT_PADDING = 10;
private Context context = null;
private String str = null;
private boolean tableHeader = false;
private LinearLayout column = null;
public TableWithinExpListTask(Context context, String str,
boolean tableHeader, LinearLayout column) {
this.context = context;
this.str = str;
this.tableHeader = tableHeader;
this.column = column;
}
#Override
protected LinearLayout doInBackground(Void... arg0) {
return this.createFormattedCell(this.tableHeader, this.str);
}
#Override
protected void onPostExecute(LinearLayout result) {
this.column.addView(result);
}
private LinearLayout createFormattedCell(boolean tabHeader, String str) {
// Layout che circonda le textView necessario per disegnare il bordo
// delle celle
LinearLayout container = new LinearLayout(this.context);
container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
container.setBackgroundColor(Color.BLACK);
TextView textView = new TextView(this.context);
textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING,
TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
textView.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
textView.setLayoutParams(params);
if (tabHeader) {
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setBackgroundColor(this.context.getResources().getColor(
R.color.light_grayish_orange));
}
textView.setText(str);
container.addView(textView);
return container;
}
}
I believe also you would need to have a loot a bit at Java generics. I would start to read the Oracle tutorial for this.
I'm trying to fix my camera app. When i hold the camera in landscape mode, it previews on the screen sideways.
I found this fix on stack : Android - Camera preview is sideways
In my CameraSurfaceView class constructor, I get the surface like this this.Surface = getHolder();.
In the surfaceChanged method i check the display rotation against Surface.ROTATION_<#> for 0,90,180,and 270 degrees (method below). However, each one of the ROTATIONSurface constants is underlined with the following error ROTATION_<#> cannot be resolved or is not a field. I am not sure what I am doing wrong. My CameraSurfaceView is separate from my activity so I'm wondering if the display has something to do with it?
Hope this is explained correctly, if you need more code or anything let me know. I appreciate this in advance!
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ZoomControls;
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
int orientation = 0;
Camera.Parameters params = Cam.getParameters();
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
android.view.Display display = window.getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
params.setPreviewSize(height, width);
Cam.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_90)
{
params.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_180)
{
params.setPreviewSize(height, width);
}
if(display.getRotation() == Surface.ROTATION_270)
{
params.setPreviewSize(height, width);
Cam.setDisplayOrientation(180);
}
List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
Camera.Size previewSize = getBestPreviewSize(width, height);
params.setPreviewSize(previewSize.width, previewSize.height);
int zoom = 0;
params.setZoom(zoom);
zoomControls.setOnZoomInClickListener(new View.OnClickListener(){
public void onClick(View v){
Camera.Parameters params = Cam.getParameters();
maxZoomLevel = params.getMaxZoom();
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
Cam.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener(){
public void onClick(View v){
Camera.Parameters params = Cam.getParameters();
maxZoomLevel = params.getMaxZoom();
if(currentZoomLevel > 0){
currentZoomLevel--;
Cam.startSmoothZoom(currentZoomLevel);
}
}
});
Cam.setParameters(params);
try {
Cam.setPreviewDisplay(Surface);
} catch (IOException e) {
e.printStackTrace();
}
Cam.startPreview();
}
Since you have a field named Surface, you cannot access the class Surface from within your class.
Instead, you will have to write
android.view.Surface.ROTATION_0
or rename your field.
You've named your field Surface, which is also the name of the type you are trying to reference a constant from. Rename the field so that it does not have the same name as the type, then import android.view.Surface and you should be able to reference Surface.ROTATION_0 etc with no problems.
Note that Java style usually dictates you start variable names with lower case, and types with upper case, which avoids this type of conflict.
I already made a simple android application, which I also have integrated Google Maps in it..
It is also capable of connecting to MySQL (localhost) to display my desired places using longitude and latitude values..
My question is, is it possible to make another overlay item above Google Maps when a marker is clicked (just like what happens in foursquare)?
To be specific, i want to display a text that contains the name of a place.
Heres my class of displaying the overlay items.
I made an onTap method, but it display a dialog box, I want to display a simple text box that shows the name of the place.
package finddroid.map;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private int markerHeight;
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
populate();
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context)
{
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i)
{
return mapOverlays.get(i);
}
#Override
public int size()
{
return mapOverlays.size();
}
#Override
//Event when a place is tapped
protected boolean onTap(int index)
{
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay)
{
mapOverlays.add(overlay);
this.populate();
}
}
Have a look at this project - balloon itemized overlay. It is using own class extending FrameLayout to show balloons.
So if you want to modify your code put this into your onTap method to display a TextView above taped item
TextView text = new TextView(context);
text.setText(item.getTitle());
MapView.LayoutParams params = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, item.getPoint(), MapView.LayoutParams.BOTTOM_CENTER);
params.mode = MapView.LayoutParams.MODE_MAP;
mMapView.addView(text, params);
I think this code is simple and easy to understand and you can improve it accordingly to your needs. To make it work you have to pass instance of MapView to constructor of your overlay and save it to private variable mMapView.
private MapVeiw mMapView;
public CustomItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView) {
this(defaultMarker);
this.context = context;
this.mMapView = mapView;
}
And don't forget to add MapView as one of parameters when you call new CustomItemizedOverlay().