setting a max width for my dialog - android

My dialog is using a linearlayout for its root, and its using the following code as its custom theme:
<style name="HuskyBusDialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.

The width will be dealt with in the XML for the linear layout, not in the style that you apply to it. Use the android:layout_width tag in XML to specify how wide it could possibly be.

An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog
This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:
1) set a layout listener to know when the dialog layout is set.
2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size
Here is the working snippet I'm currently using, called after setContentView():
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout()
{
adjustDialog();
}
});
Now the dialog size adjustment, as a percentage of actual screen size:
private void adjustDialog()
{
Window w = getWindow();
w.setGravity(Gravity.CENTER);
int current_width = w.getDecorView().getWidth();
int current_height = w.getDecorView().getHeight();
WindowManager.LayoutParams lp = w.getAttributes();
DisplayMetrics dm = getResources().getDisplayMetrics();
int max_width = (int) (dm.widthPixels * 0.90);
int max_height = (int) (dm.heightPixels * 0.90);
if (current_width > max_width)
{
lp.width = max_width;
}
if (current_height > max_height)
{
lp.height = max_height;
}
w.setAttributes(lp);
}

yeaa if u wanna do with Widht GO for ur xml
and try to do
android:layout_width="Defined in pixels//20px"
or u can try this also
android:width="Defined in pixels// 30px"
i hope it will be hepful to u

Related

How to make Dialog's width match screen width

I want to create a Dialog with custom layout. I want it's width to be same as the phone screen's width and height as WRAP_CONTENT.
Here is what I tried:
Dialog dialog = new Dialog(context, R.style.DialogSlideAnim);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_share);
dialog.setCanceledOnTouchOutside(true);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = width;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.BOTTOM;
dialog.getWindow().setAttributes(layoutParams);
The issue is that the dialog takes up only about 90% of the screen width, there is some margin on the left and right side of the dialog. How can I make it completely fill the width of the phone?
The accepted solution is simple and working, but you can also try the below solution to achieve the requirement too.
Step 1: Create Subclass of Dialog class as you want to create custom dialog.
public class ARProgressDialog extends Dialog
{
Activity context;
public ARProgressDialog(Context context,int id) {
// TODO Auto-generated constructor stub
super(context,id);
this.context=(Activity) context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.progress_dialog); // Your custom layout
// BELOW CODE IS USED TO FIND OUT WIDTH OF ANY DEVICE
DisplayMetrics displaymetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
// BELOW CODE IS USED TO SET WIDHT OF DIALOG
LinearLayout layout = (LinearLayout)findViewById(R.id.dialogLinearLayout); // this is the id of your parent layout defined in progress_dialog.xml
LayoutParams params = layout.getLayoutParams();
params.width = width;
layout.setLayoutParams(params);
...// add your remaining code
}
}
Step 2: Show dialog.
ARProgressDialog dialog=new ARProgressDialog(this,R.style.MyTheme);
dialog.show();
Step 3: Code of MyTheme.xml
<style name="MyTheme" parent="android:Theme.Holo.Dialog">
<item name="android:windowBackground">#00000000</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
This may be helpful for you:
dialog.setContentView(R.layout.my_custom_dialog);
dialog.getWindow().setBackgroundDrawable(null);
or you can try with style class like this:
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
...
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>

android popup activity show MainActivity with it

i use this for make floating activity
public class Conversation extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpWindow();
setContentView(R.layout.main);
}
public void setUpWindow() {
// Nothing here because we don't need to set up anything extra for the full app.
}
}
this is simple activity now i will make it floating by use this
make another class
public class PopupConversation extends Conversation {
#Override
public void setUpWindow() {
// Creates the layout for the window and the look of it
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 1.0f; // lower than one makes it more transparent
params.dimAmount = 0f; // set it higher if you want to dim behind the window
getWindow().setAttributes(params);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if (height > width) {
getWindow().setLayout((int) (width * .9), (int) (height * .7));
} else {
getWindow().setLayout((int) (width * .7), (int) (height * .8));
}
}
}
now when i start the class PopupConversation it must show my Conversation activity as floating activity and that's what happen
but the problem its show or start MainActivity in my application behind it
see photo
it must be like this
as we see its floating outside application
now my code not floating outside application its start application and float it over the application
as we see the application start and the floating activity start over it
this is my activity in manifest
<activity
android:name=".activity.PopupConversation"
android:theme="#style/Theme.FloatingWindow.Popup"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
android:clearTaskOnLaunch="true"
android:exported="true"
tools:ignore="ExportedActivity"
>
</activity>
and this is my style code
<style name="Theme.FloatingWindow.Popup" parent="Theme.AppCompat.NoActionBar" >
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">stateUnchanged</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowAnimationStyle">#style/PopupAnimation</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
i want my activity be like first photo i want show it in every where in phone not in application
at the end i start the class from service by call this
Intent window = new Intent(this, PopupConversation.class);
window.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(window);
i solve it by use this when i start it
Intent i = new Intent();
i.setClass(this, PopupConversation.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);
so i just add
Intent.FLAG_ACTIVITY_MULTIPLE_TASK

Get the current ActionBar size

I tried several things before and searched the web, but no answer.
How do I get the current ActionBar size?
I styled my ActionBar like this:
<style name="MusicSlide.Theme.ActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:height">60dp</item>
<item name="android:actionBarSize">60dp</item>
<item name="android:homeAsUpIndicator">#drawable/ic_drawer</item>
<item name="android:background">#color/complete_transparent</item>
<item name="android:titleTextStyle">#style/MusicSlide.Theme.ActionBar.TitleText</item>
<item name="android:subtitleTextStyle">#style/MusicSlide.Theme.ActionBar.TitleText</item>
</style>
It is transparent.
In my app I implemented a DrawerLayout. I don't want the DrawerLayout to be underneath the ActionBar , so I set the margin of the DrawerLayout:
final TypedArray styledAttributes = mContext.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int actionheight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mDrawerListRight
.getLayoutParams();
mlp.setMargins(0,(actionheight + statbarheight),0,0);
The problem is that android.R.attr.actionBarSize returns the original height of the ActionBar.
How do I get the 60dp I set in my style?
android:actionBarSize isn't an attribute of Widget.ActionBar, this is why you aren't returning the correct value. Add that line to your app's theme, wherever you define android:actionBarStyle.

How to set percentage width for a dialog-themed Activity

I have an Activity with a dialog theme (Theme.Holo.DialogWhenLarge). It appears too narrow, and I'd like it to fill up a larger percentage of the screen. I am trying to accomplish this by overriding the windowMinWidthMinor and windowMinWidthMajor attributes.
The theme used by my Activity looks like this...
<style name="MyTheme" parent="#android:style/Theme.Holo.DialogWhenLarge">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
However, it seems like the windowMinWidthMajor and windowMinWidthMinor have no effect. Can anybody explain what I'm doing wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Some code here
setWindowHeight(90);
}
/**
* Set percentage width height
* #param percent percent from current size. From 0 to 100.
*/
private void setWindowHeight(int percent){
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = (int)(screenHeight*percent/100);
this.getWindow().setAttributes(params);
}
It's impossible to set percents for android, BUT there is a way around. What I've done is get the screen width and multiply it by the percent I want my view or item to be (example: if I want something to fill 40% of the width if would be Screen-Width * 0.4)

How to set layout unit for parameters set in code?

I am writing an Android app. I have an activity in my main project which inherits from an activity in my library project. I have a custom titlebar in the base activity which has a button, which uses the following style:
<style name="TitleButton">
<item name="android:padding">6dp</item>
<item name="android:layout_width">48dip</item>
<item name="android:layout_height">48dip</item>
<item name="android:layout_gravity">right</item>
</style>
Works fine. In my child activity, I want to add a button. I can add the button, and it clicks and works fine, but it LOOKS wrong. I am adding the button as so:
ImageView imgAdd = new ImageView(this, null, R.style.TitleButton);
imgAdd.setImageResource(R.drawable.add);
imgAdd.setClickable(true);
imgAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addGroup();
}
});
FrameLayout tb = (FrameLayout) this.findViewById(R.id.Header);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
tb.findViewById(R.id.TitleClose).getLayoutParams()
);
lp.gravity = Gravity.RIGHT;
lp.width = 48;
lp.height = 48;
lp.setMargins(0, 9, 68, 0);
imgAdd.setLayoutParams(lp);
tb.addView(imgAdd, 1);
Notice how the Add button which I've added through code is too big, and offset too much. I'm figuring it must be due to what was pointed out in the comments on this answer, that setting the width and height so sets them in pixels, where as the XML layout sets them in dip. So, my question is, when setting layout params in code, how can you set the unit, so that I may set my new add button to be measured in dip instead of in pixels so that it will display right?
You can use DisplayMetrics to get current density and use it to set width, height and margins accordingly (reference here: px = dp * (dpi / 160))

Categories

Resources