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
Related
I found a description about the difference of transparent and translucent. These are the important parts:
What does translucent mean? If you were writing about a pane of glass,
but that pane of glass was frosty, darkly colored, or very dirty, you
would use translucent.
What does transparent mean? If you were writing about a pane of glass,
and that piece of glass was perfectly clear, you would use
transparent. Something that is transparent allows all light to pass
through it. Clean air and the windshield of a car are transparent.
If you want to understand this deeply then look here.
I want to make a ViewGroup translucent in Android. In a video I found how to make an Activity translucent. What they do is defining the following styles and colors:
<color name="transparent_green_color">#8800ff00</color>
<style name="TranslucentGreen" parent="android:Theme.Translucent">
<item name="android:windowBackground">#color/transparent_green_color</item>
</style>
And then in the manifest the within the application tag they add this new style as a theme:
<application
android:theme="#style/TranslucentGreen"
...>
...
</application>
What I want to do is NOT to make an Activity translucent but to make a ViewGroup translucent.
My layout is structured like this:
<FrameLayout>
<RelativeLayout>
...
</RelativeLayout>
<RelativeLayout>
...
</RelativeLayout>
</FrameLayout>
The second RelativeLayout shall be translucent and the first one shall be visible a bit through the translucent RelativeLayout. How to do that?
Using the Blurry library solved my problem. So if the second ViewGroup (in my case second RelativeLayout) has to be at the top and the first one has to shine through translucent, then you need to call something like:
Blurry.with(context).radius(25).sampling(2).onto(viewGroup);
and then you have the desired effect. For going back to the old view state without translucency you can just use this method:
Blurry.delete(viewGroup)
on the desired view. I like that library because it's very compact.
If you are not able to use external libraries you should maybe start with this Medium article. It works with RenderScript and Bitmap class. Bu It shall be possible to create Bitmaps from ViewGroups.
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.
I am wanting to create help overlays like the ones you see when ICS loads for the first time or in apps like ES File Explorer or Apex Launcher (there are more, but I can't think of them right now). Is this just a relative layout with one view sitting on top of the other? I haven't been able to find any sample code for doing such a thing. Anyone know how this is done or have any ideas?
Let's assume you ordinarily would call setContentView(R.layout.main), but on first run, you want to have this overlay.
Step #1: Create a FrameLayout in Java code and pass that to setContentView().
Step #2: Use LayoutInflater to inflate R.layout.main into the FrameLayout.
Step #3: Use LayoutInflater to inflate the overlay into the FrameLayout.
Step #4: When the user taps the button (or whatever) to dismiss the overlay, call removeView() to remove the overlay from the FrameLayout.
Since the overlay is a later child of the FrameLayout, it will float over top of the contents of R.layout.main.
"Coach mark" is "Help overlay" in UX talk :-)
coach_mark.xml is your coach mark layout
coach_mark_master_view is the id of the top most view (root) in coach_mark.xml
public void onCoachMark(){
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.coach_mark);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.coach_mark_master_view);
masterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
Adding sample of coach_mark.xml (to this excellent solution given by Oded Breiner), so its easy for ppl to copy & paste to see working example quickly.
Sample of coach_mark.xml here, change the -> drawable/coach_marks to your image:
coach_mark.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/coach_mark_master_view">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/coach_marks_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:src="#drawable/coach_marks" />
</RelativeLayout>
</LinearLayout>
And optionally use this theme to remove padding:
<style name="WalkthroughTheme" parent="Theme.AppCompat">
<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:backgroundDimEnabled">false</item>
</style>
You can do that pretty quickly. You add, for exemple a LinearLayout where you put a picture with alpha which correspond to your help information and what do you want to draw like an overlay. In you xml of your activity you put this layout in a RelativeLayout after the layout of your activity with the Gone visibility. When you want to draw the help information, you just neeed to set this visibility to visible.
I hope, I'm clear, if you have any question,I'm be please to answer them.
See my another answer how programmatically show an overlay layout on top of the current activity. Activity's layout.xml does not need to know anything about the overlay skin. You can put overlay semi-transparent, cover only part of the screen, one or more textview and buttons on it...
How to overlay a button programmically?
create res/layout/paused.xml RelativeLayout template or use any layout toplevel
create a function to show overlay skin
key is to get handle to layout.xml, use LayoutInflater class to parse xml to view object, add overlay view to current layout structure
My example uses a timer to destroy overlay object by completely removing it from the view structure. This is probably what you want as well to get rid of it without a trace.
My goal was that main activities are not aware of any overlay skin, overlays come and go, many different overlays, still able to use overlay1.xml text files as a template, and content should programmatically be updated. I do pretty much what CommonsWare told us my post shows the actual program code to get started.
disclaimer: OPs "Thanks for your input. This is how I pictured it being done. I have to give credit to the answer below" comment does not mean my answer but CommonsWare answer. Stackoverflow have changed post orderings.
HY
I want a TextView to have a maximum textSize of 30dip, but if the screen is to small to display everything, the size should be adjusted. Is there a way to do so?
<style name="cimbaliHeadline" parent="#android:style/TextAppearance.Large">
<item name="android:textSize">30dip</item>
</style>
Thanks
Regards
First things first: ALWAYS define text size with sp, this way you shouldn't even have this issue.
If you have a string that is simply too long to display on a screen, you could either use android:ellipsize="true" or use something along the lines of this:
if(myTextView.height > defaultTextView.height)
myTextView.setTextSize(smallerTextUnit, smallerTextSize);
where defaultTextView is a dummy view that you can set to invisible somewhere in your layout.
Hope this helps.
I have looked and tried a lot of different things but no matter what I end up doing the screen is always blank and I'm sure it's something really dumb I'm doing and I'm hoping someone will catch it.
I'm trying to alternate background colors but before I even get to that I need to get it so that even one background color will display properly.
First, my xml layout works fine and when I got to the layout view it displays the color just as I want it to. When I go to setContentView() in the activity that calls the xml it is never displayed and I only get a blank screen.
Second, since this initial issue described above I have tried several fixes and have numbered them accordingly. As I did a fix I usually only bothered to comment it out instead of deleting it after it didn't work. After certain lines there is a number, so if three lines have 1's behind them then those were the three lines used in attempt #1.
Third, while trying these fixes I added a colors xml file while I'll display as well.
Finally, I'll show my main activity first, xml file second, and colors file last. As you can see my ultimate goal would be to change the background dynamically but I can't even to get it to work normally right now. And FYI my splash screen works fine. But that's an image.
Thanks for you help.
public class Blink extends Activity {
long startTime= System.currentTimeMillis();
long now=0;//the current time in millis
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//TextView backgroundColor=new TextView(this);2,3,4,5,6
//backgroundColor.setBackgroundColor(0xFFFF0000);5
//backgroundColor.setBackgroundResource(R.color.royalBlue);2,3,4
//backgroundColor.setVisibility(0);//make visible 3
setContentView(R.layout.blank);1
//setContentView(backgroundColor);4,5,6
//backgroundColor.setBackgroundColor(Color.argb(255, 255, 255, 255));6
//setContentView(R.layout.blink_blue);
//blink from royal blue to blank
/*while(true){
startTime= System.currentTimeMillis();
do{
now=System.currentTimeMillis();
setContentView(R.layout.blink_blue);
}while((-(startTime-now))>1000);
do{
now=System.currentTimeMillis();
setContentView(R.layout.blank);
}while((-(startTime-now))>1000);
}*/
}
This begins the xml file
//it is formatted properly but for some reason stack overflow doesn't like it so I'm only posting relevant lines.
//This is a Linear layout
android:id="#+id/blinkBlue"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/royalBlue"
This begins the colors file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="royalBlue">#4169e1</color>//Yes I have tried #FF4169e1 instead
<color name="plainBlue">#ff000000</color>
<color name="darkBlue">#ff000000</color>
<color name="black">#00000000</color>
<!-- I also know that the blues here aren't those colors... I'll change that when I fix this thing. -->
</resources>
You don't set colors that way, You specify colors of your Layouts that are defined in your XMLs and then you setContentView() to that XML file.
For example, lets pretend that your XML file has the name as my_layout.xml, then you have specified the color in my_color.xml so you go this way:
You write a layout in my_layout.xml
Now you write a resource XML for color as you have written above, and save it in the /res/values/my_color.xml
Set the layout (defined in my_layout.xml) background to be `android:background="#color/my_color"
in your code, use setContentView(R.layout.my_layout)
This will make my_layout.xml to be your content's layout and further the background color thing will be handled by the layout inside my_layout.xml file.
I hope that helps.
Take a look at using a state list drawable as a background. Each item can point to a shape drawable that specifies a different background color. Alternatively, use a shape drawable as a background that points to a color state list as the solid color.
If the built-in attributes available for defining a state list drawable or a color state list are not right for your application, you can use the technique shown in this thread to define your own.