Android dialog activity remove margin and finish when touched outside - android

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>

Related

Full Screen Application Not Working Android

I am creating an application where it needs to be in full screen all the time so that the status bar is not shown and the navigation back bar should show.
Here is what I am doing currently:
AndroidManifest.xml
<application
android:name="ApplicationClass"
android:allowBackup="true"
android:configChanges="locale|orientation|layoutDirection"
android:fullBackupContent="#xml/backup_descriptor"
android:hardwareAccelerated="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:resizeableActivity="false"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="UnusedAttribute">
......
<activity
android:name=".HomePageActivity"
android:hardwareAccelerated="true"
android:screenOrientation="portrait">
<activity
android:name=".SplashScreen"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|layoutDirection"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:theme="#style/Theme.Transparent">
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:layoutDirection">ltr</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:fontFamily">#font/roboto</item>
<item name="fontFamily">#font/roboto</item>
</style>
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:layoutDirection">ltr</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#color/white</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
Splash Screen Activity Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
overridePendingTransition(R.anim.noanim, R.anim.activity_fade_out);
HomePage Activity Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
overridePendingTransition(R.anim.noanim, R.anim.activity_fade_out);
activity_home_page.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/cl_home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bb_bottom_bar"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
The MAIN Issue:
Please help what am I doing wrong, I almost tried everything I could but couldn't get the solution. I just need the status bar to go away and the navigation bar to remain there it is with the bottom bar tab above my navigation bar.
Please help!!

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"

How to delete the default bar in android studio?

I am currently developing my first app in android studio and i am facing a problem that i tried to fix for a while now.
I am trying to remove the first action bar.
I tried changing the theme to a .noActionBar one but to result
Please check out the image for a visual explanation
this is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.user.survay_1.SecondActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/toolbar2"
android:background="#00a4f9"
android:visibility="visible">
</android.support.v7.widget.RecyclerView>
change your theme NoActionBar at styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
also add
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
write below code in Style.xml:
<style name="LoginTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
Now u call this theme in manifest.xml with adding this line android:theme="#style/LoginTheme"
See manifest.xml look like :
<activity
android:name=".MainActivity"
android:theme="#style/LoginTheme"
android:windowSoftInputMode="stateHidden" />
getSupportActionBar().hide();
In your Activity just use this :- only one line
You can do it in two ways.
First way is to disable it in styles.xml file:
styles.xml
<style name="CustomTheme.NoActionBar" parent="CustomTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And in your AndroidManifest.xml you can use it for all activities:
<application android:theme="#style/CustomTheme.NoActionBar">
Or for one activity only:
<activity android:theme="#style/CustomTheme.NoActionBar">
The second way is to hide action bar from your activity:
Activity.class
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
declare custom theme in your style.xml file like this..
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
</style>
then in your manifest give your activity theme like this..
<activity android:name=".ActivityName"
android:theme="#style/AppThemeNoActionBar">
</activity>

colorPrimaryDark is not applying for particular activity in Status Bar

For information, I am using Android Studio 2.0 Preview 7, API 23.
Activity that is having the issue
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="in.ezyride.blovia.OfferRide"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:id="#+id/view">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view">
<include
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/basicLayoutAddl"
layout="#layout/content_offer_ride"
/>
</ScrollView>
</RelativeLayout>
But other activities are regular. This happened when I was trying to fix the keyboard covering EditText issue. However I almost solved that, new one raised.
For fixing it, I added scroll view in the above relative layout.
The manifest part of the activity is as follows
<activity
android:name=".OfferRide"
android:label="#string/title_activity_offer_ride"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateAlwaysVisible"
android:theme="#style/AppTheme.NoActionBar" />
Other activity where things are normal.
File: styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="md_positive_color">#color/colorPrimary</item>
<item name="md_neutral_color">#color/colorPrimary</item>
<item name="md_title_color">#color/colorPrimary</item>
<item name="md_negative_color">#color/colorPrimary</item>
<item name="md_widget_color">#color/colorPrimary</item>
</style>
<style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">#null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
In my case, the problem was because I had two files styles.xml in my project, in both of them had the same name of theme for the activities.
Let me describe it.
This is my activity in the AndroidManifest.xml:
This is my first styles.xml (default):
This is my second styles.xml (V21):
The app in running mode used the second style, it had the problem, because we couldn't see the colorPrimaryDark of the app.
The solution was, comment the last item in the second style.xml:
http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html
AppBarLayout is a vertical LinearLayout which implements many of the
features of material designs app bar concept, namely scrolling
gestures.
Children should provide their desired scrolling behavior through
setScrollFlags(int) and the associated layout xml attribute:
app:layout_scrollFlags.
This view depends heavily on being used as a direct child within a
CoordinatorLayout
Use CoordinatorLayout instead of RelativeLayout and instead of ScrollView, Use NestedScrollView.
And change this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Since you already have Toolbar, let's imagine you've already added this in your OnCreate:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
For me, the issue was windowTranslucentStatus:
Before:
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:colorPrimary">#color/bright_purple</item>
<item name="android:colorPrimaryDark">#android:color/background_dark</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
After:
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:colorPrimary">#color/bright_purple</item>
<item name="android:colorPrimaryDark">#android:color/background_dark</item>
</style>
Use this in your styles.xml:
<item name="android:statusBarColor">#color/colorPrimaryDarkBlue</item>

Activity with transparent background

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.

Categories

Resources