How to hide a View programmatically? - java

In my application, I have 2 LinearLayout's right above each other. Via a menu option, I want to be able to make the bottom one disappear, and have the top one drop down over the disappeared LinearLayout.
The problem is, I have no idea on how to do this in Java.
It doesn't have to be animated, I want to hide the Layout on return of another activity (the menu), in OnActivityResult. The menu activity sets a boolean on which I check in OnActivityResult, and according to it's value I determine if I need to hide or show the bottom Layout:
// Only change value if it is different from what it was.
if(mUseVolumeButtonAsPTT != resultData.getBoolean("UseVolumeButtonAsPTT")){
mUseVolumeButtonAsPTT = resultData.getBoolean("UseVolumeButtonAsPTT");
if(!mUseVolumeButtonAsPTT){
// Hide lower LinearLayout.
} else {
// Show lower LinearLayout.
}
}
Can anybody give me a hint or a link on how I should do this?

You can call view.setVisibility(View.GONE) if you want to remove it from the layout.
Or view.setVisibility(View.INVISIBLE) if you just want to hide it.
From Android Docs:
INVISIBLE
This view is invisible, but it still takes up space for layout purposes. Use with setVisibility(int) and android:visibility.
GONE
This view is invisible, and it doesn't take any space for layout purposes. Use with setVisibility(int) and android:visibility.

Try this:
linearLayout.setVisibility(View.GONE);

Kotlin Solution
view.isVisible = true
view.isInvisible = true
view.isGone = true
// For these to work, you need to use androidx and import:
import androidx.core.view.isVisible // or isInvisible/isGone
Kotlin Extension Solution
If you'd like them to be more consistent length, work for nullable views, and lower the chance of writing the wrong boolean, try using these custom extensions:
// Example
view.hide()
fun View?.show() {
if (this == null) return
if (!isVisible) isVisible = true
}
fun View?.hide() {
if (this == null) return
if (!isInvisible) isInvisible = true
}
fun View?.gone() {
if (this == null) return
if (!isGone) isGone = true
}
To make conditional visibility simple, also add these:
fun View?.show(visible: Boolean) {
if (visible) show() else gone()
}
fun View?.hide(hide: Boolean) {
if (hide) hide() else show()
}
fun View?.gone(gone: Boolean = true) {
if (gone) gone() else show()
}

Related

Programmatically Change Status Bar Text Color in Android 11 (API 30)

I am currently able to update the status bar text color from light to dark using the following inside my base activity:
private fun toggleStatusBarTextColor(light: Boolean) {
// clear any existing flags
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
if(light) {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
} else {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
}
}
systemUiVisibility is now showing deprecated on API 30, and although the deprecated methods will still function for the time being, I would prefer to replace them with the newer way to accomplish this. I have read that we should now use the WindowInsetsController functions, but it is not clear to be how to accomplish this from the docs. Can someone point me in the right direction?
For API 30 you can use WindowInsetsController.setSystemBarsAppearance (int appearance, int mask):
To make status bar light:
window.insetsController?.setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
To clear the flag:
window.insetsController?.setSystemBarsAppearance(
0,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)
Note that getInsetsController is nullable hence the ? check.
Alternatively (and for lower APIs) you can use WindowInsetControllerCompat:
val windowInsetController = ViewCompat.getWindowInsetsController(window.decorView)
windowInsetController?.isAppearanceLightStatusBars = true // or false
Note: if clearing flag doesn't work check value of window.decorView.windowSystemUiVisibility - if it contains View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR that means your view hierarchy contains a View with this flag which is propagated and affects systemUiVisibility calculation.
I, as others, couldn't get the new API that #Pawel recommended to work on all Android OS versions. I unfortunately found I had to use both the older API as well as the new to get it working on Android 11 & below:
fun setStatusBarLightText(window: Window, isLight: Boolean) {
setStatusBarLightTextOldApi(window, isLight)
setStatusBarLightTextNewApi(window, isLight)
}
private fun setStatusBarLightTextOldApi(window: Window, isLight: Boolean) {
val decorView = window.decorView
decorView.systemUiVisibility =
if (isLight) {
decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
} else {
decorView.systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
private fun setStatusBarLightTextNewApi(window: Window, isLightText: Boolean) {
ViewCompat.getWindowInsetsController(window.decorView)?.apply {
// Light text == dark status bar
isAppearanceLightStatusBars = !isLightText
}
}

Java Android Badge Drawable not working properly after configuration change

I have a Bottom Navigation view with a badge drawable that shows new chats.
This badges are updated upon a listener to Firebase database, where I store the notification counter. When this value change, the badge is updated. Moreover if the counter is equal to zero, the badge is set not visible.
Everything works fine except if I change some configuration using the device Settings (such as language or removing permissions). In fact, if I do that and go back to the app, the activity is re-created (sometimes without destroying it) and badge reloaded. But the setVisibility seems not working. Even if the counter is zero the badge is visible. Plus is not update anymore when the listener is triggered.
The code works, I checked with some logs if the listener is triggered and if the lines which include setVisibility are run. It just seems to have random behaviour.
If the activity is destroyed and recreated again, it works.
Any help will be appreciated!
this is how I initialize the badge
bottomNav = findViewById(R.id.bottom_navigation);
badge_chat = bottomNav.getOrCreateBadge(R.id.nav_chat);
badge_chat.setVisible(false);
this is the listener code
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
int badge_counter = dataSnapshot.getValue(int.class);
boolean visibility;
if (badge_counter == 0) {
visibility = false;
} else {
visibility = true;
}
badge_chat.setNumber(badge_counter);
badge_chat.setVisible(visibility);
}
One way I've managed to resolve this is to create/remove badge whenever it needs to be visible/hidden. In your case, something like this should work:
if (badge_counter == 0) {
getBadge(R.id.nav_chat)?.isVisible = false
removeBadge(R.id.nav_chat)
} else {
getOrCreateBadge(R.id.nav_chat).apply{
isVisible = true
number = badge_counter
}
}
note: the answer is in Kotlin.

How to Highlight Single Row or Item of Recycler View and Scroll the Highlighted Row to Top of screen

I want to Highlight a single row of recyclerview with sound one after another and scroll the highlight row to top of screen This is what i have done:
Here its code:
fun AnimateToSurahAlFeel(recyclerView: RecyclerView, layoutManager: LinearLayoutManager, currentPosition: Int) {
var position: Int = currentPosition / 1000
when (position) {
0 -> {
recyclerView.smoothScrollToPosition(0)
recyclerView.getChildAt(0).isSelected = true
}
4 -> {
recyclerView.smoothScrollToPosition(1)
recyclerView.getChildAt(0).isSelected = false
recyclerView.getChildAt(1).isSelected = true
}
11 -> {
recyclerView.smoothScrollToPosition(2)
recyclerView.getChildAt(1).isSelected = false
recyclerView.getChildAt(2).isSelected = true
}
17 -> {
recyclerView.smoothScrollToPosition(3)
recyclerView.getChildAt(2).isSelected = false
recyclerView.getChildAt(3).isSelected = true
}
21 -> {
recyclerView.smoothScrollToPosition(4)
recyclerView.getChildAt(3).isSelected = false
recyclerView.getChildAt(4).isSelected = true
}
28 -> {
recyclerView.smoothScrollToPosition(5)
recyclerView.getChildAt(4).isSelected = false
if (recyclerView.getChildAt(5) != null)
recyclerView.getChildAt(5).isSelected = true
}
}
}
In the Function currentPosition is Media player current position
Problem in this code is:
In the Screen shot Row 4 and 5 are currently not visible,when highlighting Row 4 and 5 the App crash and giving Null Pointer Exception, according to my knowledge these two row are not yet created that's why
recyclerview.getChildAt(4) or recyclerview.getChildAt(5) return null and that cause the App crash.
Now
How to fix the App crash that recyclerview.getchildAt(4) or recyclerview.getchildAt(5) return null and also getChildAt(position) return n-1 row, so the App crash at recyclerview.getchildAt(5) will occur anyhow but i want n Row because i want to highlight all rows
How to scroll the highlighted row to position 0 (at top)
i.e. Row 0 go up from screen and Row 1 take it position and so on...
I want to achieve like this the highlighted one is at top and that will go off from screen when another row is highlighted
You need time for View to Bind. Just for ex. you can post action.
....
17 -> {
recyclerView.smoothScrollToPosition(3)
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
recyclerView.getChildAt(2).isSelected = false
recyclerView.getChildAt(3).isSelected = true
}, 500);
}
....
But I strongly recommend you use some state Collection, which will save and handle states of your running and showing Views.
This is because recycler views don't have all the views inflated, but only the visible ones. This is by design and should not be tinkered with. Personally, I think you should use the recycling functionality.
You need to make the selected state part of your model in the adapter - the items in the adapter. Let's say this is called RowItem, so in your adapter you'd have a list of RowItems for example. Aside from the text in both languages, you need to add the selected state too. Then it's just a matter of getting the list's adapter, setting the correct position to selected and deselecting the ones you want.
For example, in your adapter:
fun select(position: Int) {
data[position].selected = true
notifyItemChanged(position)
// deselect all other positions that you don't want selected
}
When you bind the view holder you could do then:
public void onBindViewHolder(ViewHolder viewHolder, int position) {
val item = data[position]
viewHolder.itemView.selected = item.selected
// take care of the rest of the views
}
data would be a list where you store your RowItems
Now you can scroll with no problem and set the item to selected. Once the view is visible in the recycler view, the adapter will set the correct state.
It's fair to say I'm guessing a bit since there's no adapter code in your question, but the idea I think it's easy to understand. You need to change the state in the adapter and let the implementation of the recycler view handle it for you. After all the purpose is to get the recycler view to recycle your views based on the models adapted by the adapter.
Remember you can always get your adapter from the recycler view itself. In the end you can do something like this:
...
0 -> {
recyclerView.smoothScrollToPosition(0)
(recyclerView.adapter as MyAdapter).select(0)
}
Here MyAdapter would be the class name of your adapter
For the scrolling part you can take a look at this

cant change key icon on softKeyboard

i am trying to learn android keyboard api.
Using the softKeyboard example, i am trying to change the key icon, i can change everything, but the icon never change.
i am doing it in the LatinKeyboardView, at the onLongPress method, using this line:
copia.icon = getResources().getDrawable(R.drawable.tecladir);
but the icon doesn't change.
Even after using
this.invalidateAllKeys();
to force redraw of the keys, the icon is still unchanged.
Complete code of onLongPress as following:
#Override
protected boolean onLongPress(Key key) {
key.icon = getResources().getDrawable(R.drawable.tecladir);
//tecladir is one image i have
key.text = "batata";
key.label = "batata";
this.invalidateAllKeys();
//default code of method
if (key.codes[0] == Keyboard.KEYCODE_CANCEL) {
getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
return true;
} else {
return super.onLongPress(key);
}
}
Am i missing something?
Ok, found the solution.
before assign a new icon, the label must be null, otherwise the icon doesn't change, the correct way is:
key.label = null;
key.icon = getResources().getDrawable(R.drawable.tecladir);

How to detect is a GWT element is visible?

I have several divs that are shown and hidden. How can I detect on a given element is it is currently visible on the page?
The element's style won't help, since it is a parent div in the DOM that is being hidden.
Its offset height and width will both be 0.
UIObject component = ...
boolean isHidden = (component.getOffsetHeight() == 0 && component.getOffsetWidth() == 0);
I have run into this issue before as well and I found the following to be the best solution:
Given an Element called "element":
boolean visible = UIObject.isVisible(element) && (element.getAbsoluteLeft() > 0) && (element.getAbsoluteTop() > 0);
The static "isVisible" method on UIObject will check for display none and that sort of thing, while the checks on the AbsoluteLeft and AbsoluteTop are there to handle detachment. The reason that I found the latter checks to be necessary was because if an element is detached from the DOM (and is hence not visible on the page) then GWT will still tell you that its visibility is true unless its visibility was explicitly set to false.
NB: You could replace the AbsoluteTop and AbsoluteLeft checks with the offset width and height checks as suggested by Simon, but you should include the isVisible check as well in my opinion.
You can have something like that:
public boolean isVisible(Widget w) {
while (w.getElement().hasParentElement()) {
if (w.isVisible()) {
return true;
}
w = w.getParent();
}
return w.isVisible();
}
If it's an Element, not a UIObject, the following worked for me:
!"hidden".equals(element.getStyle().getVisibility())
&& !"none".equals(element.getStyle().getDisplay())
I was walking down the tree, so knew the parent elements were visible; if your case is different, you'll probably need to do the same check on all parent elements.

Categories

Resources