Set Android View to Invisible/Gone Not Working - android

I have a surfaceview for which im putting a camera preview onto. On top of that, I have a relative layout that has a bunch of views, like a scan button, etc.
I am trying to make one of these views invisible, like this
myIcon.setVisibility(View.INVISIBLE);
And I have also tried
myIcon.setVisibility(View.GONE);
Neither seems to be working. Invisible doesn't work at all, and Gone sort of works
Here you can see a 2D icon, a settings button. Before hiding, it looks like this:
Then I call setVisibility(View.GONE)
You can clearly see that the linearlayout that holds those views responded by giving the settings button more space, but it didnt remove the 2D from behind it. I think it has something to do with the SurfaceView, because I know this would normally work.
I have tried
myIcon.requestFocus();
After I do the change of visibility, but still nothing.
Any suggestions? Please only respond if you have some experience with this particular problem
Edit:
Here is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.sample.SampleJavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:id="#+id/cv_surface_view" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<ImageView
android:id="#+id/myIcon"
android:layout_width="30dip"
android:layout_height="30dip"
android:src="#drawable/twod_icon"
/>
<ImageButton
android:id="#+id/settingsBtn"
android:layout_width="30dip"
android:layout_height="30dip"
android:src="#drawable/settings_gear"
android:background="#null"
/>
</LinearLayout>
<ImageButton
android:id="#+id/scan_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_small"
android:background="#drawable/blue_radial_grad"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="20dip"
/>
Here you can see the images im trying to hide are wrapped in a linear layout. The surfaceview is at the top, it is the OpenCV wrapped class for a SurfaceView

maybe try myIcon.bringToFront();

Related

Android - Display file path in Toolbar

I am searching for a way to display a file path in the toolbar like this:
It needs to be clickable and should be swipeable if it's a long path. (Or small device).
I thought about using a HorizontalScrollView with a TextView and ImageView, but don't know if that is the best way to accomplish this. Is there a better (simpler) way to do this? Thanks!
Edit:
With thanks to #aelimill I found out that a RecyclerView can go horizontally, but I'm still having some issues. If you click on the text in the previous screenshot it shows this:
But for me (after I set the custom list item to clickable) it is like this:
(Look at the click animation)
How can I display the circle animation just like other ActionBar items?
I solved this by using a RecyclerView as #aelimill suggested. This is the custom list item I used:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:id="#+id/relativeLayout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageButton"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"
android:background="#null"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/textView"
android:layout_alignBottom="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/textView"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageButton"
android:layout_toEndOf="#+id/imageButton"
android:gravity="center"
android:textSize="16sp"
android:minWidth="20dp"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Use selectableItemBackground instead of selectableItemBackgroundBorderless to support pre lollipop devices. (It wont be a circle animation, but a rectangle animation).

Android: LinearLayout Rearranged when updating views

I'm having a problem with a LinearLayout in an app i'm working on. The linear layout is vertical. There are several items in the layout including a TextView, a vertical SeekBar, and an ImageView.
Whenever I update the textView (ie setText()) my layout rearranges. The ImageView goes from being at the bottom to being in the middle. Same thing happens if I replace the drawable in the ImageView.
I assume I have something wrong with how I am specifying my layout. This is the portion having problems:
<modrobotics.code.ui.BlockValueSliderView
android:id="#+id/sliderSix"
android:layout_width="0dp"
android:visibility="gone"
android:layout_height="fill_parent"
android:layout_marginBottom="-12dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/BVLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="12dp"
android:text="64"
android:textColor="#000"
android:textSize="20sp" >
</TextView>
<View
android:id="#+id/circle1"
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_gravity="center"
android:layout_marginBottom="-6dp"
android:background="#drawable/circle"
android:gravity="center_horizontal"
android:padding="0dp" />
<LinearLayout
android:id="#+id/controlElementList12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="0dp" >
<modrobotics.code.ui.VerticalSeekBar
android:id="#+id/slider"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="-20dp"
android:layout_weight="20"
android:padding="0dp"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/seekbar_thumb"
android:thumbOffset="-1dp" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="-20dp"
android:layout_marginTop="-20dp"
android:layout_weight="1"
android:padding="0dp"
android:src="#drawable/on6" >
</ImageView>
</LinearLayout>
</modrobotics.code.ui.BlockValueSliderView>
EDIT:
I have tried the suggestions mentioned below to no avail. I have removed all negative margins, simplified the layout by removing unnecessary LinearLayout, gotten rid of GONE, fixed height for images etc... I'm starting to believe the problem is greater than just this.
I'm using a single Activity with a PageAdapter. It seems I'm not using PageAdapter correctly. startUpdate() is being called over and over again. Any static global variables I have seem be cleaned up or on separate threads some how. I believe these problems may all be linked. Perhaps I don't full understand the threading model when using a PageAdapter.
In 'modrobotics.code.ui.BlockValueSliderView' change the visiblity from GONE to INVISIBLE. It's possible that the layout elements will re-arrange whenever you change the visibility programatically from GONE to VISIBLE of this View because while the View is set to be GONE, it doesn't occupy any space in the layout and rest of the Views are placed in the layout just taking its space. If it's GONE, It behaves like it doesn't exist in the layout. Then, when you sets this view to be VISIBLE, it switches rest of the Views in the layout to prepare a space for itslef.
This turned out to be a problem related to the VerticalSeekBar implementation I was using.
I was rotating the thumb for the seekbar, and also calling bringToFront() on the seekbar. The combination of these two calls caused elements to be rearranged. Not to sure why, but removing these two lines of code fixed my problems.

How can I achieve this layout?

I am trying to achieve this in an android app layout.
So far I have managed to get the buttons the way I wish, but I am having trouble working out how I am going to implement the divider and the layout on the right side of it.
Here is what I have so far:
<?xml version="1.0" encoding="utf-8"?>
<!-- this is main activity's layout. a main menu of sorts. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" android:background="#drawable/background">
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:id="#+id/layout_maintest2_relativelayoutleft">
<Button android:text="#string/string_main_NewCalculation" android:drawableLeft="#drawable/calculator5" android:onClick="onClickButton_NewCalculation" android:id="#+id/button_main_NewCalculation" android:layout_width="300px" android:layout_height="90px" android:drawablePadding="0px"></Button>
<Button android:text="#string/string_main_Help" android:drawableLeft="#drawable/question" android:onClick="onClickButton_Help" android:id="#+id/button_main_Help" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation"></Button>
<Button android:text="#string/string_main_Share" android:drawableLeft="#drawable/share" android:onClick="onClickButton_Share" android:id="#+id/button_main_Share" android:layout_width="150px" android:layout_height="90px" android:drawablePadding="0px" android:layout_below="#id/button_main_NewCalculation" android:layout_toRightOf="#id/button_main_Help"></Button>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/layout_maintest2_relativelayoutright" android:layout_toRightOf="#id/layout_maintest2_relativelayoutleft" android:layout_alignTop="#id/layout_maintest2_relativelayoutleft">
<TextView android:layout_width="wrap_content" android:textSize="24px" android:textStyle="bold" android:layout_height="match_parent" android:id="#+id/textView_calculator_CalculatorTitle" android:text="#string/string_calculator_CalculatorTitle"></TextView>
</RelativeLayout>
</LinearLayout>
Its hard to tell from the image you uploaded but are you trying to keep the two relative layouts side by side?
You can try to position the layouts by declaring them to be drawn to one side of the parent or the other. I cant remember off the top of my head at the moment and I know thats a crappy answer but I think the answer is somewhere in here
http://developer.android.com/guide/topics/ui/layout-objects.html
Take a look at the android:layout_alignParentRight="true" declaration.
Im pretty sure I used something similar to position multiple views and buttons in the same parent before. Try it out and when I get back to my work computer on monday Ill look through my projects and see which one I know works the best.

How do I vertically align an item within a list using relative layout?

I am using a list view in Android 1.5 to show a list of images and text next to the image. I am trying to vertically center the text but the text is at the top of the row instead of centered. Below is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<ImageView android:id="#+id/item_image" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingRight="10dip"
android:src="#drawable/default_image" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"/>
<TextView android:id="#+id/item_title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="#id/item_image"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
/>
</RelativeLayout>
It seems strange that I need to set alignParentTop="true" when I'm trying to vertically center the text, but if I don't the text does not even show up. What am I doing wrong?
EDIT following the comments:
It turns out making this work with RelativeLayout isn't easy. At the bottom of the answer I've included a RelativeLayout that gives the effect wanted, but only until it's included in a ListView. After that, the same problems as described in the question occurred. This was fixed by instead using LinearLayout(s).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:orientation="horizontal">
<ImageView android:id="#+id/pickImageImage"
android:layout_width="100dp"
android:layout_height="80dp"
android:background="#drawable/icon"
android:scaleType="fitXY"
android:layout_marginRight="10dp"/>
<TextView android:id="#+id/pickImageText"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="left|center_vertical"
android:text="I'm the text"/>
</LinearLayout>
If you want to have two text boxes, you can nest a second orientation="vertical" and LinearLayout after the ImageView and then put the text boxes in there.
This works, but I have to admit I don't know why the RelativeLayouts didn't. For example, this blog post by Romain Guy specifically says that the RelativeLayout should. When I tried it, I never got it to quite work; admittedly I didn't do it exactly as he did, but my only changes were with some attributes of the TextViews, which shouldn't have made that much of a difference.
Here's the original answer:
I think you're confusing Android with all those somewhat contradictory instructions in RelativeLayout. I reformatted your thing to this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<ImageView android:id="#+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:src="#drawable/icon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/item_image"
android:layout_centerVertical="true"
android:text="Blah!"/>
</RelativeLayout>
And that works fine. I removed many of your redundant android:layout_alignParentxxx because they weren't necessary. This view now comes up with the picture in the top left corner and the text vertically centered next to it. If you want the picture vertically centered as well, then you can't have the RelativeLayout be on android:layout_height="wrap_content" because it's trying to make itself no taller than the height of the picture. You'd have to specify a height, e.g. 80dp, and then set the ImageView to a fixed height like 60dp with android:scaleType="fitXY" to make it scale down to fit properly.
Was stuck on a similar issue for a while, but found this from CommonsWare:
"When you inflate the layout, use inflate(R.layout.whatever, parent, false), where parent is the ListView."
Works but only when you set the height of the row to a specific value (ie you can't use wrap_content).
Baseline directive would do it, but ImageView simply does not support baseline alignment as of today. You can work around this by creating a subclass of ImageView, override the getBaseline() method and return the height of the image.

View overlapping with RelativeLayout on Android 1.5

I am having a problem with views overlapping in a RelativeLayout on Android 1.5... Everything is working fine on Android 1.6 and above.
I do understand that Android 1.5 has some issues with RelativeLayout, but I was not able to find anything on StackOverflow or the android beginners group for my specific problem.
My layout consists of four sections, each of which are made up of a TextView, a Gallery, and another TextView aligned vertically:
Running Apps
Recent Apps
Services
Processes
When all four sets of these items are displayed everything works fine. However, my app allows the user to specify that some of these are not displayed. If the user turns off Running Apps, Recent Apps, or Services then the remaining sections all of a sudden overlap eachother.
Here is my code for the layout. I'm not sure what I am doing wrong. When the user turns off display of a section I use the View.GONE visibility setting:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:background="#null"
>
<!-- Running Gallery View Items -->
<TextView
style="#style/TitleText"
android:id="#+id/running_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="1sp"
android:paddingRight="10sp"
android:text="#string/running_title"
/>
<Gallery
android:id="#+id/running_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/running_gallery_title_text_id"
android:spacing="5sp"
android:clipChildren="false"
android:clipToPadding="false"
android:unselectedAlpha=".5"
/>
<TextView
style="#style/SubTitleText"
android:id="#+id/running_gallery_current_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/running_gallery_id"
android:gravity="center_horizontal"
/>
<!-- Recent Gallery View Items -->
<TextView
style="#style/TitleText"
android:id="#+id/recent_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/running_gallery_current_text_id"
android:gravity="left"
android:paddingLeft="1sp"
android:paddingRight="10sp"
android:text="#string/recent_title"
/>
<Gallery
android:id="#+id/recent_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recent_gallery_title_text_id"
android:spacing="5sp"
android:clipChildren="false"
android:clipToPadding="false"
android:unselectedAlpha=".5"
/>
<TextView
style="#style/SubTitleText"
android:id="#+id/recent_gallery_current_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recent_gallery_id"
android:gravity="center_horizontal"
/>
<!-- Service Gallery View Items -->
<TextView
style="#style/TitleText"
android:id="#+id/service_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/recent_gallery_current_text_id"
android:gravity="left"
android:paddingLeft="1sp"
android:paddingRight="10sp"
android:text="#string/service_title"
/>
<Gallery
android:id="#+id/service_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/service_gallery_title_text_id"
android:spacing="5sp"
android:clipChildren="false"
android:clipToPadding="false"
android:unselectedAlpha=".5"
/>
<TextView
style="#style/SubTitleText"
android:id="#+id/service_gallery_current_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/service_gallery_id"
android:gravity="center_horizontal"
/>
</RelativeLayout>
I ommitted the xml for the Processes section in a (somewhat vain) attempt to keep this shorter...
What can I do to make this work in Android 1.5? I don't think it is just a matter of reordering the views in the xml because it works fine when everything is displayed.
Two possible solutions:
Try setting the element's height to 0 or 1 px and visibility to INVISIBLE instead of GONE.
Wrap each Gallery/TextView in a LinearLayout set to wrap_height, and set the above/belows on the layouts instead of the subviews. Then set the subelements to View.GONE, leaving the linear layouts used for the relative positioning still visible but with wrapped height 0.
The idea with either solution is to make sure you're never positioning something relative to a view that's View.GONE; I'd suspect that's the source of the bug you're running into.
If I may ask, though... why do you even need to use a RelativeLayout here at all? From what I can see at a glance, everything here would fit fine into a vertical LinearLayout, and in fact seems to be conceptually simpler for this arrangement.

Categories

Resources