Android Activity with transparent background and Action Bar? - android

im trying to build a Popup for my Android App. It should be a Activity which doesn't use the whole screen, just 200dp on the right side of the screen and the rest should be transparent black.
A Picture of what it looks like / what i want to have: http://i.stack.imgur.com/6APy6.png
Java:
public class ActivitySettings extends BaseActivity implements View.OnTouchListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setFinishOnTouchOutside(true);
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
System.out.println("Klicked " + motionEvent.getAction());
// If we've received a touch notification that the user has touched
// outside the app, finish the activity.
if (MotionEvent.ACTION_OUTSIDE == motionEvent.getAction()) {
finish();
return true;
}
return false;
}
}
AndroidManifest:
<activity
android:name="com.cleverlize.substore.test.ActivitySettings"
android:label="#string/title_activity_settings"
android:theme="#style/Theme.Transparent">
</activity>
Style XML:
<style name="Theme.Transparent" parent="Theme.AppCompat.Light">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/itemPremium</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
So i just need the adjustment and the ActionBar... please Help :)

You can have acitivity without titlebar and actionbar in manifest with the theme as
<activity
android:name="com.demo.YourActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
and for layout
for transperancy your base layout background will be android:background="#android:color/transparent"
your container layout will be
design as per your requirement with android:layout_alignParentRight="true"

Go to Android manifest file-> search for the required activity-> include this:
<--android:theme="#android:style/Theme.Translucent"-->
in the activity tag like this:
<activity
android:name=".Services"
android:label="#string/title_activity_services"
android:theme="#android:style/Theme.Translucent" >
</activity>

Suppose your Activity layout is inside the LinearLayout, set the inner layout margin from left 200sp:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="200sp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Text Display"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>

Related

App won't run in landscape

Today I started trying to optimize my application for landscape mode. But I just cannot get my application to run in landscape mode, there are no settings that I have enabled to force portrait.
I am showing my placeholder screen as an example.
I also double checked that auto rotate in my AVD is enabled. I have this problem on every AVD.
Declared like this in my manifest.
<activity
android:name=".PlaceholderActivity"
android:theme="#style/AppTheme.NoActionBar" />
My layout looks like 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"
android:background="#color/background_material_light"
android:orientation="vertical"
android:animateLayoutChanges="true" >
<View
android:id="#+id/icon"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_centerInParent="true"
android:background="#drawable/feedyr_full" />
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<include
layout="#layout/helper_error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And my Theme looks like this.
<style name="AppTheme.NoActionBar" 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:colorButtonNormal">#color/colorAccent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
There is also nothing is my class that's forcing portrait
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_placeholder);
config = FirebaseRemoteConfig.getInstance();
model = Model.getInstance();
....
What am I missing?
Try setting the activity orientation to sensor :
<activity
android:name=".PlaceholderActivity"
android:theme="#style/AppTheme.NoActionBar"
android:orientation="sensor"/>
in your AndroidManifest
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
and in MainActivity.java
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation== Configuration.ORIENTATION_PORTRAIT)
{
}
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
}
}
hope it`s help
Don't ask me why, but suddenly the landscape mode works. I pressed the screen rotation buttons on the right menu. When it went back in landscape it suddenly worked. Did this before so don't know why it suddenly started to work.
I did not change anything in my code, theme, manifest or layout...
Try to enable the emulator screen rotation mode to work.

Scene transition image drawn in tiles during transition

I'm facing weird drawing issue with scene transitions (running with Nexus 5x 6.0.1 platform)
I found out that <item name="android:windowBackground">#null</item> has some relation to the issue.
Anyone know why this happens and what can be possible places to check in order to fix this?
Here is my test project setup to reproduce the problem:
minSdkVersion 16 and targetSdkVersion 23 using:
com.android.support:appcompat-v7:23.3.0
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".NextActivity"/>
</application>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#null</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View iv = findViewById(R.id.iv_test);
ViewCompat.setTransitionName(iv, "test");
findViewById(R.id.b_go).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this,
android.support.v4.util.Pair.create(iv, "test")).toBundle();
startActivity(new Intent(MainActivity.this, NextActivity.class), bundle);
}
});
}
}
NextActivity.java
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
final View iv = findViewById(R.id.iv_test);
ViewCompat.setTransitionName(iv, "test");
}
}
activity_main.xml
<?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"
android:background="#android:color/white"
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.test.scenetransitions.MainActivity">
<ImageView
android:id="#+id/iv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_dialog_email"
android:tint="#color/colorAccent"/>
<Button
android:id="#+id/b_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#color/colorAccent"
android:padding="20dp"
android:text="GO"/>
</RelativeLayout>
activity_next.xml
<?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"
android:background="#android:color/white"
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.test.scenetransitions.MainActivity">
<ImageView
android:id="#+id/iv_test"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="#android:drawable/ic_dialog_email"
android:tint="#color/colorAccent"/>
</RelativeLayout>
And here is screenshot took in proper time during the transition:
The issue is with <item name="android:windowBackground">#null</item> you should never ever use null for window background. Replace it with <item name="android:windowBackground">#android:color/transparent</item>
When you set it to null Android doesn't know what to draw and it could draw any garbage from GPU. So you should explicitly specify transparent background.
I copy pasted your code, but i didn't get any drawing issues. I mean when the second page opens up, the image is a bit blurry(pixelated) that's cause of the small size of the actual image(if that's the case just use a bigger image in the second activity). May be its your device. I tested this in Moto E2. For me the transition is smooth, no drawing issues what so ever.

how to delete the toolbar in android design

The red frame should not have in my code, but after I updated my Android Studio, it is showed in all of my APPS. please click and see the picture.
How to delete the red frame from my code? And it could not find in its design/text. The blue frame's begin code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/activity_login_user_email_edt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="35dp"
android:autoText="true"
android:hint="e-mail" />
The Answer Skynet posted is correct,but this is the programatical way
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Full Screen and remove toolbar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
That is the theme your activity is extending from. You change it in the styles.xml. Then you need to update this setting in the Manifest.
You need to add this to styles.xml:
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in your AndroidManifest.xml for this Activity:
<activity
android:name=".YourActivity"
android:label="#string/title_activity_main"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
</activity>
The above will work if you are extending from AppCompatActivity which should look like:
public class YourActivity extends AppCompatActivity{}

activity as dialog in android

I want to use an activity as dialog and i made the theme of the activity as dialog & i succeed.
but
i have the problem here is when i click outside of the activity
its automatically get closed & the previous activity get started..
i tried a thing to make transparent parent layout it does not look like a dialog..
i want to use this activity to create new account in my application as it has only 3 fields so in tablet it looks large space unused... so i want to use activity as dialog.....
thenx in advance...!!!
examples will be appreciated..!!!!!
try with following property
this.setFinishOnTouchOutside(false);
Make change in code as per your need.
Thanks
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:orientation="vertical"
android:paddingBottom="20dp" >
<RelativeLayout
android:id="#+id/RlayMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="30dp"
android:layout_marginTop="120dp"
android:background="#FFFFFF"
android:padding="10dp" >
<TextView
android:id="#+id/txtsignin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="SIGN IN"
android:textColor="#000000"
android:textSize="25sp" />
<EditText
android:id="#+id/edtUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtsignin"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/txtuser"
android:hint="USERNAME" />
<EditText
android:id="#+id/edtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edtUserName"
android:layout_marginTop="10dp"
android:hint="PASSWORD"
android:inputType="textPassword" />
<Button
android:id="#+id/btnSignIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/edtPassword"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Sign In" >
</Button>
<Button
android:id="#+id/btnSignUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnSignIn"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Sign Up For Free!" >
</Button>
</RelativeLayout>
</RelativeLayout>
if you haven't already tried it, then this is the way to achieve activity as dialog:
in your manifest file, add to your activity the following attribute:
<activity
android:name=".MyActivityName"
android:theme="#android:style/Theme.Dialog" />
For the issue of avoiding closing the activity when clicking outside window
from API 11
as mentioned by Vivek use this.setFinishOnTouchOutside(false);
but for prior APIs use this code:
#Override
public boolean onTouchEvent(MotionEvent event) {
if ( event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)){
return true;
}
return super.onTouchEvent(event);
}
private boolean isOutOfBounds(Activity context, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = context.getWindow().getDecorView();
return (x < -slop) || (y < -slop)
|| (x > (decorView.getWidth()+slop))
|| (y > (decorView.getHeight()+slop));
}
to Start activity as dialog I defined:
<activity android:theme="#android:style/Theme.Dialog" />
Now when I startActivity() it displays like a dialog and parent activity displays behind it. I want a Button which when clicked Dialog gets dismissed and parent activity should display without refreshing the page.
Create an activity as we usually create it.
Also check CustomDialogActivity.java on android.com
I think you should create activity as a dialog, it helps.
This way, you can set style and theme for your activity.
Using Same Activity in Mobile devices and Tablets.
Mobile :-
Goto res -> values.
Open styles.xml and add following theme settings.
styles.xml :-
<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>
</style>
Tablet :- Launch activity as dialog.
Goto "res".
Create new folder "values-sw720dp".
Create new styles.xml and add following theme settings.
styles.xml :-
<style name="AppTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/primary_color</item>
<item name="colorPrimaryDark">#color/primary_dark_color</item>
<item name="android:textColorPrimary">#color/white</item>
<item name="colorAccent">#color/material_green_800</item>
<item name="colorButtonNormal">#color/material_green_800</item>
<item name="windowFixedHeightMajor">800dp</item>
<item name="windowFixedHeightMinor">800dp</item>
</style>
Manifest.xml :-
<activity
android:name=".LogInActivity"
android:label="#string/title_activity_log_in"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" //set the theme activity
android:windowSoftInputMode="adjustPan" >
</activity>

Android Activity as a dialog

I have an Activity named whereActity which has child dialogs as well. Now, I want to display this activity as a dialog for another activity.
How can I do that?
To start activity as dialog I defined it like this in AndroidManifest.xml:
<activity android:theme="#android:style/Theme.Dialog" />
Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list
android:excludeFromRecents="true"
If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:
After setContentView() in your Activity use:
this.setFinishOnTouchOutside(false);
Now when I call startActivity() it displays as a dialog, with the previous activity shown when the user presses the back button.
Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use #style/Theme.AppCompat.Dialog instead.
Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:
this.setFinishOnTouchOutside(false);
requires API level 11
You can define this style in values/styles.xml to perform a more former Splash :
<style name="Theme.UserDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowBackground">#drawable/trans</item>
</style>
And use it AndroidManifest.xml:
<activity android:name=".SplashActivity"
android:configChanges="orientation"
android:screenOrientation="sensor"
android:theme="#style/Theme.UserDialog">
If you need Appcompat Version
style.xml
<!-- Base application theme. -->
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
yourmanifest.xml
<activity
android:name=".MyActivity"
android:label="#string/title"
android:theme="#style/AppDialogTheme">
</activity>
1 - You can use the same activity as both dialog and full screen, dynamically:
Call setTheme(android.R.style.Theme_Dialog) before calling setContentView(...) and super.oncreate() in your Activity.
2 - If you don't plan to change the activity theme style you can use
<activity android:theme="#android:style/Theme.Dialog" />
(as mentioned by #faisal khan)
If your activity is being rendered as a dialog, simply add a button to your activity's xml,
<Button
android:id="#+id/close_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
Then attach a click listener in your Activity's Java code. In the listener, simply call finish()
Button close_button = (Button) findViewById(R.id.close_button);
close_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
That should dismiss your dialog, returning you to the calling activity.
If you want to remove activity header & provide a custom view for the dialog add the following to the activity block of you manifest
android:theme="#style/Base.Theme.AppCompat.Dialog"
and design your activity_layout with your desired view
Create activity as dialog, Here is Full Example
AndroidManife.xml
<activity android:name=".appview.settings.view.DialogActivity" android:excludeFromRecents="true" android:theme="#style/Theme.AppCompat.Dialog"/>
DialogActivity.kt
class DialogActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dialog)
this.setFinishOnTouchOutside(true)
btnOk.setOnClickListener {
finish()
}
}
}
activity_dialog.xml
<LinearLayout 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="wrap_content"
android:background="#0072ff"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="#dimen/_300sdp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtTitle"
style="#style/normal16Style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:text="Download"
android:textColorHint="#FFF" />
<View
android:id="#+id/viewDivider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#fff"
android:backgroundTint="#color/white_90"
app:layout_constraintBottom_toBottomOf="#id/txtTitle" />
<TextView
style="#style/normal14Style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:text="Your file is download"
android:textColorHint="#FFF" />
<Button
android:id="#+id/btnOk"
style="#style/normal12Style"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginBottom="20dp"
android:background="#drawable/circle_corner_layout"
android:text="Ok"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
Set the theme in your android manifest file.
<activity android:name=".LoginActivity"
android:theme="#android:style/Theme.Dialog"/>
And set the dialog state on touch to finish.
this.setFinishOnTouchOutside(false);
Some times you can get the Exception which is given below
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
So for resolving you can use simple solution
add theme of you activity in manifest as dialog for appCompact.
android:theme="#style/Theme.AppCompat.Dialog"
It can be helpful for somebody.

Categories

Resources