ScrollView doesn't want to show up with Image - android

I'm wondering all the time why the scrollView dosen't want to show up. I'm downloading some image from the Internet and then setting it as a Drawable in a ImageView but nothing happens. When picture is small everything works fine. Where is the mistake?
Funkction where I'm populating View:
public void setUpLayout()
{
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
heightOfScreen = height;
heighOfImage = image.getHeight();
System.out.println("Screen" +height);
System.out.println("IMAGE" + heighOfImage);
if(heighOfImage < height)
{
imageViewSmall.setImageBitmap(image);
imageViewSmall.setVisibility(View.VISIBLE);
scrollViewImage.setEnabled(false);
}
else
{
scrollViewImage.setVisibility(View.VISIBLE);
imageViewBig.setImageBitmap(image);
//imageViewBig.setVisibility(View.VISIBLE);
scrollViewImage.setEnabled(false);
}
}
And XML file with all content View:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="20dp"
android:layout_width="fill_parent"
android:id="#+id/textB"
android:text="dadasadsadsasddassdsdsdsadasdd"/>
<com.polites.android.GestureImageView
android:id="#+id/imageSmal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below ="#+id/textB"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
gesture-image:max-scale="10.0"
gesture-image:min-scale="0.0"
gesture-image:strict="false" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageSmal"
android:id="#+id/scrollViewImage"
android:scrollbars="none"
android:visibility="gone">
<com.polites.android.GestureImageView
android:id="#+id/imageBig"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
gesture-image:max-scale="10.0"
gesture-image:min-scale="0.0"
gesture-image:strict="false" />
</ScrollView>
<ProgressBar
android:id="#+id/imageProgressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>

Related

Layout issue in different device in Android

I have Imageview inside a framelayout. I have set scaletype for imageview as MATRIX, but it's looks different in different devices.
Here is layout I used:
<FrameLayout
android:id="#+id/canvasView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible"
tools:context=".MainActivity">
<ImageView
android:id="#+id/img_baseview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix" />
<Utils.DrawingHelper
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
/>
</FrameLayout>
In my Java file I am displaying image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
actualImageBitmap = BitmapFactory.decodeFile(new File(Datas.received_uri.getPath()).getAbsolutePath(), options);
ImageViewHeight = options.outHeight;
ImageViewWidth = options.outWidth;
img_baseview.setScaleType(ImageView.ScaleType.MATRIX);
canvasView.getLayoutParams().height = ImageViewHeight;
canvasView.getLayoutParams().width = ImageViewWidth;
canvasView.requestLayout();
Log.e("ESize", ImageViewWidth + "---" + ImageViewHeight);
Log.e("ESize", canvasView.getHeight() + "---" + canvasView.getWidth());
try {
actualImageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Datas.received_uri);
bitmapMaster = actualImageBitmap;
effect_bitmap = bitmapMaster;
} catch (IOException e) {
e.printStackTrace();
}
// img_baseview.setImageURI(Datas.received_uri);
img_baseview.setImageBitmap(actualImageBitmap);
use CENTER_INSIDE
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerInside".
instead of MATRIX
remve img_baseview.setScaleType(ImageView.ScaleType.MATRIX);
Check out
<FrameLayout
android:id="#+id/canvasView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Utils.DrawingHelper
android:id="#+id/drawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:layout_alignLeft="#+id/img_baseview"
android:layout_alignStart="#+id/img_baseview"
android:layout_alignRight="#+id/img_baseview"
android:layout_alignEnd="#+id/img_baseview"
android:layout_alignTop="#+id/img_baseview"
android:layout_alignBottom="#+id/img_baseview" />
<ImageView
android:id="#+id/img_baseview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher"/>
</RelativeLayout>
</FrameLayout>

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

Android layout scaling (for elements of a given size in dp)

Three FrameLayout's of different background colors to form a strip.
Each FrameLayout has the size specified in dp.
[
The strip is long and does not fit on the screen.
How to scale the strip to maintain the aspect ratio and without changing the dimensions dp?
[
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:rotation="0">
<FrameLayout
android:layout_width="200dp"
android:layout_height="42dp"
android:background="#508da6"
/>
<FrameLayout
android:layout_width="400dp"
android:layout_height="42dp"
android:background="#494949"
/>
<FrameLayout
android:layout_width="240dp"
android:layout_height="42dp"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
There is no need to inflate three whole FrameLayout just for that.View will do just fine. Also you should not give fixed widths since there is no way to make it work for all screen sizes.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:measureAllChildren="true">
<LinearLayout
android:id="#+id/strip"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layoutDirection="ltr"
android:rotation="0">
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#508da6"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#494949"
/>
<View
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
/>
</LinearLayout>
</FrameLayout>
set height on runtime
LinearLayout stripLayout = (LinearLayout)findVieById(R.id.strip);
stripLayout .getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
#Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
// TODO Auto-generated method stub
int width = stripLayout .getWidth();
int height = //calculate your height//
FrameLayout.LayoutParams params = stripLayout .getLayoutParams();
params.height = height;
stripLayout .setLayoutParams(params);
if (Build.VERSION.SDK_INT < 16) { stripLayout .getViewTreeObserver().removeGlobalOnLayoutListener(this); }
else { stripLayout .getViewTreeObserver().removeOnGlobalLayoutListener(this); }
}
});

make horizontal listview's listitem's height dynamic

I have list of button in bottom of the page which is in horizontal listview, i want to show only 4 row in listview, when screen become bigger it's item size should also become bigger so only 4 item shows.
for example
here you can see that after increasing size of width it's related horizontal listview's listitem also increasing
is it possible? if possible than can any body suggest me how we can handle it ?
Create one custom layout class like below..
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context) {
super(context);
}
public void setLayoutParams(int width) {
LayoutParams params = new LayoutParams(width / 4,
LayoutParams.WRAP_CONTENT);
this.setLayoutParams(params);
}
#Override
public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
}
}
put this horizontal scrollview in your xml file
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.example.widget.MyLinearLayout
android:id="#+id/llMainHome"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/orange_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeFavorite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favorite" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeGrocery"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/make_juice" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSettings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/settings" />
</com.example.widget.MyLinearLayout>
</LinearLayout>
</HorizontalScrollView>
And in your activity class file declare
private MyLinearLayout llHomeSearch, llHomeFavorite,
llHomeGrocery, llHomeSettings,llMainHome;
oncreate Method code is
llHomeFavorite = (MyLinearLayout) findViewById(R.id.llHomeFavorite);
llHomeFavorite.setOnClickListener(this);
llMainHome = (MyLinearLayout) findViewById(R.id.llMainHome);
llMainHome.setOnClickListener(this);
llHomeSearch = (MyLinearLayout) findViewById(R.id.llHomeSearch);
llHomeSearch.setOnClickListener(this);
llHomeGrocery = (MyLinearLayout) findViewById(R.id.llHomeGrocery);
llHomeGrocery.setOnClickListener(this);
llHomeSettings = (MyLinearLayout) findViewById(R.id.llHomeSettings);
llHomeSettings.setOnClickListener(this);
Display display = getWindowManager().getDefaultDisplay();
Point size = getDisplaySize(display);
int width = size.x;
llMainHome.setLayoutParams(width);
llHomeFavorite.setLayoutParams(width);
llHomeGrocery.setLayoutParams(width);
llHomeSearch.setLayoutParams(width);
llHomeSettings.setLayoutParams(width);
and getDisplaysize method is
#SuppressLint("NewApi")
private static Point getDisplaySize(final Display display) {
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
return point;
}
here you go..it will work nice..
there is no need to take horizontal list view if you have fix number of item in your tab bar..
Maybe try to get Screen width and then set List's element to 1/4 of that width?
Something like in onResume/onCreate
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x; //remember that width
And in getView
LayoutParams params=new LayoutParams(width/4, -1);
element.setLayoutParams(params)

Define maximum RelativeLayout size

I have to define 5 layouts on the screen and place them like a 3x3 grid. Layouts is empty.
At some moment I want programmatically to add some view on one layout and that view mustn't grows over allocated size of layout. In other words I want to have layout in which to put any view and it will be guaranty that it won't grows over that layout. Important note: I don't have rights to change manually size of added view, I only can to control its maximum size.
I have right now such layout:
<RelativeLayout
android:id="#+id/pluginsLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/mainButtons"
android:layout_below="#+id/paramsLayout">
<RelativeLayout
android:id="#+id/capturingPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_horizontal|left"
android:layout_above="#+id/processingPluginLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/processingPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_horizontal|left"
android:layout_above="#+id/filterPluginLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/filterPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_horizontal|left|bottom"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
</RelativeLayout>
<RelativeLayout
android:id="#+id/exportPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_horizontal|right"
android:layout_above="#+id/viewfinderPluginLayout">
</RelativeLayout>
<RelativeLayout
android:id="#+id/viewfinderPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="fill_horizontal|bottom|right"
android:layout_toLeftOf="#+id/filterPluginLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</RelativeLayout>
</RelativeLayout>
And I added new view in code:
public void addVFPluginView(View view)
{
((RelativeLayout)MainScreen.thiz.findViewById(R.id.viewfinderPluginLayout)).addView(view);
}
I expect to got like this behaviour:
But I got only this, view fill entire parent layout:
Note: I use API8 (don't have GridLayout)
u can use grid layout in API8.
I've managed to solve this issue.
The main idea was to control view's sizes by myself programmatically.
I calculate appropriate proportion for each zone and when somebody add a new view to some of this layout I check suitability of the size of this view to the allocated size of layout. If size is not suitable I modify LayoutParams for this View.
Here is the xml of empty layouts:
<RelativeLayout
android:id="#+id/pluginsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/mainButtons"
android:layout_below="#+id/paramsLayout">
<LinearLayout
android:id="#+id/capturePluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</LinearLayout>
<LinearLayout
android:id="#+id/processingPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true">
</LinearLayout>
<LinearLayout
android:id="#+id/filterPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
</LinearLayout>
<LinearLayout
android:id="#+id/exportPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
</LinearLayout>
<RelativeLayout
android:id="#+id/viewfinderPluginLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>
And like that I manage the size of adding views:
public void addVFPluginView(View view)
{
//Get maximum width and height for such plugins
int layoutHeight = this.getViewfinderLayoutHeight();
int layoutWidth = this.getViewfinderLayoutWidth();
//Calculate appropriate size of added plugin's view
android.widget.RelativeLayout.LayoutParams viewLayoutParams = (android.widget.RelativeLayout.LayoutParams)view.getLayoutParams();
viewLayoutParams = this.getTunedRelativeLayoutParams(view, viewLayoutParams, layoutWidth, layoutHeight);
//Get fixed maximum size for this plugin type
public int getViewfinderLayoutWidth()
{
return ((RelativeLayout)MainScreen.thiz.findViewById(R.id.pluginsLayout)).getWidth()/2;
}
public int getViewfinderLayoutHeight()
{
return (int)(((RelativeLayout)MainScreen.thiz.findViewById(R.id.pluginsLayout)).getHeight()*0.7);
}
((RelativeLayout)MainScreen.thiz.findViewById(R.id.viewfinderPluginLayout)).addView(view, viewLayoutParams);
}
android.widget.RelativeLayout.LayoutParams getTunedRelativeLayoutParams(View view, android.widget.RelativeLayout.LayoutParams currParams, int goodWidth, int goodHeight)
{
int viewHeight, viewWidth;
if(currParams != null)
{
viewHeight = currParams.height;
viewWidth = currParams.width;
if(viewHeight > goodHeight || viewHeight <= 0)
viewHeight = goodHeight;
if(viewWidth > goodWidth || viewWidth <= 0)
viewWidth = goodWidth;
currParams.width = viewWidth;
currParams.height = viewHeight;
view.setLayoutParams(currParams);
}
else
{
currParams = new android.widget.RelativeLayout.LayoutParams(goodWidth, goodHeight);
view.setLayoutParams(currParams);
}
return currParams;
}
Finally, next issue is for several views with suitable sizes but in sum they're bigger than allocated size. Maybe I'll just straightly count sizes of views and if the sum with the size of next adding view will be more than allocated size I just disallow to add this view.

Categories

Resources