Basically, I have an app with two Activities.
#1 - MainActivity
This has a solid black background and a button.
When the button is pressed TransparentActivity should be presented.
#2 - TransparentActivity
I want this to be transparent (so the phones normal UI can be seen through).
I've tried using the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent);
setContentView(R.layout.activity_trick);
}
But it causes the app to crash with an NullPointerException.
Try1:
Make super.onCreate(savedInstanceState); call after setTheme(android.R.style.Theme_Translucent);. So do as:
setTheme(android.R.style.Theme_Translucent);
super.onCreate(savedInstanceState);
Try 2:
If that doesn't work, I find the following way easiest to make my activity transparent:
<activity android:name=".your.activity.declaration.here"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
Basically add android:theme="#android:style/Theme.Translucent.NoTitleBar" to your activity declaration in manifest. I can see that you are trying to do a similar thing programatically but by specifying it in manifest never crashed for me. If it does, then there might be other reasons.
Hope it helps.
AppCompatActivity hasn't the Theme_Translucent(maybe the Theme_Translucent is null),you should create your own style.
Related
reading the https://developer.android.com/topic/performance/vitals/launch-time about the right way to implement a launch screen they say to create a Launcher style
<activity ...
android:theme="#style/AppTheme.Launcher" />
and they say that the easiest way to transition back to your normal theme is to call setTheme(R.style.AppTheme) before calling super.onCreate() and setContentView():
KOTLIN
JAVA
public class MyMainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.Theme_MyApp);
super.onCreate(savedInstanceState);
// ...
}
}
problem is with delphi, when/where can I call setTheme(R.style.Theme_MyApp); ? from inside the form create it's not work so seam already too late, and even just after Application.Initialize it's seam to not work either :(
In any application the add/edit will be comparatively having lesser inputs. I have seen that the application, esp., calendar, are using clever strategy to show these as simple dialog, so that the user may not notice that there is empty space in the designed form
As shown below
My question is, how to make it happen?
What I'm doing is I extend DialogFragment:
public class AboutFragment extends DialogFragment { ... }
I also have an activity that contains that fragment. And when the dialog/activity needs to be called, this method decides how to display it:
public static void showAbout(Activity parent) {
if (isTablet) {
AboutFragment aboutFragment = AboutFragment.newInstance();
aboutFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme);
DialogUtils.showDialog(parent, aboutFragment);
} else {
Intent aboutIntent = new Intent(parent, AboutActivity.class);
parent.startActivity(aboutIntent);
}
}
How to decide whether it is a tablet, is totally up to you.
This technique is explained in the documentation.
In my opinion the best approach here is to use
<!-- Theme for a window without an action bar that will be displayed either full-screen
on smaller screens (small, normal) or as a dialog on larger screens
(large, xlarge). -->
"android:Theme.Holo.Light.DialogWhenLarge.NoActionBar"
The best/easiest solution I've found is to always use an Activity, and based on screensize (and version), change your Theme parent.
in res/values/themes.xml
<style name="Detail.Theme" parent="#android:style/Theme.Holo.Light" >
...
</style>
and in res/values-large/themes.xml
<style name="Detail.Theme" parent="#android:style/Theme.Holo.Light.DialogWhenLarge" >
...
</style>
use Context.setTheme method to set them programmetically. As the doc says
this should be called before any views are instantiated in the Context
(for example before calling.
So, to switch between themes need to call setTheme before onCreate
public void onCreate(Bundle savedInstanceState) {
// check screen size
setTheme(dialogTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
As #StinePike answered, setting a dialog theme programatically doesn't do any use (to me), as it shows a wierd black screen behind the dialog, rather than a dimmed background (as shown in the question). This is obviously a bug.
Instead of trying to set it programatically, or in style.xml, and pretty much everywhere except for AndroidManifest.xml, I did the reverse, which has worked for me.
(the solution which I took from the marvelous solution of the above issue)
The simplest solution (that works) as follows:
1. Make the activity a dialog by default through AndroidManifest.xml:
e.g., in the AndroidManifest.xml:
<activity
android:name="com.example.MyActivity"
android:label="#string/title_activity_mine"
android:theme="#android:style/Theme.DeviceDefault.Dialog">
...
</activity>
2. On starting the activity, set the theme to default if device is not a tablet.
if (!(isTablet(this)) {
setTheme(defaultTheme);
}
super.onCreate(savedInstanceState);
...
Note:
solution will work with custom styles defined in style.xml.
Ref:
How to detect device is Android phone or Android tablet?
Dialog with transparent background in Android
Issue 4394 - android - setTheme() does not work as expected
PS: final app on tablet and phone is as follows:
Use a DailogFragment and then control how its shown with setShowsDialog()
Here is a pic of android applicationbar i like ( I mean the part where settings is written on, not sure if applicationbar is the right name :) )
http://oi50.tinypic.com/ehwwpc.jpg
Some apps i have downloaded also have the exact same bar so im guessing its a predefined theme but when i make a project (theme.holo) then my applicationbar is just totally black.
So my question is how do i get the same project bar?
Thank you!
Edit:
I found that all apps are opensoruce and i looked out settings apps manifest for android 4.0.4, same as mine.
Here it is: https://android.googlesource.com/platform/packages/apps/Settings/+/android-4.0.4_r2.1/AndroidManifest.xml
Whats strange is that it uses the same theme i do: android:theme="#android:style/Theme.Holo"
... but still the titlebar is different. There must be another attribute somewhere that defines the titlebar? Does anyone have an idea? :)
The title of the activity can be defined in the Manifest file, and it should be android:label property. For example:
<activity
android:name=".MainActivity"
android:label="#string/TitleBarText" />
TitleBarText is the android string resource that contains the name of the title.
If you wish to do it from code, you can just do the following in the onCreate method:
#Override
public void onCreate(Bundle savedInstanceState) {
setTitle("Title Bar Name");
setContentView(R.layout.activity_main);
}
To create a custom title bar, refer to: this and this
I am using ActionBarSherlock (ABS) and would like to add a dialog to my application as one can see in the ABS Demos Sample application provided by the project. The dialog sample look like this:
I created an activity myself. Here is relevant source code:
public class Dialog extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_DarkActionBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
For some reason, Android forces me to add setTheme() although the ABS sample does not do this. If I leave it out, I will run into the following error.
java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
My AndroidManifest.xml has the following settings, which are very similar to the ones from the ABS sample.
<activity
android:name=".activities.Dialog"
android:label="#string/title_activity_dialog"
android:theme="#style/Theme.Sherlock.Dialog" >
</activity>
The following screenshot shows how my dialog activity looks like.
I am using ActionBarSherlock 4.1.0 with maps support, the Android Support library v4.
Question: Can you figure out, why it looks so different?
Dark vs. light user interface
Transparent vs. opaque background
With and without actionbar
Update:
The ABS sample starts the dialog activity as follows:
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
Intent intent = (Intent) map.get("intent");
startActivity(intent);
}
I start the dialog activity as follows:
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(R.string.title_menuItemDialogActivtiy)
.setIcon(R.drawable.ic_action_dialog)
.setIntent(new Intent(this, Dialog.class))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Meanwhile, I saw that this pattern is deprecated. Instead, I could use a DialogFragment. The question that occurs here: How can I integrate the fragment with the action menu item?
Alternative solution:
I decided to use a DialogFragment instead of an Activity as I estimate it to be more "future-safe". I basically followed the very informative tutorial Using DialogFragments (posted June 3, 2012), which I like to recommend as perfect starting point for any interest reader. Further, I like to add related and useful posts:
Validating user input
Softkeyboard vs. separate DONE button
The output you are seeing definitely comes from setting the Theme in Java code (which will override the value set in XML). I just stood up the following sample application (this is literally all there is) and replicated the issue by adding the extra setTheme() call.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Dialog"
android:theme="#style/Theme.Sherlock.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And the Dialog...
public class Dialog extends SherlockActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("This is a dialog!");
setContentView(text);
}
}
Now as to why you got an exception without that extra method, that's another matter (and quite strange). As you can see it should work with as little code as I provided above.
Perhaps make sure that both the library project and your project are being compiled with at least Android 4.0 (API 14) as this is a requirement of the library.
Beyond that, if you just want to show a Dialog in your application, does it need to be a themed Activity? This is not common. You can always create a simple Dialog or AlertDialog subclass to display as well. Take a look here for more information...
Try setting a theme for the application.
<application
android:theme="#style/Theme.Sherlock"
The dialogue should inherit the theme. If you've already set that then remove the android:theme tag in the activity declaration and the setTheme() call and see what happens. The reason you get the error without setTheme is because SherlockActivities must have one of the themes in the error message and you had it set to something else in the manifest setTheme() overrode this before you got into trouble.
I am writing a phone dialer app for android. I created a layout for keypad, which contains a TextView and 10 buttons. Buttons are as keys for 10 digits(0 to 9) and TextView is for displaying the number according to keys pressed.
In my app, i am appending the text ("0" or "1", etc.) to the TextView for each button pressed. If i pressed the buttons 1, 2, 3 then the text on TextView is 123.
The problem is, let's take the screen is in landscape mode and TextView contains 123, if i turn it, in portrait mode no text on TextView.
Please Help Me Regarding this.
What #jeet recommended didn't work for me. I had to add "screenSize". This is the line you should add in your manifest.xml in the <activity> node of your activity:
android:configChanges="keyboardHidden|orientation|screenSize"
Thus, the complete node may look like this:
<activity
android:name=".YourActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#style/AppTheme.NoActionBar">
Please check on orientation change, on create method is called, which requires all the views to be created again, so you need to use one of the following methods:
use onSavedInstance method and save the states of components/views to bundle.
Just use following flag true in your manifest file in activity tag android:configChanges="keyboardHidden|orientation". like below:
<activity android:name=".SampleActivity" android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation">
...
</activity>
The reason for this is due to Android basically destroying the activity and creating it again every time you rotate the device. This is mainly to allow for different layouts based on portrait/landscape mode.
The best way to handle this is to store whatever data you need to keep within the Activity Bundle, by responding to the onSavedInstance event (called just before Android destroys the activity), and then reapplying those in the standard onCreate event.
Although you can add "orientation" to the configChanges property, keep in mind that you're basically telling Android that you're going to be handling everything relating to orientation change yourself - including changing layout, etc.
To preserve a TextView's text, you can simply set the TextView's freezesText property to true.
As in:
<TextView
...
android:freezesText="true"
.../>
This is the accepted answer here:
Restoring state of TextView after screen rotation?
If someone is still having troubles... this did the trick for me
public class BranjeKP extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_branje_kp);
//...
}
#Override
protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("TVNazivPod", TVNazivPodatka.getText().toString());
out.putString("TVEnotaMere", TVEnotaMere.getText().toString());
}
#Override
protected void onRestoreInstanceState(Bundle in) {
super.onRestoreInstanceState(in);
TVNazivPodatka.setText(in.getString("TVNazivPod"));
TVEnotaMere.setText(in.getString("TVEnotaMere"));
}
You basically save any values you want before rotation (that's when onSaveInstanceState is called) into a Bundle and after rotation (onRestoreInstanceState) you just pull all values out from Bundle.
Just to clarify, TVNazivPodatka and TVEnotaMere are TextView widgets.
...with a help from
How to prevent custom views from losing state across screen orientation changes