For some reason I can't use a Dialog, so I'm just emulating it with a View hierarchy. I want to make my dialog appear in the center of the screen with a standard Android dialog size.
I found a resource value dialog_min_width_minor, but how do I use it? When I try to do it like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/semi_transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- provides the gray semi-transparent background !-->
<FrameLayout
android:id="#+id/dialog_container"
android:layout_gravity="center"
android:background="#android:color/white"
android:layout_height="wrap_content"
android:layout_width="#android:dimen/dialog_min_width_minor"/>
</FrameLayout>
I just get an exception:
Binary XML file line #8: You must supply a layout_width attribute.
I spent a bit of time and came upon this example. You can emulate a Dialog using an Activity with this theme.
Interestingly enough, the AppCompat library has its own definition of these properties, but they aren't lined up with the standard sizes.
For what it's worth, android.R.dimen.dialog_min_width_minor is defined like this in one case:
<item type="dimen" name="dialog_min_width_minor">95%</item>
You can't set this as a width in an xml layout, but you can systematically use it to set the width in your activity/fragment/view. You can do something like this:
// Get the percentage as defined by android.R.dimen.dialog_min_width_minor
Resources resources = context.getResources();
TypedValue typedValue = new TypedValue();
resources.getValue(android.R.dimen.dialog_min_width_minor, typedValue, true);
float percentage = typedValue.getFraction(1, 1);
// Get the width of the display
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int displayWidth = size.x;
// set the width of your view
// view.width = displayWidth * percentage <--pseudocode
Related
I have a view that I want to layout in either a large format or compact format depending on the length of text in a textview like so:
What is the best way to achieve this?
I am thinking I will need to measure the length of the text and the controls and get the available space to see if they would fit on one line. If they will then use a compact layout otherwise use the large layout.
Is this the right approach or is there a way to achieve this with a single layout?
The layout you require is something called FlowLayout. But android SDK doesn't have such a layout support currently. There are nice 3rd party libraries available & one of them is FlowLayout.
Gradle:
compile 'com.wefika:flowlayout:0.4.1'
In your layout :
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Put both in a LinearLayout and set its orientation programically in java file like this
LinearLayout layout = /* ... */;
layout.setOrientation(LinearLayout.VERTICAL);
Set Horizonatal if you want compact and set vertical if you want large one.
and put condition based on the width you get from below code
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
You can try to make user set texts for all your textViews and after use something like this:
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if(layout.getWidth()/2<Title.getWidth()){
p.addRule(RelativeLayout.BELOW, R.id.Title);
controls.setLayoutParams(p);
}else{
p.addRule(RelativeLayout.RIGHT_OF, R.id.Title);
controls.setLayoutParams(p);
}
It is called a FlowLayout. Google it for multiple libraries that support it.
I have a list of photos, each having some text in it. I would like each photo to horizontally fill the screen and would like to height to be the same as of the width
Trying to accomplish something like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="self.width"
...
How could I accomplish this ?
Since I will be using a recycleview I can edit the height at runtime.
XML is not dynamic. Screen measurements have to be done in Java during onCreate.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int width=size.x; int height=size.y;
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width,width);
yourView.setLayoutParams(params);
find width of your layout programmatically, and set that value as height of your layout.
//get Width
int width=layout.getWidth();
//set height
layout.getLayoutParams().height=width;
should be like this,if you want height for full screen use match_parent also
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I'm using a SlidingPaneLayout in my activity:
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myslidingpanelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- menu left -->
<LinearLayout
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#8d305f"
android:orientation="vertical" >
...
</LineareLayout>
<!-- main page right-->
<LinearLayout
android:id="#+id/right_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical" >
...
</LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>
I want the menu to cover 3/4 of the page I want it to work on all the phones so I can't put for example
android:layout_width="300dp"
I want to calculate the screen width and set it to the left pane
Thank for your help
Thanks for you all I found this answer and it works with me:
int width;
int height;
if (android.os.Build.VERSION.SDK_INT >= 13){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else {
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
}
if(width>0&&height>0){
LinearLayout layout = (LinearLayout)findViewById(R.id.menu);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = height;
params.width = width*3/4;
}
Just looking up the doc for sliding pane, looks like it functions like a linear layout, and can use the
layout_weight
parameter to set a percentage based width since the parent viewgroup is match_parent
In the case of 3/4 = 75% you can
android:layout_weight="0.75"
From the android docs http://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html:
Like LinearLayout, SlidingPaneLayout supports the use of the layout parameter layout_weight on child views to determine how to divide leftover space after measurement is complete. It is only relevant for width. When views do not overlap weight behaves as it does in a LinearLayout.
When views do overlap, weight on a slideable pane indicates that the pane should be sized to fill all available space in the closed state. Weight on a pane that becomes covered indicates that the pane should be sized to fill all available space except a small minimum strip that the user may use to grab the slideable view and pull it back over into a closed state.
And from the LinearLayout docs http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
Note: You will end up setting the layout_width parameter to 0dp since the view group will actually use the weight to lay the children out
Apart from Selecsosi's answer, which is correct, there is also this view I wrote to always display the second item as a pane (ignoring the default show-side-by-side-if-the-fit behaviour). It can, as the name shows, wrap around the sliding view.
You can implement the behaviour you're after by either using a lot of #dimen resources and switching them based on swXXXdp-(port|land) or just setting the sliding view's width at runtime (something I'm reasonably certain you can do with the default layout as well).
I am building an android application with eclipse and I'm struggling to master the image positioning in the screen. I have put an image in a relative layout, and I want the borders of the image to match perfectly the borders of the screen, but when I extend the image manually it never fits and even if I put padding :/
(see the right side is ok, but not the left, the problem is not even a problem about the dimensions of the screen)
Thank you for your help
If you want to do that, you need to take the dimensions of the screen using this code :
// pixels
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Then you need to set programmatically the dimensions of the picture using the width and height properties.
If you need that your image to be the backgroung of the activity you can use the background property.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
</RelativeLayout>
The "#drawable/your_image_name" is your image. The image should be the dimensions that google recomends for deferent sizes of screen.
Im using a customized webview as follows
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyWebView="http://schemas.android.com/apk/res/com.mds.android.mireader.webview" android:background="#FFFFFF" android:layout_height="fill_parent" android:layout_width="fill_parent">
<com.mds.android.mireader.webview.MyWebView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/myweb" **android:layout_width="318px"** android:layout_height="fill_parent" > </com.mds.android.mireader.webview.MyWebView>
im doing some thing strange, for that i need to satisfy following condition
condition: webview width should always divisible by 3
So i can't give fixed value or fill_parent in above xml file.For example if i give fill_parent it works fine for portrait(assume HVGA emulator with portrait width=480 which is divisible by 3)but no success for landscape(bcz landscape width 800 which is not divisible by 3).
Solution should work for both orientation change and device change"
Im struggling with 1 or 2 pixel values difference and it will be solved if i satisfy that condition. TIA ....
HI,
You can get the height and width of the screen using following piece of code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Now you have the width you can check whether it is divisible by 3 or not. If yes then well and good, otherwise you can subtract the modulus(width%3) from the original width and set it as the width of your webview.
Add A View Programmatically:
Sample XML:
<LinearLayout (define id:testLayout, width, height here> </LinearLayout>
Java Code:
LinearLayout linLayout = (LinearLayout) findViewById(R.id.testlayout);
// create an object of your webview and set all the properties you want like background color etc.
MyWebView myWebView = new MyWebView();
linLayout.addView(myWebView, new LinearLayout.LayoutParams(<my calculated width>, LayoutParams.FILL_PARENT);
PS: you dont have to add the webview object in your XML file. "addView" function will add your webview inside your linear layout object.
Hope this helps!!