How can i Blur the background of my textview in android studio - java

I have an image with a textview on top of it. I am wondering how I can blur the background of my textview (only part of the image) to make it more readable. I have tried looking at the Blurry library but it seems like it can only blur the whole picture.

Add this custom BlurView solution: https://github.com/mmin18/RealtimeBlurView
Then put your BlurView positioned behind the TextView in your layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<com.github.mmin18.widget.RealtimeBlurView
android:id="#+id/textview_blurview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/my_textview"
android:layout_alignBottom="#+id/my_textview"
android:layout_alignStart="#+id/my_textview"
android:layout_alignLeft="#+id/my_textview"
android:layout_alignEnd="#+id/my_textview"
android:layout_alignRight="#+id/my_textview"/>
<TextView
android:id="#+id/my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text in front of blur view"/>
</RelativeLayout>
Finally, set the blur and color as desired in code
blurView.setBlurRadius(80.0f);
blurView.setOverlayColor(R.color.background_blur_color);

You can use frame layout to produce a blur effect , frame layout allows to align views along z-axis the last child appears above all other children, example of usage:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_height="200dp"/>
<TextView
android:layout_width="match_parent"
android:background="#10000000"
android:layout_gravity="bottom"
android:layout_height="100dp" />
<TextView
android:layout_width="wrap_content"
android:text="Text Here"
android:layout_marginBottom="50dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_height="wrap_content" />
</FrameLayout>
You can also set the textview background to some color (#AARRGGBB) with lower aplha (AA < "FF" ->max alpha) to reproduce a similar effect easily.

As you want to make your textview more readable as it sits over the imageview. To do this instead of blurring image why don't you use gradient drawable or color.
I had same requirement in my app and I simply went with placing grading view over the part of ImageView.
Let's do this
Step 1: Create a gradient shape and put it under drawable folder, let say shape name is gradient_overlay.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#00000000"
android:startColor="#BB000000"/>
</shape>
Step 2: Create a dummy view and place this over the ImageView or part of ImageView.
<View
android:id="+#id/gradientView"
android:layout_width="match_parent"
android:layout_height="60dp" // adjust the height
android:layout_alignBottom="#id/picture" // give the id of your image
android:background="#drawable/gradient_overlay">
</View>
Step 3: Then align your textview over the gradientView. so that text become proper visible.

Related

How can I correctly put an image at the top of an ImageView having a specified height?

I am absolutly a beginner in Android development and I have some problem positioning an image into an ImageView element of my layout. So I have the following situation, I have this fragment layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Dummy content. -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:scaleType="fitCenter"
android:layout_height="350dp"
android:src="#drawable/carbonara"/>
<!--android:background="#drawable/carbonara" />-->
<TextView android:id="#android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
</LinearLayout>
</ScrollView>
So, as you can see in the previous code snippet I am setting a background image into an ImageView having an height of 350dp setted.
The problem is that the image height is less that 350dp and this is what I am obtaining:
As you can see in the previous screenshot what I obtain is that I have the 350dp ImageView (highlighted in blue) and the image is setted inside it vertically centered. I don't want that the setted image is vertically centerd but I want that it start at the top of the ImageView.
How can I implement this behavior? What am I missing?
I have tryed to change:
android:src="#drawable/carbonara"/>
with:
android:background="#drawable/carbonara" />
and in this way it fills all the ImageView space but in this way the image could appear deformed so I think that it is not a good solution.
try to change in your layout and test again.
android:scaleType="fitCenter"
to
android:scaleType="fitStart"
See full docs here

Forcing image to be cropped to wrap content

I have a Dialog where I set the layout to a custom layout I created. What I'm trying to do is make the background image (in this case the ImageView) crop to just what is needed to cover the space of the content of the TextView. This is how it looks now:
But what I need is for the background image to be cut off after the "content" line of the TextView. I'm not sure of a way to do this. The only way I found was to hardcode the RelativeLayout height (i.e. 200dp) but the TextView's content will be dynamic and will grow/shrink so this isn't an solution for me. Any ideas? Thanks
Here is the XML for this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="#drawable/blue9" >
</ImageView>
<TextView
android:id="#+id/tvMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvTitle"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="#FFF"
android:text="This \n is \n my \n content"
android:textSize="20sp" />
</RelativeLayout>
Also, the same effect happens when I set the background of the RelativeLayout to the drawable. I was hoping the scaleType attribute on the ImageView would help.
I believe you should be able to set the TextView's background attribute to be the drawable.

Android button layout adapting to screen size

I am new in Android development.I created a simple project with a simple button.I thought that the
button will look the same on different screen sizes but when i previewed all screens eclipse displayed this
http://imgur.com/kjEMhHx
This is the xml code
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.businessideas.MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Button" />
</RelativeLayout>
Can you please help me and tell me how can i design that button look the same on all screen sizes?
Thanks
You'll need to use the weight attribute when the button's parent is LinearLayout. That way it will force the button to have a size proportional to the screens width:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:text="Button"
android:layout_weight="2"
android:layout_width="0dp"/>
</LinearLayout>
Notice that if the LinearLayout's orientation is horizontal, width will be adjusted by android (implicitly) therefore the children have width of 0dp.
Also note that now the button will take 20% (2/10) of the LinearLayout's (the parent's) width.
On the button part change:
android:layout_width="20dp"
android:layout_height="20dp"
Use specific DP sizes to keep items the same physical size on different screen (replace 20 with your desired size).
If you want your button to be of the same size in all different screen than you have to manually give the DP size.
like
andoird:layout_width = "20dp"
android:layout_height = "20dp"
You can also place your button at specific location by changing your relative layout to linear layout.

Android: Why does View background draw in editor but not in app?

I have an Android project with an xml file representing a row for an Adapter.
This row has a coloured View on the left hand side.
For some reason the View renders correctly in the graphical editor in Eclipse but not when I run it on a device.
The View draws at the correct size but there's no blue background like the editor has.
I've noticed when I dump the UI hierarchy that the View nodes aren't in the hierarchy.
If I break on binding the views in the adapter, I can see that mMeasuredHeight is correct but mMeasuredWidth is 0. I feel like I'm missing something fundamental.
Attached are screenshots and the xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="#+id/view_header_accent"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/blue_primary" />
<TextView
android:id="#+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="#dimen/grid_small"
android:layout_marginTop="#dimen/grid_small"
android:layout_toLeftOf="#+id/img_cancel"
android:layout_toRightOf="#+id/view_header_accent"
android:text="Brad"
android:textSize="18sp" />
<TextView
android:id="#+id/txt_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt_title"
android:layout_alignRight="#+id/txt_title"
android:layout_below="#+id/txt_title"
android:paddingBottom="#dimen/grid_small"
android:text="Active"
android:textColor="#80000000"
android:textSize="14sp" />
<ImageView
android:id="#+id/img_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/grid_small"
android:src="#drawable/ic_cancel" />
</RelativeLayout>
Screenshot from graphical editor in Eclipse:
Screenshot from device:
Found the issue.
The issue is because the top level view is a RelativeLayout with wrap_content as the height.
Combine this with the child having a layout_height of match_parent and the measured height for the coloured child View ends up getting calculated to 0.
The workaround unfortunately is to set a fixed height for the row or optionally specify the top and bottom alignment of the coloured view to match other child views.
The final View looks like this:
<View
android:id="#+id/view_header_accent"
android:layout_width="8dp"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/txt_subtitle"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/txt_title"
android:background="#color/blue_primary" />

Why is my 'transparent' overlay view opaque?

I have a single activity with a simple layout (a bunch of coloured panels) and I'm trying to overlay a small moving graphic on a transparent view over the top. Like #umar here I'm following this tutorial but my overlay view is refusing to be transparent. I can make the view appear, it contains just what it should, but the background of the view is black and however I try to make it transparent fails. I've tried a few of the solutions mentioned on SO and it always comes out the same.
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/everything"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00ffffff"
>
<LinearLayout
android:id="#+id/row_1_bg"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="100dp"
>
<LinearLayout
android:id="#+id/row_1_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#808080"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="#+id/row_1_pantone"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="#+id/row_1_rgb"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:id="#+id/row_1_hex"
android:paddingLeft="6dip"
android:textColor="#f0f0f0"
android:textStyle="bold"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="5px"
android:paddingTop="5px"
android:paddingLeft="5px"
android:gravity="bottom"
android:orientation="vertical"
android:background="#00808080">
<barry.pantone.TransparentPanel
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/transparent_panel"
android:paddingRight="5px"
android:paddingTop="5px"
android:paddingLeft="5px"
android:paddingBottom="5px"
android:background="#00808080">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/button_click_me"
android:text="Click Me!"
/>
</barry.pantone.TransparentPanel>
</LinearLayout>
</LinearLayout>
My TransparentPanel.java is straight from the tutorial, using:
innerPaint.setARGB(225, 75, 75, 75);
to make a transparent panel, and this is my onCreate from the main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
sPantone = res.getStringArray(R.array.pantone); // gets colour definitions
iHeight = getWindowManager().getDefaultDisplay().getHeight();
PopulateTable (); // draws colours in the various layout elements
}
The problem is that whatever I do inside the view is fine - I can draw a semi transparent panel over other elements in that view, but the overlay view itself has a black background which obscures the layout beneath. If anyone can help me understand why transparency doesn't work, I will be eternally grateful.
Many thanks
Baz
EDIT
Following another idea I tried moving the definition of the overlay out into its own XML file and using a LayoutInflater to inflate and add the new view. Nope. Exactly the same - the additional view has a solid background. Here's the tranny.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#80000040"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:paddingLeft="5dip"
android:gravity="bottom"
android:orientation="vertical"
>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/button_click_me"
android:text="Click Me!"
/>
</LinearLayout>
and here's the code from onCreate to inflate the new view and add it to the main layout:
private View view;
ViewGroup parent = (ViewGroup) findViewById(R.id.everything);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.tranny, parent, false);
parent.addView(view);
Basically I've gone about creating exactly the same result using a different technique. Handy to learn about LayoutInflater for the first time, but no help re transparency :(
OK, no answers so I'm assuming there's no way to use parent.addView(view) to add an overlaid view which has a transparent background. I had hoped to add a narrow transparent view over the right hand edge of my layout (a scrollbar) into which I could draw a bitmap indicating the position in the big list of displayed items.
Instead I have now made my whole layout slightly narrower and left room for a narrow LinearLayout down the right edge, which has a black background, and into that I draw my bitmap, so I get this:
I would have preferred to put the pointer over the top of the blocks of colour, but unless some guru in the future finds this question and shows us how to do it, I'm close enough.
Baz.
I can draw a semi transparent panel over other elements in that view
Exactly this. You need the "transparency" to be overlaying what's behind it.
The transparency is likely working, but the parent layout's background is transparent (#00ffffff), so the background of the overlay looks black.
Change your root layout's background and you'll likely see that the transparency is working.
If you want it overlay the other elements of your view, you need the transparency as a child to that section of the layout. The way it's currently laid out is like:
<App>
<Content>
</Content>
<Overlay>
</Overlay>
</App>
Whereas you need:
<App>
<Content>
<Overlay>
</Overlay>
</Content>
</App>
If you look at the source for that tutorial you're following, you'll see the custom transparent panel is a child of the MapView, not the root application view. This is why it's able to overlay the MapView content.

Categories