I can't scroll my customized view - java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<ScrollView android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<adam.music.testfont.NwcDrawingArea android:id="#+id/NwcDrawingArea"
android:layout_height="2000dp"
android:layout_width="2000dp"
/>
</ScrollView>
</LinearLayout>
I created Android Project and edited main.xml as above.
And then I customized view to draw image.
To see the effect of scrolling customized view I set width and height as above.
Now I can see the image in a customized view but it doesn't scroll.
Could you help me what the problem is?
Should I add something more?

There's an interplay between ScrollView and its contents. For example, if, instead of your NwcDrawingArea widget, you inserted this TextView into your ScrollView:
<TextView android:text="8This is a lot of text 7This is a lot of text 6This is a lot of text 5This is a lot of text 4This is a lot of text 3This is a lot of text 2This is a lot of text 1This is a lot of text"
android:layout_width="10dp"
android:layout_height="2000dp"
android:background="#FF00FF00"
/>
You'll see a skinny green vertical TextView whose text is longer than the screen, and the ScrollView shows scrollbars that let you see the hidden part of the TextView to the extent of the text. Now, change the TextView layout_width="2000dp". The TextView becomes a full-screen green area that has a single line of text that runs off the right side of the screen. ScrollView, of course, shows no horizontal scroll bars. However, ScrollView shows no vertical scroll bars either, even though we sized the TextView to be much longer than the screen. ScrollView is attempting to determine the visually interesting portion of its contents, so you need to understand the behavior for whatever widget you are subclassing.
For example, ScrollView respects the layout sizes of LinearLayout and RelativeLayout in the way that you expected it to behave with TextView. In fact, it is common to make a LinearLayout or RelativeLayout -- rather than a view such as TextView -- the child of a ScrollView. Drop your TextView in a fixed-height LinearLayout and you should get the behavior that you expected.

Related

How can I change the width of an image button in a horizontal scroll view?

I'm trying to create a page that looks like the contribution page of GitHub (like each square represents a day, and the transparency of the square represents the number of commits). I want the page to look something like this:
So, I made a horizontal scroll view because I want the section of squares can be scrolled horizontally. And I tried to add the squares into the HSV. Here's the code:
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="295dp"
android:layout_marginTop="10dp"
android:layout_marginRight="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/time">
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="20dp" <!-- the problem -->
android:layout_height="35dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/square" />
</HorizontalScrollView>
The problem is, I cannot set the layout_width of the image button, no matter what I change it to, the design always remain like this:
As you can see, the height is correct, but the width is fixed at a weird place. Is there anyway I can change it?
Any help would be appreciated.
You should use the layout inside of the HorizontalScrollView instead of adding the image button directly. For example LinearLayout or GridLayout
The point is that the scroll view can only have one child, because the scroll view extended FrameLayout
Quote from the documentation:
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A HorizontalScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through.

Android - How to Resize/Reposition ImageView According to Parent?

What's the recommended approach for ensuring an ImageView button is the correct size according to its parent?
I have a VideoView set to "match_parent" (screen width) and an ImageView over the VideoView that is 60dp x 60dp, which is fine on the screen I'm testing.
Problem is, if I rotate the phone and the VideoView is much smaller, the ImageView is no longer proportional and in the correct position. Same issue is true for larger screen sizes such as tablets.
I want the ImageView to always be the same proportional size and appear in the same position relative to the VideoView (its parent).
You should consider using layout_sum in android:
Since I don't know how you are exactly coding this, I can only provide assumptions based code. I am assuming that you would want to proportionately align an VideoView and ImageView in a parent. (Assuming you required 30% of the space to be occupied by the ImageView and the rest 70% of the space to be occupied by the VideoView).
You can code the XML file as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
android:layout_gravity="center">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7"/>
</LinearLayout>
This will embed the VideoView and ImageView into the LinearLayout (Parent view).
Try to use RelativeLayout,wrapContent and avoid using hardCoded sizes.
Or better define a separate layout for Landscape and Portrait View that may solve your issue.
Even if your issue is not solved try using this library i frequently use it in my projects-SDP
I ended up doing this programmatically using ViewTreeObserver GlobalLayout listener. It works great and I only need it for one view on the screen, so not a big deal. Button appears in the correct position relative to its parent size even with the phone is rotated.
var topPadding = Convert.ToInt32(_videoView.Height * .25);
_buttonLayout.SetPadding(_buttonLayout.PaddingLeft, topPadding, _buttonLayout.PaddingRight, _buttonLayout.PaddingBottom);

How to set view width according to another view's position?

I have a TextView and a Button in an android XML layout(TextView is at left most side and Button is at right most side in the same line), as the text in TextView is dynamic so it should take a minimum width for some initial text to be visible. As width of devices changes the minimum width of TextView does not look good all the time.
For example if width of device becomes greater, then the minimum width alloted to the TextView would look so bad because it would not use more space available on the screen.
So what I want to do is to set the minimum width of TextView according to the position of Button present at the Right most side of the screen
Like minWidth = "leave this dp" form Button.
I hope you have understood it well if not then ask me more about it.
Seeking for attributes to use.
Use a LinearLayout with horizontal orientation put that TextView and Button inside that LinearLayout. Give weight of that TextView to 1 and set width to 0dp and use wrap_content for Buttn's width
Code snippet
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
Put button and textview inside linearlayout and set weight for textview and button according your requirement. it will set layout view according device screen size.

Darken the screen except one gridview item in Android

I am trying to find a way to darken the entire screen except for a specific gridview item (in order to draw attention to this specific item).
I have been able to darken the whole screen by putting a black partially transperant view in front of the app content like this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gridView"
android:numColumns="2"/>
<View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#88000000" />
</FrameLayout>
Unfortunately I cannot come up with a way to make one gridview item not get darker. Can anyone help me out here?
Inflate the gridview position view in a custom dialog which maintains its x, y position on screen and 'darkens' the rest of the layout.
Could this also work? Set a background color to whatever you want and in getView return transparent image to all grid items except the one you want to display.

Android. How to make dialog buttons appear a bit outside the dialog

I need to have a dialog (it's a game dialog) where buttons are at the lower corners of the dialog. Not inside the dialog but rather on the very corners (i.e. part of the button will reside over the dialog and the part will be outside of it).
First, as far as I know you can't move layout children outside their parent.
I've never tried exactly what you're going for, but I think it can be done. The trick would be to go with an activity with a dialog theme (you can find examples of these on the developer site or the API demos). Make sure your layout's root node has width and height set to wrap_content. Your root layout should be a RelativeLayout and have NO background (android:background="#0000").
Next, add another layout to your root node (FrameLayout would probably work) with a custom drawable for a background (or use the one that the default dialog uses from the framework) and width and height set to fill_parent or match_parent. Set android:padding to some dip value which pulls the background in from the edge of the dialog.
The only thing left to do would be to add your other layout elements to the root node. The FrameLayout will be drawn beneath everything else, and the padding will create the illusion of borders which do not encompass your UI.
Update
Yikes, just tried the above with good and bad results. First, you'll definitely want to look at the "Custom Dialog" example from the API demo, which makes use of:
CustomDialogActivity.java
layout/custom_dialog_activity.xml
xml/styles.xml
drawable/filled_box
Create an activity which uses the above xml layout file, and set the style for the activity to Theme.CustomDialog that you defined in xml/styles.xml. This will get you a red background for your activity. You can then edit the filled_box shape file to just have one background attribute set to invisible ("#0000"). The result should be an dialog-shaped activity with no background.
Next I tried to hack a background using my thoughts from above. The idea should be that there's a phony background drawn behind the other UI elements which does not encompass them, so it could be "shrunk" using layout_margin and not affect them. The problem here is that the phony background needs to have width and height set to relative to the other UI elements, so it sort of HAS to encompass them, so it can properly measure its own width and height relative to them.
So I think the solution could be to do most of what I've said above, except don't try the phony background thing. Just use a 9-patch drawable for your root layout background, and shrink the edges of your background to be drawn farther in than your content. You'd still use the custom theme stuff from above with an invisible window theme.
Here is a sample layout which i tried:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:orientation="vertical"
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_frame">
</FrameLayout>
<Button android:layout_width="wrap_content"
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:text="Button"></Button>
</RelativeLayout>
here is the screenshot:
hope u get the hint , goodluck

Categories