I have an array of buttons that I want to hide using .visibility = View.INVISIBLE
But this doesn't work for an array, any guesses on how I could achieve this?
You should try View.Gone instead of View.INVISIBLE
buttonArray.forEach{
it.visibility=View.GONE
}
Related
I have created an app in which I am fetching data from server. So, I have used JSON parsing in my code. I am successfully parsed my JSON from server. Now I want to set buttons in layout according to their visibility getting from JSON response.
That is if visibility true then button should visible and if false button should disable. It works but if some button has visibility false then there is a space in layout. So how to set in layout dynamically that all visible buttons show in line.
I have created three linear layouts each contains three buttons.Now if json response says visibility false for some button I set visibility to off for that button.
if (Menuname.equals("StudentDailyDiary")) {
studentdiary.setVisibility(View.VISIBLE);
moreOptionLayout.setVisibility(View.VISIBLE);
hrview.setVisibility(View.VISIBLE);
rlleftview.setVisibility(View.VISIBLE);
rlrightview.setVisibility(View.VISIBLE);
}
if (Menuname.equals("BookSearch")) {
studlibrary.setVisibility(View.VISIBLE);
moreOptionLayout.setVisibility(View.VISIBLE);
hrview.setVisibility(View.VISIBLE);
rlleftview.setVisibility(View.VISIBLE);
rlrightview.setVisibility(View.VISIBLE);
}
Say in first layout three buttons are there from which second button has visibility false then on position of second button third button should be placed there should be no blank space for buttons in all layout.That is if third button has visibility false then first button of second layout should set on position of third button.
I think that the correct ViewGroup to use is the GridLayout.
Here a simple example:
activity_main.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" />
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val numbers = arrayOf("BUTTON1", "BUTTON2", "BUTTON4", "BUTTON5", "BUTTON6")
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers)
gridView.adapter = adapter
gridView.onItemClickListener = AdapterView.OnItemClickListener {
parent, v, position, id ->
Toast.makeText(applicationContext, (v as TextView).text, Toast.LENGTH_SHORT).show()
}
}
}
This example uses String but you can make an Adapter of whatever you want. Now after you JSON response just fill your array and set it to the adapter.
Hope it helps
there are three options for elements visibility,
View.VISIBLE
View.INVISIBLE
View.GONE
use View.GONE for elements that you don't want to show.
the different between
View.INVISIBLE
and
View.GONE
is that View.GONE take no space and you should use view.GONE
You have two option to do this
1:-
View.VISIBLE //Visible the view which is acquire its space
View.INVISIBLE //Invisible the view and it acquire the space
View.GONE //visibility Gone of its view and also not acquire view space
2:-
you can use RecyclerView and pass the model to adapter and then show buttons according to its model.
Set your buttons Visibility to Gone in your layout XML
So they don't left any blank space while their Visibility are INVISIBLE
<Button
android:id="#+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
Is there a way to programmatically make the visibility of an adview == false?
e.g. in XML I am able to do this;
android:visibility="invisible"
However, I want to achieve this in Java - I have tried this but it doesn't seem to work;
adView.getVisibility().set(false);
adView.setVisibility(false);
adView.setVisibility("invisible");
You should use View.INVISIBLE
adView.setVisibility(View.INVISIBLE);
INVISIBLE
This will just hide the view.
adview.setVisibity(View.INVISIBLE);
GONE
This will completely hide the view from ParentLayout
adview.setVisibity(View.GONE);
You can call adView.setVisibility(View.GONE) if you want to remove it from the layout.
Or adView.setVisibility(View.INVISIBLE) if you just want to hide it.
I know loads of articles describe how to animate the expansion/collapse of a listView item but I simply cannot get it working :-(
I have a listView where each item contains a hidden LinearLayout element ll (View.GONE) and I would like to expand/collapse ll using an animation. My problem is - apparently - the variable height of ll. ll's height is set to wrap_content as it contains a description and some images.
The challenge as I see it it the fact that I start out with ll's viibility set to View.GONE. If I change it to View.VISIBLE before starting the animation the area expands instantly and the text animates down. If I change visibility after the animation I don't get the animation effect.
Here's a snippet from my onItemClickListener:
LinearLayout llMatchInfo = (LinearLayout) view.findViewById(R.id.matchInfo);
//llMatchInfo.setVisibility(llMatchInfo.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
Animation animation = AnimationUtils.loadAnimation(ctx, (llMatchInfo.getHeight() == 0) ? R.anim.down_from_top : R.anim.up_from_bottom);
llMatchInfo.startAnimation(animation);
Can anyone shed some light as to how to accomplish animating an element with height wrap_content and initial visibility View.GONE?
I'm really sorry if I've missed an obvious answer but I just feel like I've tried everything... :-/
Thanks in advance
You can use this library:
https://github.com/traex/ExpandableLayout
It can save your life and time.
It's easy, you need only add the custom listview on your layout, create the adapter with customs implementations, setAdapter and voalá.
I just tried to help...
i'm using following code to set "visibility=gone" for a linear layout
//onCreate method
//setcontentview
. . . .
LinearLayout rlayout1 = (LinearLayout) findViewById(R.id.readerBottomLayout);
rlayout1.setVisibility(2);
But the controls are still visible when activity runs.why? any idea?
set rlayout1.setVisibility(View.INVISIBLE)
Yes, view.GONE and view.INVISIBLE will work. The reason it didn't work before is because two (2) is the incorrect integer value.
The correct values for set.Visibility are:
0 = visible
4 = invisible
8 = gone
you can also do something like this:
Get Parent layout object through its id
Get layout which you want to remove/hide through its id
parentlayout.remove(childlayout);
I have a ListView with few items. Is it possible to change size of the selected item in the ListView. For example, I want to expand selected item to show some buttons.
I appreciate you for help in advance.
Set the visibility of these buttons as View.GONE in the items that you don't want it. Whenever you need them set them as View.VISIBLE. By setting them as View.GONE android doesn't include them in any kind of measurement (width or height) and it looks like nothing was supposed to be there.
btn = (Button) findViewById(R.id.btn);
if(condition){
btn.setVisibility(View.VISIBLE);
btn.setText("You can see me!");
} else {
btn.setVisibility(View.GONE);
// btn.setText("Now you can't!");
}