Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the width and height when show an Activity as a Dialog

If I have defined a Activity:

public class DialogActivity extends Activity{      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(dialog_activity.xml);     }   } 

I would like to display the above activity like a dialog, so in the AndroidManifest.xml file, I declare this activity like below:

<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"/> 

Everything is fine at this point, my DialogActivity showed as a dialog.

The problem is How to customize the width and height of the DialogActivity to make it more like a small dialog? (Currently it occupies most of the screen by default)

----------------------------Update-----------------------

I defined a custom theme like below:

<style name="myDialog" parent="@android:style/Theme.Dialog">         <item name="android:windowFrame">@null</item>         <item name="android:windowNoTitle">true</item>         <item name="android:windowIsFloating">true</item>         <item name="android:windowContentOverlay">@null</item>          <item name="android:width">100dip</item>          <item name="android:height">100dip</item>  </style> 

Then, declare the DialogActivity in AndroidManifest.xml as.

<activity android:name=".DialogActivity" android:theme="@style/myDialog"/> 

My DialogActivity now even occupies the whole screen:( . The width and height definition:

<item name="android:width">100dip</item> 

in the theme do not take any effect, why?

like image 382
Leem Avatar asked Jul 08 '11 12:07

Leem


2 Answers

I finally figured out one way to customize the size of my DialogActivity.

That's inside the onCreate() method of DialogActivity, add the following code:

WindowManager.LayoutParams params = getWindow().getAttributes();   params.x = -20;   params.height = 100;   params.width = 550;   params.y = -10;    this.getWindow().setAttributes(params);  
like image 173
Leem Avatar answered Oct 16 '22 07:10

Leem


You can just change params of window in your acctivity like:

override fun onStart() {         super.onStart()         window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)     } 
like image 24
Djek-Grif Avatar answered Oct 16 '22 06:10

Djek-Grif