How to hide a View programmatically?
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)
andandroid:visibility
.GONE
This view is invisible, and it doesn't take any space for layout purposes. Use with
setVisibility(int)
andandroid:visibility
.
I want to hide element/Views programmatically in android
Rather passing view id you should pass view in display method that is more convenient.
First Views Ids are generated automatically so if things you are storing this ids some Where and later used to get views it not right thing because Ids are generated and it different device to device and might change any time when application closed and start again.
You can do it by getIdentifier()
try {
String buttonID = elementId;//String name of id
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
View view = findViewById(resID);
if (isVisible) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
as above we are passing view id with combination of i and j values and then using getIdentifier() method to make Views objects .
I thing above code is solution towards your problem.
Showing and hiding a view controller programmatically
Good evening.
Based on your question i assume you're networkconnection check works as it should. I would display the banner in the following way:
create the banner using a new .swift file containing the following code:
class BannerViewLauncher: NSObject {
var viewController: UIViewController?
lazy var bannerView : UIView = {
let view = UIView(frame: .zero)
view.backgroundColor = .lightGray
return view
}()
//create more UIelements if you want to design the banner further. don't forget to add them as subviews to the bannerView and give them constraints.
func showNoNetworkBanner() {
if let window = UIApplication.shared.keyWindow {
window.addSubview(bannerView)
//define your desired size here (y position is out of view so it is off view by default, the y position will be changed when you call the showNoNetworkBanner() method.
bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100)
//change withDuration if you want it to display slower or faster, just change all of them to the same for it so look good
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
self.bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100)
}, completion: nil)
}
}
func hideNoNetworkBanner() {
if let window = UIApplication.shared.keyWindow {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100)
}, completion: nil)
}
}
}In the ViewController you would like to display this (one or more) subclass the
BannerViewLauncher
and call the method as described when your NoNetwork observer methods trigger.lazy var bannerViewLauncher : BannerViewLauncher = {
let launcher = BannerViewLauncher()
launcher.viewController = self
return launcher
}()
//call the following when you want the banner to display
bannerViewLauncher.showNoNetworkBanner()
//call this when you want to hide the banner
bannerViewLauncher.hideNoNetworkBanner()
Hope this answers your question.
Dynamically hiding view in SwiftUI
Rather than dynamically setting a variable and using it in my view, I found that I was able to hide or show the date picker this way:
struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()
var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
} else {
DatePicker($datePickerDate).hidden()
}
}
}
}
Or, optionally, not including the date picker instead of hiding it:
struct ContentView : View {
@State var showDatePicker = true
@State var datePickerDate: Date = Date()
var body: some View {
VStack {
if self.showDatePicker {
DatePicker($datePickerDate)
}
}
}
}
Android: hide an element
Two ways:
XML
in your XML file. If you want to have initial visibility
android:visibility="gone" <-- hides
or
android:visibility="visible" <-- makes it visible
Java
in java file. For when you need to change it programmatically
textView.setVisibility(View.GONE);//makes it disappear
or
textView.setVisibility(View.VISIBLE);//makes it visible again
Different visibilities:
- Visible
Says itself: Sets the view to be visible
- Invisible
Hides the view, but it still occupies space.
- Gone
Hides the view, and makes it occupy no space.
The proper way of hiding and showing Android Views?
To show/hide a View
programmatically you can use setVisibility(int visibility)
.
The visibility
parameter can be:
View.VISIBLE
- shows theView
.View.GONE
- hides theView
and recalculates the layout.View.INVISIBLE
- hides theView
, but still leaves/occupies its space in the layout.
How to show/hide grouped views in Android?
Part one and two should be in their own layout. After, play with the visilibity
property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone
for the visibility property.
Ok, here I go. Below you have a complete example of how to hide/show grouped views.
main.xml
<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" >
<LinearLayout
android:id="@+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
I hope this helps ;)
How do I show and/or hide a subview using swift
You should create IBOutlet
s for each of the three subviews. Then you can show/hide each of them directly from those references. If you hide a view, it will automatically hide its subviews.
Once you have an outlet for the view, you can do this:
viewYouWantToHide.isHidden = true
Related Topics
How to Add Mouselistener to Item on Java Swing Canvas
Java Interfaces/Implementation Naming Convention
How to Make a New List in Java
How to Use Jndi Datasource Provided by Tomcat in Spring
How to Get an Android User's Email Address
How to Programmatically Enable Gps in Android Cupcake
Androidruntime Error: Parcel: Unable to Marshal Value
Difference Between Java.Util.Random and Java.Security.Securerandom
Why Is Exception.Printstacktrace() Considered Bad Practice
Dynamically Add CSS Stylesheets in Javafx
How to Set -Fx-Max-Width to Use_Pref_Size in Javafx CSS
How to Get the Selected Index of a Radiogroup in Android
Why Do I Need Transaction in Hibernate for Read-Only Operations
Calculate Elapsed Time in Java/Groovy
Java.Net.Socketexception: Socket Failed: Eperm (Operation Not Permitted)