I have a Linear Layout
and I want to add to this Linear Layout a button (from 3 Buttons) Dynamic in Run Time Depending on an integer value that returns from a function
I know how to add a view to a Layout using AddView , and how to remove views form layout using removeView ...
but my question is how to check if the view Exists in the layout before call the method reomveView
I suggest you create your buttons beforehand and just set the visibility to View.GONE
when you trigger some event you can set other buttons visibility to true while the others are false..
this will work with no problems and you dont have to add them dynamically.. it will just "seem" dynamic :)
You can do that with this code:
button1.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);
Given the buttons are "button1" and "button2"
View.GONE will "remove" the view from the screen, however it is still "there" it just doesn't appear for the user, and it doesn't take up any space
View.INVISIBLE will "remove" the view from the screen, how ever the space it took up is still used by it.
View.VISIBLE will show the view as it would usually.
Related
I have two layouts, manageLayout and mainLayout. They have constraints to the parent on all sides. I need in a one time have manageLayout on the top of mainLayout, and on the other time mainLayout on the top of manageLayout. Of course, there is sense in using visibility=gone on one of them, but i need one layout on the background of another. Problem: layout on the background handle events from top layout. How to make lower layout(and his elements) untouchable when another layout is risen?
Layout tree image:
LayoutTreeImg
Code sample, where i want to disable communications with lower layout: https://pastebin.com/PeL7u3YD (not only isSaveEnabled=false had no effect, also isEnabled=false had no effects too)
If you just need an explanation.
Once you've initialized both your views for mainLayout and manageLayout, you will need to set an empty onClickListener on both of them. Basically, layouts should get the click but do nothing. This way you can block the layout and widgets underneath the view on Front from getting clicked.
Now for for switching view to front maintain a boolean to know which view is on the front and on your button click set the other view bringToFront() (Or try some other ways mentioned here if you want) and don't forget to switch the boolean value.
Let me know if this works for you or you have any issues regarding this.
According to my perception, you can make lower layout setEnable(false). I hope it will work.
The problem
Suppose you have a view inside a layout view, and you wish that each touching event on the layout would affect the view's background selector.
for example, when you touch the layout , the selector of the view will choose the state_pressed state.
What I've tried
I've read about duplicateParentState and addStatesFromChildren , but I think it's the opposite of what I'm searching for.
I've also tried to use splitMotionEvents, but it didn't help.
The question
How do you do this?
Also, what should be done in case the layout is inside a listView (as an item within it) ?
Consider you have a spinner inside a linear layout. Make focusable property of view(here that is spinner) to false. And onClick of linear layout call performClick on view(spinner in this case).
EDIT:
In focus Change Listener of linear layout if it has focus, call requestFocus on view.
You can follow same method to other states of view
Another method:
do not apply any selector to layout(linearlayout in this case) and add whatever selector you want to view(spinner in this case). For view add this parameter
android:duplicateParentState="true"
I have layout containing 30 fields ( using editbox, spinners, buttons etc)
but it looks too weird with crowded elements. ( scroll view is also longer)
So i decided to keep 1 more button, so firstly 15 fields will display and 1 "More" button below,now i want to display rest of fields onClick "more" button on same layout ( below that 15 fields). How to do this, thanks in advance
I would suggest you to make a parent view with two main relative layouts. First relative layout for all the content you want to show at first time. Make this layout visibility View.VISIBLE. Second relative layout for all the content you want to show on click event. Make this layout visibility View.INVISIBLE.
On button click event make second relative layout visible by setting view.setVisibility(View.VISIBLE)
Don't forget the invisible the layout in the end. Otherwise it will be visible throughout the app session.
You can also set this property in your xml too. There is a android:visibility="visible" tag in every view.
This approach will help you in management because by setting a single view visibility you can manage your all view. Happy Coding!!
I have 3 Buttons, 3 Scroll Views, and some Textviews and pictures and what not.
The 3 Buttons I want to be able to show a different Scroll View when they are clicked.
So clicking Button #1 would show Scroll View #1 and hide Scroll View #2 & #3
etc....
Is there a way to do this programmatically? Like in an OnClick() Event?
you can use the setVisiblility() method:
mScrollView.setVisibility(View.GONE);
by setting View.GONE you'll view will become invisible, and will not take any actual space inside the layout container it reside in.
then when you'll want to make it visible again, use the:
mScrollView.setVisibility(View.VISIBLE);
Use ExpandableListview For that
Refer this Links this will help to Implement ExpandableListview in android
http://androidword.blogspot.in/2012/01/how-to-use-expandablelistview.html
http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.html
http://android-adda.blogspot.in/2011/06/custom-expandable-listview.html
In my app I want to have a button that if the user clicks it
than a new layout is opened within the current (acually the main) layout.
the new layout should not fill all of the screen and parts of the previous layout
should be grayed out.
Any ideas on how to do this ?
You can show a hidden layout within your button's onClick event by calling
view.setVisibility(View.VISIBLE)
You can also fade out view elements or whole views with
view.setAlpha(75);
view.setBackgroundColor(Color.GRAY);
Note that "view" in the first example is your layout element.. LinearLayout, RelativeLayout, etc. and in the 2nd example, "view" is the element(s) you're trying to gray out.
Follow the answer of SBerg413. And for more information. you can take the relativelayout for the part that you want to hide and display on the button click.
And as like SBerg413 answer. you can hide the respective layout and show the layout you want to display.
Hope it will help you.
Thanks.
you can use a ViewFlipper to achieve what you want, position the viewflipper where the child views should fit (part of the screen you say)..
Inflate the rest of the "child" layouts from other xml, add them to the flipper and switch between them when you want...