How to make a transparent layout? - java

I have following code for main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout"
android:gravity="center"
android:background="#00000000"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FFFFFF" />
</LinearLayout>
I need that the LinearLayout will be transparent. I thought that I can do it using the alpha setting in a background attibute, but it doesn't work. Please, tell me - I need that my application will be empty and has a white rectangle on the center of a screen.

You want the activity to be transparent?
Try using this theme in your activity tag (in the manifest file): #android:style/Theme.Translucent
For other possible ways, ie child theme see the following answers:
Activity should be transparent, but has black background
How do I create a transparent Activity on Android?
how to make activities transparent

Use below line in the xml file:
<Linearlayout android:background="#android:color/transparent"/>

Related

Center ProgressBar inside RecyclerView that is inside ConstraintLayout

I have a ProgressBar that is an item of the RecyclerView and was added there with the adapter following this tutorial Android RecyclerView dynamically load more items when scroll to end with bottom ProgressBar.
But instead of using a RelativeLayout in the activity_main.xml file, I'm using the default ConstraintLayout. The problem for me is that the ProgressBar isn't centered.
Please find below the layouts I've described above:
The progress bar item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.bookquotes.quotes.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
Below is a screenshot of the ProgressBar that appears on the left (I need it to be centered):
I've taken a closer look at your issue and it is not clear at all what is going on. I have taken the sample project and applied your changes to the layouts and it works just fine. (I did remove a tools reference and changed a couple of ids.)
Here is the GitHub project with the changes just to the two layouts above. Here is a video of this modified app that shows the progress bar in the center as expected.
I was thinking that this was a constraint issue but, as you can see from the demo, that is not the case.
It is also possible that this may be related to the version of ConstraintLayout you are running or some other configuration thing, so you may want to check that out. Otherwise, it looks like something else is going on that has not been revealed. Are you doing any other manipulation of the layout widgets?
It would be useful to know what in your layout is not lining up. Give the LinearLayout a background color to see if it stretching across the screen or not. That will tell you if the progress bar is misaligned or whether the LinearLayout is pinched.
One other possibility: Are you failing to name the parent in the inflation of the LinearLayout? Say something like this:
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, null, false);
instead of this
View view = LayoutInflater.from(activity).inflate(R.layout.item_loading, parent, false);
That small difference will shove your progress bar to the side.
You need to add gravity to your parent LinearLayout which sets the child's gravity to center.I have corrected the progressbar layout.
Hope this could help.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Are you trying to center in middle of screen ?
Or just inside the linear layout
Try gravity instead of layout gravity which will try to center inside the linear layout instead of constraint layout. Since you already have match parent width on it . Also post code to show when you are adding the progress bar to the view .

Use multiple ImageViews as background

I'm currently working on an app for 2 'players', the layout contains the same controls on top and (rotated) bottom.
I use an image as background to display it for both players:
Blue represents player one, red represents player two. Grey is for settings.
I used this until now, but I want the players to be able to change their background color.
My attempt was to put imageViews in the background, containing the selected color. But how can I put them in the background so my other views won't be affected? And how can I add multiple of them? I thought I could add two ImageViews to my RelativeLayout, the first one taking up 45% of parent space from top, and the second one from the bottom. The background itself would be grey for the center (for example).
How can I achieve that?
How about this?
<?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">
<LinearLayout
android:id="#+id/layout_center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:background="#android:color/darker_gray">
</LinearLayout>
<LinearLayout
android:id="#+id/layout_top"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout_center"
android:background="#android:color/holo_red_light">
</LinearLayout>
<LinearLayout
android:id="#+id/layout_bottom"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layout_center"
android:background="#android:color/holo_blue_light">
</LinearLayout>
</RelativeLayout>

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 splash screen not appearing correctly

When the app is loading, instead of a proper splash screen it just shows, the best way to describe it, when you go to install an app from the Play store, the black screen with the icon in the top left corner and some text, that's all it shows when launching my app...
I narrowed it down to activity_game.xml as when looking in the Graphical Layout of that file, it just shows a boring black screen with text.
How would I change this to incorporate a full screen image on splash instead?
Let's say for example, the image is called 'loading.png'
Content of activity_game.xml is below;
<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=".GameActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
--- Edit -
Thanks Rod, works to the point I now get a black screen on launch, I copied loading.png into the drawable folder and use the code like this;
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".GameActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/loading"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</RelativeLayout>
Not sure why it is not loading the image?
First you need to make your activity full screen by setting the theme to no title bar into your activity tag of the manifest and add this:
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Dont add padding to the RelativeLayout to avoid the black screen border, instead of TextView use the ImageView to create an image to the layout.
sample:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".GameActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="your_drawable_image"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</RelativeLayout>

Android ImageView scale type centerCrop

I'm creating an activity on Android. I want to show a background image in it. I put a ImageView into my main RelativeLayout.
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="#drawable/bg4"
android:scaleType="centerCrop" />
</RelativeLayout>
It makes view like this.
I need to make view like this
How can I do it?
Try set ImageView attribute ScaleType=fitEnd ?
use this code.it's give the out-put as you want.
Code
<?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" >
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="150dp"
android:background="#android:color/background_dark"
android:layout_height="150dp" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
one advice if u give the imageview width fill parent then it's contain whole space according to main container.
Best Luck
Set the android:background="#drawable/bg4" in the RelativeLayout like that:
The whole XML should look like that:
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#drawable/bg4
android:orientation="vertical" >
</RelativeLayout>
First, I believe there are a couple of things wrong in the xml:
RelativeLayout does not make use of an orientation, you should remove that
Is this your full xml file? If so then the alignParentLeft in the RelativeLayout doesn't do anything because that is an attribute you use if it's parent is a RelativeLayout.
fill_parent is the deprecated name of match_parent, you should use that.
I also believe that code you have would center the image on the screen putting anything that is to big evenly on both sides and not at all what you said it does.
If you just want a background you can just use android:background="#drawable/bg4" in the RelativeLayout
EDIT: Changing the layoutparams of the ImageView to wrap_content both height and width and removing the scaleType alltogether might make it like you wanted it to be.

Categories