Activity with transparent background - android

what I want to achieve is activity with dialog-like transparency with 100% visibility of RelativeLayout content. This is activity's xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="50dip">
(...)
</RelativeLayout>
</LinearLayout>
and this is manifest:
<activity
android:name="com.acentic.rcontrol.activities.MyActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
</activity>
Right now background is still visible, what am I doing wrong?
--- EDIT:
I added
android:background="#c0000000"
to LinearLayout. Now background is transparent as I wanted to, but also TextViews inside RelativeLayout are also transparent.. how to change it?

Try to set
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
in your activity.
You can also try to do this in a style:
<style name="AppTheme" parent="#android:style/Theme.Translucent.NoTitleBar">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>

Create Style
<style name="MyTransparentTheme" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
this is manifest:
<activity
android:name="your package.activity"
android:theme="#style/MyTransparentTheme">
</activity>

I added android:background="#c0000000" to LinearLayout. Now background
is transparent as I wanted to, but also TextViews inside
RelativeLayout are also transparent.. how to change it?
Add a solid background to your RelativeLayout element.
This way the RelativeLayout will have a solid background, and only the margins will be transparent.

If your activity needs to extends from AppCompatActivity, you can create a custom style for your activity
<style name="transparentActivity" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
And then change the default style of your activity in the Manifest
<activity android:name=".MyActivity"
android:theme="#style/transparentActivity"/>
Otherwise, if you do not need your activity to extend from AppCompatActivity, just change the theme of your activity in the manifest in this way.
<activity android:name=".MyActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar"/>

After setting LinearLayout transparency try to set it's child transparency to the value that you want.
This should make them have different transparency than LinearLayout. Try it.

Related

Layout Height AppCompat Not Full Screen

What's wrong with AppCompat?
My application does not use AppCompat and there are no problems with XML Layout
But after I use AppCompat why is the XML Layout not full on the screen?
I tried by creating a new XML layout, but the results were the same.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"/>
</LinearLayout>
Add this theme to your activity
in style.xml
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
and define this style to your activity in AndroidManifest :
<activity
android:name=".YourActivity"
android:theme="#style/AppTheme"/>
Using this will remove the extra space coming for toolbar by default.
You can achieve fullscreen mode when you use AppCompact like this
Add this to your style file
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen"
parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
And in your manifest file add this to your Activity tag
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen"

Android dialog activity remove margin and finish when touched outside

I have a dialog themed activity declared like this:
AndroidManifest:
<activity android:name=".DialogActivity" android:theme="#style/mydialog"></activity>
Style:
<style name="mydialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
OnCreate of DialogActivity:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.95);
int height = (int)(getResources().getDisplayMetrics().heightPixels*0.80);
getWindow().setLayout(width, height);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
The problem is the activity does not close when I touch inside the red rectangular area shown in this image:
So my question is how to remove this extra space, so that the activity will finish when touched just outside its actual shape?
The activity finishes fine if I touch outside the red rectangle. Already tried this, couldn't remove the extra space.
I was searching for the same and end up with this solution:
Style.xml:
<style name="CustomTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:statusBarColor">#color/black</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowBackground">#null</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Manifest.xml:
<activity android:name=".CustomActivity"
android:theme="#style/CustomTheme" // this is important
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
activity_custom.xml:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#FFFFFF"
android:theme="#style/CustomTheme" // this is important
android:excludeFromRecents="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CustomActivity.kt:
class CustomActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_custom)
// this is important
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
The only thing I didn't find the solution of how to close the activity by the click outside.
Will update the answer if so.
This worked for me
<style name="DialogThemeActivity" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="windowNoTitle">true</item><!-- if required you can remove title also -->
<item name="android:windowCloseOnTouchOutside">false</item>
</style>

Hide ActionBar title just for one activity

If I apply theme for whole application it hides ActionBar title successfully:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
assign in manifest:
<application
android:name="myapp"
android:allowBackup="true"
android:icon="#drawable/action_bar_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
I need ActionBar title hidden just in one activity.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
...other things...
</style>
<style name="MainActivityTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions">showHome|useLogo</item>
</style>
</resources>
setting this theme explicitly for one activity:
<LinearLayout 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=".MainActivity"
android:id="#+id/mainLayout"
android:orientation="vertical"
android:theme="#style/MainActivityTheme">
and it still shows title in MainActivity ActionBar. I know how to hide the title in java, I need to know what I do wrong here in XML.
Set the theme on your activity in the manifest.
<activity
android:name=".MyActivity"
android:theme="#style/MainActivityTheme" />
You probably want to create a sub-theme which hides the action bar:
<style name="AppTheme.NoActionBar">
<item name="android:windowActionBar">false</item>
</style>
and then apply it to your activity in the manifest like so:
<activity android:name="NonActionBarActivity"
android:theme="#style/AppTheme.NoActionBar" />
Using the dot notation (AppTheme.NoActionBar) makes this NoActionBar theme inherit everything from AppTheme and override the setting on android:windowActionBar. If you prefer you can explicitly note the parent:
<style name="AppThemeWithoutActionBar" parent="AppTheme">
<item name="android:windowActionBar">false</item>
</style>
Also, if you're using the AppCompat libraries, you probably want to add a namespace-less version to the sub-theme.
<item name="windowActionBar">false</item>
If you want to remove actionbar title then just remove label attribute from <activity> tag in your manifest file.
Such as,
<activity
android:name=".YourActivityName"
android:label="#string/title" />
And after removing,
<activity
android:name=".YourActivityName" />
Done!

How to create an app that has floating views on to of others

How to create an app that has a floating view all over the others, and always stays at the top. Is it an open activity, If so how to crate a such view?
similat to how its done in the following app:
https://play.google.com/store/apps/details?id=genius.mohammad.floating.stickies&hl=en
You need a transparent activity.
Create a style.xml inside values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
in manifest:
<activity android:name=".SampleActivity" android:theme="#style/Theme.Transparent">
...
</activity>
Use a FrameLayout to let those views float and overlap.
To change views on top. use bringToFront() method for view.
Those views can be LinearLayout that contains ImageView, TextViews and may also contain FrameLayouts. you can also get use of the background property for those controls.

Remove default drop shadow at top of activity layout?

I have an activity. It does not have a title bar. The content view is just a linear layout. It looks like android draws a drop shadow at the top of my content view, directly below the status bar. Is there a way to stop that from being drawn?
My layout is just:
<LinearLayout ... />
I have no titlebar:
requestWindowFeature(Window.FEATURE_NO_TITLE);
Thanks.
In res/values/styles.xml:
<style name="Theme.Custom" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowContentOverlay">#null</item>
</style>
Then in your manifest:
<activity android:name=".YourActivity"
android:theme="#style/Theme.Custom" >
...
</activity>
In res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Custom" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>

Categories

Resources