How to create custom PopupWindow from intent call? - android

I wanna create a custom PopupWindow similar to this :
http://android-er.blogspot.kr/2012/03/example-of-using-popupwindow.html
In this example, PopupWindow is created by a button click event from an activity, but i want to create PopupWindow via intent from another application.
Is it possible? Any comments will be very appreciated!

The idea is to declare a standard Activity to make it appear like a Popup window of sorts.
Use this code (standard boiler plate Intent code to trigger the Activity)
SOME_WIDGET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), THE_POPUP_ACTIVITY.class);
startActivity(myIntent);
}
});
If, for example, you name the popup Activity as Popup, then replace the THE_POPUP_ACTIVITY.class with Popup.class
Now, for this Activity, in the Manifest, declare a theme. For example:
And this is the corresponding style declaration:
<style name="DialogNoTitleBar" parent="android:style/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>
</style>
Also, in the onCreate() of the Popup Activity, you might want to add this statement right after the setContentView(R.layout.THE_LAYOUT_XML); I say might because how you want it to appear may vary from how I program my popup Activity.
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

Your requirement isn't really clear. Intents are usually used when you want to switch to another activity or maybe send the intent to a service of some sorts. If you want to open a dialog for an action, you don't really need intents.

Create an activity with your custom design, while registering the activity in the manifest file just add this android:theme="#android:style/Theme.Dialog"
and call the activity with your intent. Hope you got it. :)

Related

How to show dialog/popup application in kill state?

I want to show dialog/popup when application in kill state and BroadcastReceiver receive any action.
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyDialog.showDialog(context);
}
}
I have added permission in manifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I am getting exception when activity foreground
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
My dialog is not display in activity kill state but receiver is called.
Actually, in this case, or without an activity, you can't launch any kind of dialog.
1. First method
create a dialog activity : How to make dialog activity
2. Second
create popup window: How to make a simple android popup window?
Edit
If the application is not in the foreground, please don't do this. Go for notification

Start a Dummy Activity

I recently started Android coding and wanted to create a little program for changing the screen brightness...
Well.. i know there are already some questions about it, but i tried everything suggested here and really dunno how i can solve my problem :)
I understood that you have to "refresh" the screen after setting brightness. And at this point my problem starts... I've created some kind of dummy activity and also have an intent in my main activity, but it seems like the intent dont sart the dummy activity... Heres the relevant part of my main activity:
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 255);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class); //it is working...
startActivity(in); //it is working...
}
and the dummy code:
public class DummyBrightnessActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.finish();
}
}
the manifest.xml:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Test"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.Test.DummyBrightnessActivity"
android:taskAffinity="com.Test.Dummy"
android:excludeFromRecents="true"
android:theme="#style/EmptyActivity"></activity>
</application>
maybe relevant, the styles.xml:
<resources>
<style name="EmptyActivity" parent="android:Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Toast</item>
<item name="android:background">#00000000</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#000</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
oh, btw... I dont get any errors... The dummy just wont start (I think so, because when i run it without android:excludeFromRecents="true" then it wont appear in the recent apps list.
I hope someone can help me...
Have a nice day
//EDIT: Well... it looks like the code is working properly xD
Just relooked and put some text instead of the "finish()" and the activity run properly... but i thought that the finish have to be there :/
Maybe you have any suggestions how to "reset" the screen instead? Looks like i understood one of the tutorials wrong...
//EDIT2:
Well... I cant post an answer to my own question in the first 8hours:D
So i post it in here:
Thank you all for the help and tips, but now i found the solution for myself :D
this one: Refreshing the display from a widget?
the part "kicking off an empty activity and executing the WindowManager refresh" is working for me.
i came across this before asking here, but back then i just couldnt get it to work :D
So, anyways, thank you very much ;)
This was just an example of hard it can be to code "a little, fast-coded beginner app" ;)
You may also want to consider calling invalidate() on your View instead of launching and closing a new one.
I am not sure if it will work for this scenario, as I have not tried changing the screen brightness. However, as per http://developer.android.com/reference/android/view/View.html#invalidate(), calling invalidate() on a view will cause onDraw() to be called, which sounds like what you want.
I think this should work. I use this when I am moving from one page to another. It simply identifies the class and starts the activity, running whatever your new activity is going to do.
public void onClick(View v) {
// (IN THIS EXAMPLE) IDENTIFIES WHAT IS BEING PRESSED
if(v.getId() == R.id.activity) {
// STARTS THE OTHER ACTIVITY
Intent i = new Intent(this,Activity2.class);
this.startActivity(i);
}
Also, remove finish(); because your activity initializes and closes at the same time.
Make sure that you set the content view (setContentView(R.layout.main);) so you can tell whether or not the activity you started actually brings you to the page you want.
EDIT: You say the button press isn't detected. Try implementing the OnClickListener directly to your activity android import it like this.
public class MyApplication extends Activity implements OnClickListener {
Also, make sure your button is properly initialized. Try removing this: button1.setOnClickListener(new OnClickListener(), and then add this to your code:
button1 = (Button) findViewById(R.id.activity);
button1.setOnClickListener(this);

Unable to add dialog when press my own softkeyboard key

I get the following android exception when I try to open a dialog. when i am press my own SoftKeyboard key how can I fix this problem?
BadTokenException: Unable to add window -- token null is not for an application
com.example.android.softkeyboard.SoftKeyboard.diqalog(SoftKeyboard.java:759)
com.example.android.softkeyboard.SoftKeyboard.onKey(SoftKeyboard.java:526)
android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1252)
First of all, you cannot present a dialog from a remote service, you can only do so from within a running Activity, that's why you're getting a BadTokenException. But there are solutions to this problem:
1) Present an Activity with Theme.Dialog theme:
<activity
android:name="com.srgtuszy.activity"
android:theme="#android:style/Theme.Dialog"
/>
And start the activity as a new task:
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This way, you'll get an activity which will look just like a dialog.
2) Present an empty and transparent Activity and show an AlertDialog from within the activity
Declare and start the activity in manifest just as before, but use a transparent theme:
<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>
In the activity, override the onCreate() method and don's call setContentView() and present the AlertDialog:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = this;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Hello!");
dialog.show();
}
This is a more hacky approach, but in this way you can show a dialog to the user without dismissing the input method, you can use to to present edit options for instance.
If you just want to notify the user about a certain event, consider using Notifications, they won't distract the user and pollute the UI.

How to switch activity without animation in Android?

How can I use properly the Intent flag FLAG_ACTIVITY_NO_ANIMATION in AndroidManifest file? I supose my problem is trivial, but I can't find good example or solution to it.
<intent-filter>
<data android:name="android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION" />
</intent-filter>
However no error is reported by compliator, but data isn't correct.
I just want to disable animation in case switching between activities. I can use getWindow().setWindowAnimations(0); in onCreate or onResume rather but using flag is better way, isn't it?
I can use also in code:
Intent intent = new Intent(v.getContext(), newactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
getContext().startActivity(intent);
But I want to use this flag in Android Manifest. To disable animation also in case returning from second activity to first.
You can create a style,
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
You can also define a style to specify custom entry and exit animations.
http://developer.android.com/reference/android/R.attr.html#windowEnterAnimation
If your context is an activity you can call overridePendingTransition:
Call immediately after one of the flavors of startActivity(Intent) or
finish to specify an explicit transition animation to perform next.
So, programmatically:
this.startActivity(new Intent(v.getContext(), newactivity.class));
this.overridePendingTransition(0, 0);
Try this code,
this.startActivity(new Intent(v.getContext(), newactivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
You can also just do this in all the activities that you dont want to transition from:
#Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
I like this approach because you do not have to mess with the style of your activity.
The line in the theme style works fine, yet that replaces the animation with a white screen. Especially on a slower phone - it is really annoying.
So, if you want an instant transition - you could use this in the theme style:
<item name="android:windowAnimationStyle">#null</item>
<item name="android:windowDisablePreview">true</item>
Here is a one-liner solution that works for as low as minSdkVersion 14 which you should insert in you res/styles.xml:
<item name="android:windowAnimationStyle">#null</item>
like so:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:windowAnimationStyle">#null</item>
</style>
...
</resources>
Cheers!
This is not an example use or an explanation of how to use FLAG_ACTIVITY_NO_ANIMATION, however it does answer how to disable the Activity switching animation, as asked in the question title:
Android, how to disable the 'wipe' effect when starting a new activity?
After starting intent you can use this code :
Intent intent = new Intent(Activity1.this, Activity2.class);
overridePendingTransition(0, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
If used, intent will work with no animations or transitions
create your own style overriding android:Theme
<style name="noAnimationStyle" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
Then use it in manifest like this:
<activity android:name=".MainActivity"
android:theme="#style/noAnimationStyle">
</activity>

Android - How to stop animation between activity changes

I have multiple different Activity in my app and I don't want any transition animation when changing between Activities. Below is the how I'm changing between Activities:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
This works great the first time I start a new Activity. There is no animation, but when I go back to an Activity that is already started it seems like the "Intent.FLAG_ACTIVITY_NO_ANIMATION" is ignored and the default animation happens.
I can't seem to figure out why this is happening.
Have you tried overridePendingTransition()?
You can set FLAG_ACTIVITY_REORDER_TO_FRONT by code and FLAG_ACTIVITY_NO_ANIMATION in manifest as below:
Create noAnimTheme in res/values/styles.xml
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
or
<style name="noAnimTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowAnimationStyle">#null</item>
</style>
and use it in manifest:
<activity android:name="SecondActivity" android:theme="#style/noAnimTheme"/>
I hope it helps
I wa needing this as I had to create activities on clicking the menus.
I did the following :
I added the FLAG_ACTIVITY_NO_ANIMATION flag to the intent. It stopped the animations while creating the activity for the first time.
However the activities in the stack which were called when we click on the same menu again (probably from a different activity), it had the animation.
So I added FLAG_ACTIVITY_NO_HISTORY to clear or rather finish the activity when it starts a new activity. This caused to create a new activity (without animation) when I click on the menu once again.
add this after creating the second intent
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
when you return to the first intent, animation is disabled, worked for me though
If you're using FLAG_ACTIVITY_REORDER_TO_FRONT then you can also override onNewIntent for later startActivity calls. This will just work for bring to front states instead of first call.
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
overridePendingTransition(R.anim.whatever, R.anim.whatever);
}
Sure, you must implement this in target activity.

Categories

Resources