View over surface view : Works on some android version - java

I'm actually making an android app which uses a SurfaceView to draw the Camera.
I want to add some views over it.
In fact, i already done it. It works on my android 4.1.1. All my views are drawn over the Camera.
But when i try it on a Android 2.3, it don't works anymore.
Depends of what i'm doing, it shows a part of the view in the left-top corner (As if the parent layout were too small)
Here is the xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<com.example.calculatrice.CustomCameraView
android:id="#+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/poiGroup">
<Button
android:id="#+id/bBack"
android:layout_width="150dp"
android:layout_height="80dp">
</Button>
</LinearLayout>
</FrameLayout>
I have a button, wich is perfectly shown over the camera.
I put the views i want to draw under that button (with "layout(x,x,x,x)"). They appear cuted (or don't appear, depend).
Here is some usefull code
//In oncreate method
customCamera = (CustomCameraView)this.findViewById(R.id.cameraView);
//Create view and add it to the layout
public void initObjects()
{
listPoiMarker = new LinkedList<POIMarker>();
int i = 0;
while (i < names.length && names[i] != null)
{
Location locTmp = new Location(LocationManager.NETWORK_PROVIDER);
locTmp.setLatitude(coordonates[0][i]);
locTmp.setLongitude(coordonates[1][i]);
POIMarker tmp = new POIMarker(getApplicationContext());
tmp.setPoiId(id[i]);
tmp.setId(i);
tmp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tmp.setName(names[i]);
tmp.setLocation(locTmp);
tmp.posScreenY = 100 + (i) * 60;
listPoiMarker.add(tmp);
tmp.setOnTouchListener(onTouchListener);
((LinearLayout) findViewById(R.id.poiGroup)).addView(tmp);
tmp.layout(50, 50, 250, 250);
i++;
}
}
Not exactly what i do. In fact the layout function is somewhere else.
But it's the same.
That works fine in Android 4.1.1, but not in 2.2 or 2.3
I looking for a solution for about 2 days. I tried 999 things, none worked.
That's why i come here to ask some help.
(Here is an example of what it does : http://drawsave.com/19Z
I don't want to add a picture.. Take too long
The red thing is the view i add. It's supposed to be a rectangle. But it's cuted, on 2.2 | 2.3 ..
)
Thanks for help
EDIT:
I found a way to fix it.
How ?
I added a transparent background to the layout containing the views. So it don't resize anymore, it keeps the right size.
(Yep, that's not really logical, but it works)

Related

how to set android ImageView visibility inside an included layout?

We are using android API 17 in our application. I have defined a layout containing two images vies as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_container_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_1_resource"/>
<ImageView
android:id="#+id/image_2"
android:layout_marginTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_container_layout"
android:src="#drawable/image_2_resource"/>
This layout is included inside another layout as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#style/wizard_content_style"
tools:context=".ui.Wizard"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
>
<include layout="#layout/image_container_layout"
android:id="#+id/included_view"
/>
<TextView
style="#style/wizard_content_text_style_medium"
android:id="#+id/text_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/included_view"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="#string/instruction"
android:layout_marginBottom="15dp"/>
The reason that the layout is included is that we want to reuse it in two more layouts.
Now based on some condition I want to hide or show the image views inside image_container_layout.
The java code looks like this:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
image1 = (ImageView) containerLayout.findViewById(R.id.image_1);
image2 = (ImageView) containerLayout.findViewById(R.id.image_2);
switch (accuracy) {
case 1:
log().i(getClass().getSimpleName(), "case 1 chosen");
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
log().i(getClass().getSimpleName(), "image 1 has been shown");
break;
case 2:
image1.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
break;
case 3:
image1.setVisibility(View.VISIBLE);
image2.setVisibility(View.VISIBLE);
break;
}
I am debugging this code and I am sure the code is running. The log messages are printed in Logcat as well, but nothing happens no change in the images. Also, both images are always shown.
I wonder if there is something that I have to do when working with the included layout?
Thanks for any help in advance.
Based on answers I got below, seems that inflating a view will create a new object and because of this, changes in the visibility are not shown on the user interface.
Then the question is that if we have a wizard and inside 3 different pages of the wizard I want to have an image and depending on some condition I want to show or hide the image, what is the best solution? I mean I want to reuse the code which is responsible for hiding and showing the image regardless which page of wizard is active.
Why are you complexing with so much code. If you include some layout in your xml then you can use those widgets also same as the xml have. There is no need to inflate.
ImageView image_2 = findViewById(R.id.image_2);
image_2.setVisbility(Visible.GONE);
You said at this comment the code not inside activity but wherever it is you inflated a new layout to your view currently displaying by this line:
containerLayout = (ViewGroup) ((Activity) getAndroidContext()).getLayoutInflater().inflate(R.layout.image_container_layout, null);
When you try to change visibility of those images actually it works, i think so. But if your activity or fragment layout contains image_container_layout maybe you see
those images.
And I wonder that what do you do with inflated view containerLayout. Do you add it to inside of any other view. If you dont it wont be visible for you.
you have to use it like this:
View included_view1 = findViewById(R.id.included_view1);
ImageView image_1 = included_view1.findViewById(R.id.image_1);
ImageView image_2 = included_view1.findViewById(R.id.image_2);
image_1.setVisibility(View.VISIBLE);
image_1.setVisibility(View.GONE);
image_2.setVisibility(View.VISIBLE)
image_2.setVisibility(View.GONE)
View included_view2 = findViewById(R.id.included_view2);
ImageView image_11 = included_view2.findViewById(R.id.image_1);
ImageView image_22 = included_view2.findViewById(R.id.image_2);
image_11.setVisibility(View.VISIBLE);
image_11.setVisibility(View.GONE);
image_22.setVisibility(View.VISIBLE)
image_22.setVisibility(View.GONE)
Above code will be helpful in the case of multiple time you want to use same layout.

Picasso centerCrop and fit get different results

I want to insert an image to an ImageView, which gets used as a background. This background is in a RecyclerView. When it gets loaded the first time, it looks like this:
But when I scroll to another item and scroll back, it looks like this (it should always look like this):
Here i add the image:
public void onBindViewHolder(StoryViewHolder holder, int position) {
holder.cardView.setMinimumHeight(height/3);
holder.layout.setMinimumHeight((int) ((float) width / 4));
//set image
Picasso.with(activity).load(MainPostAdapter.URL + 140 + ".png").
transform(new BlurTransform(holder.background.getContext())).fit().centerCrop().into(holder.background);
holder.background.setAlpha(0.25f);
}
And this is the image itself:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/story_view_background"/>
What could the problem be?
EDIT:
whole XML (It doesn't show all of the code. Here is also pastebin:http://pastebin.com/8rJvSx7V):
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/story_view_background"/>
<RelativeLayout
android:id="#+id/story_view_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50px">
<ImageView
android:id="#+id/story_view_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/story_view_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_below="#+id/story_view_image"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</FrameLayout>
try to set android:layout_height="150dp" for your CardView (don't use px!). Its child FrameLayout is unnecesarry (CardView is extending FrameLayout and have set same attributes set), remove it.
It's not so well written, all your Views have match_parent set for height, besides Image and Text views... Your item is probably trying to fit RecyclerView height in this case ("whole screen") and is measured without image at first time (Picasso async downloading, setting image when view is already measured). But when you inflate your View again (scroll and recycle) new list item will be measured differently, because Picasso will insert your image "in runtime", no need to async fetching (cache)

Android VideoView hides Layout Elements

I have the following layout (cut down to a minimal example)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/free_text_default_bg"
android:gravity="center_horizontal"
android:padding="#dimen/free_text_padding"
android:text="background element is a video view" />
</RelativeLayout>
The problem is, that if I move the EditText around (it has a drag listener attached to it), it dissapears at a certain point of the screen as you can see in this screenshot.
The problem seems to only appear when I have a VideoView. Even if I don't show a video.
When I replace it with e.g. an ImageView there is no problem at all.
So my question is, what's happening and how can I resolve this?
Thanks for your time!
UPDATE 1: This only happens if the VideoView has
android:layout_height="match_parent"
when I set it to some "dp" value everything works just fine...
I did not find a solution to my question jet but I have found a workaround:
Once the layout is finished, I get the height from the parent container, subtract 1px and set this as new height for the video view.
The 1px is barely visible...
Here is the code:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
layoutParams.height = parentView.getHeight() - 1;
videoView.setLayoutParams(layoutParams);
}

how to create android linearlayout with RTL horizontal orientation

I want to create a linear layout with horizontal orientation
but I want it to position its children aligned to its right and not to its left as usual
how can I do this?
I know how to do this using relativeLayout, but I want to practice linearLayout
You can use android:layoutDirection attribute which is introduced in 4.2(jelly bean).
The following links will help.
http://developer.android.com/reference/android/view/View.html#attr_android:layoutDirection
http://android-developers.blogspot.co.uk/2013/03/native-rtl-support-in-android-42.html
if you are using Android studio you can do this:
1- right click on the project main folder
2- refactor
3- add support for RTL
you can use this code snippet to reverse your layout views.
LinearLayout ll = // inflate
ArrayList<View> views = new ArrayList<View>();
for(int x = 0; x < ll.getChildCount(); x++) {
views.add(ll.getChildAt(x));
}
ll.removeAllViews();
for(int x = views.size() - 1; x >= 0; x--) {
ll.addView(views.get(x));
}
OR
To begin supporting RTL layouts in your app, set the android:supportsRtl attribute to the element in your manifest file and set it “true". Once you enable this, the system will enable various RTL APIs to display your app with RTL layouts. For instance, the action bar will show the icon and title on the right side and action buttons on the left, and any layouts you’ve created with the framework-provided View classes will also be reversed.
Look at this android doc
You can set on the parent view element's gravity to right
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" <!-- make sure this is not wrap_content !-->
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
</LinearLayout

Application do not fit all the display size

I'm building a Android application, and it's designed for Galaxy Tab, by specs, the screen size is 1024x600, but if I try to add my component in this size, the left and bottom are cut off.
I added the following code to see the difference
WindowManager mWMgr = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
int width = mWMgr.getDefaultDisplay().getWidth();
int height = mWMgr.getDefaultDisplay().getHeight();
Toast.makeText(getApplicationContext(),
"[system][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
Toast.makeText(getApplicationContext(),
"[this][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();
When running, this shows:
[system][w=600:h=1024]
[this][w=400:h=683]
Why it happens?
If needed I add my code to put the components on screen.
This is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="#+id/image01" android:layout_width="match_parent"
android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>
In this case, the image I draw on the ImageView get scaled to the screen size (400x683)
I tested with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="600px"
android:layout_height="1024px">
<ImageView android:id="#+id/image01" android:layout_width="match_parent"
android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>
And by this way, the left and bottom of the image is cut off the screen.
Don't use Display.getWidth/Height(). These return the raw size of the display. This is not how much space you actually have. For example, the status bar will reduce the actual space you have from that, by some undefined amount along some axis. And don't think using FLAG_FULLSCREEN will save you from this, because it is perfectly reasonably for a device to have a "status bar" that contains things like the back and home button, and thus not something you can get rid of.
The correct way to resize to the screen is through the view system. The simplest thing here is just to make a subclass of View that implements onSizeChanged(), and stick this in with setContentView(). Once the layout pass completes, your onSizeChanged() will be called with the space available for the view. This will be called whenever that space changes -- due to an orientation change, display change, IME being shown, etc.
Does it help to specify the respective value in the manifest:
<supports-screens android:smallScreens=["true" | "false"]
android:normalScreens=["true" | "false"]
android:largeScreens=["true" | "false"]
android:anyDensity=["true" | "false"] />
See Android docs on this.
What do you have in your XML?
Fill parent or wrap content for the view? you need first one depending on what you're doing...
Edit: on top Thorsten Dittmars answer...

Categories