Android: Set windowIsTranslucent from Activity - android

I'm developing a library where I have to set the window background programmatically and I can't use custom style XMLs. The idea is that the user who implements the library can set any theme he want - I just need to make the background transparent. So all the styles for the widgets will stay the same.
I tried some window flags like WindowManager.LayoutParams.FLAG_DIM_BEHIND and WindowManager.LayoutParams.FLAG_BLUR_BEHIND, but none of them was working.
Every solution that I've found is based on the style xml file.
Is there a way to set windowIsTranslucent directly from the code?
Thanks in advance, Roman

No, this is not possible.
It is only possible to set windowIsTranslucent in your theme.
Suggestion; Create your own theme that others can override.

android:windowIsTranslucent, android:windowIsFloating, and android:windowNoDisplay are all read exactly once, before your activity has even started. So by the time you're running your own code, it's too late.
http://osxr.org:8080/android/source/frameworks/base/services/java/com/android/server/am/ActivityRecord.java#0399

I used this
#Override
protected void onCreate(Bundle savedInstanceState) {
processSetTheme(this);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawableResource(R.color.realTranslucent);
if (Build.VERSION.SDK_INT>=19){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
super.onCreate(savedInstanceState);
<color name="realTranslucent">#00000000</color>

Here is trick:
First you use Translucent activity.
then you can set background of activity's layout file top layout.

Related

how i change background for all activity android

I put an image for all activity in my application (in style.xml ...). how I can change the background for all my activity dynamically. is it possible to change the properties of a theme dynamically?
thank you for your help
You can change window's background in your activity using
getWindow().setBackgroundDrawable(...);
getWindow().setBackgroundDrawableResource(...);
See Window docs.
Hi You can set the Background on each Activity XML Layout..
android:background="#drawable/bg"

Activity exit animations don't work as expected on Android 4.0

I have a theme that changes the activity's open/close/enter/exit animations:
<style name="down_up_theme" parent="Theme.rtlfr">
<item name="android:windowAnimationStyle">#style/down_up_animation</item>
</style>
<style name="down_up_animation" parent="#android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">#anim/slide_in_top</item>
<item name="android:activityOpenExitAnimation">#anim/hold</item>
<item name="android:activityCloseEnterAnimation">#anim/hold</item>
<item name="android:activityCloseExitAnimation">#anim/slide_out_bottom</item>
</style>
And in the manifest:
<activity android:name=".activity.ArticlesActivity"
android:theme="#style/down_up_theme" />
The goal is to make the activity content slide down on start, and slide up at exit.
The animations work fine on 2.3. On 4.0, though, the exit animation (slide up) doesn't work. What it does animate is the closing of the activities that are spawned from this activity. In my case, I want to animate the closing of the activity with the list of articles, instead the closing of the article detail has the slide up animation.
I guess I could try to add the closing animation to the activity that spawns the one I want to animate, but it actually spawns activities that should have different animations. I also couldn't find any information on this 2.3 vs. 4.0 difference in the documentation.
How can I make my animations work on 4.0?
I'm not sure why the exit animation set in the theme is not working on ICS+, but calling overridePendingTransition() seems to be working. The simplest way to do this for you is probably to override finish() in your Activity:
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.hold, R.anim.slide_out_bottom);
}
I'd like to add just a little extra to this answer; the override animation solution works fine, but you probably don't want to hard-code the animations. It would be nice to get them from the manifest as you would for other versions of the platform.. so....
add a couple of member fields to your activity to hold the ids of the animations attached to your activity..
protected int activityCloseEnterAnimation;
protected int activityCloseExitAnimation;
and somewhere in your onCreate...
// Retrieve the animations set in the theme applied to this activity in the
// manifest..
TypedArray activityStyle = getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowAnimationStyle});
int windowAnimationStyleResId = activityStyle.getResourceId(0, 0);
activityStyle.recycle();
// Now retrieve the resource ids of the actual animations used in the animation style pointed to by
// the window animation resource id.
activityStyle = getTheme().obtainStyledAttributes(windowAnimationStyleResId, new int[] {android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation});
activityCloseEnterAnimation = activityStyle.getResourceId(0, 0);
activityCloseExitAnimation = activityStyle.getResourceId(1, 0);
activityStyle.recycle();
then wherever your activity finishes/should apply animation include...
overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);
and your activities should correctly honour the animations you set in the theme/style attached to activities in your manifest.
I was confused by this problem, too. But fortunately soon later I found what the answer was. You should check your animation file whether its root element is "Set" or not. If it's not, you should wrap it with "Set" element and then ActivityCloseAnimation attribute would work fine.
I have tried it. Hope it could help you.
you shoud look here use overridePendingTransition and windowEnterAnimation/windowExitAnimation shoud work for you

REMOVE upper Border layout

I have develop app in which I set layout 480 * 320, it is starting from top. But my problem is there is one in build border are display when we take a new project and it is display in all activity. So I want to disable it.
Border like
So can you tell how can I remove it or hide it?
I think what you are trying to do is hide the TitleBar of the application. You can do that by changing the application theme.
Add android:theme="#android:style/Theme.Light.NoTitleBar" in the <application> tag of your manifest file.
Any theme with .NoTitleBar should do the trick.
use any one.
Manifest:
android:theme="#android:style/Theme.NoTitleBar"
Or
java:
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestFeature(FEATURE_NO_TITLE);
in you activity's onCreate before setting the layout should remove it.
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

Is there any way to change android:windowSoftInputMode value from java class?

I want to act my tabs to have different windowSoftInputMode properties for each tab. How to access this property from java class when all handling of your tab is done from one single activity?
Is there any way to access this manifest property from java code?
Use the following to change the softInputMode for an Activity.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Use the following to change the softInput type for an EditText.
mEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
Thanks to #Eliezer for correction
According to Prasham's comment, I did this and it saved my life, thanks to him! The EditText and SoftWindowInput mode are quite buggy when you have a layout with ScrollView and you are filling it dynamically.
Since I had gone through this post but had continued to read other answers/comments (like Prashan's one), I decided to write it in a new post.
Below the code I used with my ScrollView:
Activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I aim to use two different modes for tabs. The modes are SOFT_INPUT_ADJUST_RESIZE and SOFT_INPUT_ADJUST_NOTHING.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
The code line above simply doesn't work by itself. App always behaves in a state ADJUST_NOTHING. However, if windowSoftInputMode="adjustResize" is inserted into <activity> tag in AndroidManifest.xml file, app window is resized as default. Additionally, when you call following line
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
it will behave as expected and nothing is resized.
P.S. improvement to the answer
In Xamarin Android You Can Do programatically Like This
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.DetailDesign);
Window.SetSoftInputMode(SoftInput.AdjustPan);
}
You can use the following code programmatically
android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Thanks
Deepak

Apply the Android "Dialog" theme and sizing it properly

I have an activity that I would like to occur in a dialog. Is there anyway to do this from code, instead of in the manifest? I tried to do this, but it seemed to have no effect.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Dialog);
}
Also, the activity contains a webview and when it starts out as a dialog it's got a small amount of content and the dialog is only like 100px tall. When content fills in it scrolls inside a tiny 100px tall window in the dialog. How do I make the dialog take up more vertical space?
You can easily accomplish this via XML. Just use an XML named 'themes.xml', and place it in the values folder.
Here's a basic example, which implements a custom background:
<resources>
<style name="my_theme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/custom_background</item>
</style>
</resources>
You'll also need to add the following line to the desired activity section of the manifest:
android:theme="#style/my_theme"
PS: I realize this is an old thread, but hopefully it helps someone nonetheless :)
I'm not aware of a way to set the dialog theme from code.
The dialog is basically as big as it needs to be to contain the content, so if you want it to be bigger you need to make some component in your view larger. Perhaps you can set the hieght of the webview to something larger. Note, use dpi, not px!
that's the solution, you can apply a theme via code, thanks to this guy :)
wasn't aware of this constructor myself
https://stackoverflow.com/a/1975508/371749

Categories

Resources