Android: Why Setvisibility(View.Gone); or Setvisibility(View.Invisible); Do Not Work

Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

I see quite a few things wrong. For starters, you don't have your magic button defined and there is no event handler for it.

Also you shouldn't use:

dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);

Use only one of the two. From Android documentation:

View.GONE This view is invisible, and it doesn't take any space for
layout purposes.

View.INVISIBLE This view is invisible, but it still
takes up space for layout purposes.

In your example, you are overriding the View.GONE assignment with the View.INVISIBLE one.


Try replacing:

final DatePicker dp2 = new DatePicker(this)

with:

DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);  

Similarly for other widgets:

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

final DatePicker dp2 = new DatePicker(this);
final Button btn2 = new Button(this);
final Button magicButton = new Button(this);
final TextView txt2 = new TextView(TestActivity.this);

dp2.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
btn2.setText("set Date");

btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
txt2.setText("You selected "
+ dp2.getDayOfMonth() + "/" + (dp2.getMonth() + 1)
+ "/" + dp2.getYear());
}
});

magicButton.setText("Magic Button");
magicButton.setOnClickListener(new View.OnClickListener()
public void onClick(View arg0) {
dp2.setVisibility(View.VISIBLE);
btn2.setVisibility(View.VISIBLE);
}
});

ll.addView(dp2);
ll.addView(btn2);
ll.addView(magicButton);
ll.addView(txt2);

setContentView(ll);
}

setVisibility(View.GONE) not working for textview

v=(TextView)findViewById(R.id.fax);
v1=(TextView)findViewById(R.id.tit_fax);

keep this in globally then its work,no need to use View.VISIBLE,Keep the variable unique

Don't use this in your XML,because it get confused

android:visibility="visible"

How to recover view from View.gone. setVisibility(View.VISIBLE) not working after using 'android:visibility=gone' in xml

1.View Visibility not working

The Visibility is not working because the view is not rendered initially. Remove the visibility gone in the xml and handle the visibility fully in the adapter class. In the 'assess_list_layout' linerlayout height can be hardcode because inside this layout the listview height is already hardcoded. You can hardcode to 300 and check. This way will help the view to get the initial rendering.

2. Scroll issue

While scrolling the already visible 'assess_list_layout' view might be not visible. This is because we need to handle the visibility, this handling is similar to checkbox selection handling in listview. Hope the Course class is model class, in that add a another property named isSelected as boolean and set the default value to false. Please refer below the course class,

Course Class

public class Course {
private boolean isSelected = false;

public boolean isSelected() {
return isSelected;
}

public void setSelected(boolean selected) {
isSelected = selected;
}

}

please refer the below code changes in the adapter class.

    public class CourseListAdapter extends ArrayAdapter<Course> {
private static final String TAG = "CourseListAdapter";

private Context context;
int mResource;

public CourseListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Course> objects) {
super(context, resource, objects);
this.context = context;
mResource = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// get info
String course_code = getItem(position).getCourseCode();
Double course_grade = getItem(position).getCurrentGrade();

// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);


TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);

//My Change
// expand the view to include a new fragment
LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);

// get the list view and add each course to the course view
ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);

assess_list_layout.setVisibility(View.GONE);

if (getItem().get(position).isSelected()) {
assess_list_layout.setVisibility(View.Visible);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
//My Change

tv_course_code.setText(course_code);
tv_course_title.setText(String.valueOf(course_grade));

// add on click to each list view element
LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "List view element has been clicked " + course_code);

//My Change
getItem().get(position).setSelected(true);
//My Change
assess_list_layout.setVisibility(View.VISIBLE);


AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
});

return v;
}
}

I have commented as My Change to find the difference in the code.

Android setVisibility View.INVISIBLE and View.GONE aren't working

I think you are trying to show/hide the textviews based on whether the related json data is available.
As for your current code:

infoHolder.yelp = (TextView) convertView.findViewById(R.id.theYelp);

if(infoHolder.yelp != null){
...
}

The if statement you declared is just checking whether the textview is null, it will never be null since its assigned a textview in the beginning. You should first determine whether the json data exists before deciding whether to show/hide the textview.

For example:

JSONObject jsonObject = this.dataArray.getJSONObject(position);
String yelpStr = jsonObject.optString("yelp");
if(yelpStr != null){
infoHolder.yelp.setText(yelpStr);
infoHolder.yelp.setVisibility(View.VISIBLE);
Toast.makeText(activity, "Yelp Here", Toast.LENGTH_LONG).show();
}else{
infoHolder.yelp.setVisibility(View.GONE);
Toast.makeText(activity, "Yelp Gone", Toast.LENGTH_LONG).show();
}

Also, I believe you misplaced a right curly brace, it should be right after infoHolder is retrieved using the tag:

if(convertView == null){
...
}else{
infoHolder = (allTheDealershipInfo) convertView.getTag();
}

Make sure u end the if/else statement for assigning infoHolder properly so the textviews will be used in the rest of the code as intended.

setVisibility(View.INVISIBLE) not working in MapFragment

I find a way to workaround. I am setting Linearlayout constraint to out of the screen so it is no more visible and to get back setting default constrain programmatically.

The thing I did is add set constraint to app:layout_constraintBottom_toTopOf="parent" programmatically when click on expand button.(When it expanding)

Then set the default constraint and margin programmatically when it collapses.

  app:layout_constraintBottom_toBottomOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="@+id/linearLayout"

Here is the code for the solution

case R.id.expandMapBtn:
mVib.vibrate(50);
storageAccess.logMessage("Expand button click");
expandMapBtn.startAnimation(myAnim);
constraintMap.clone(constraintLayout);
constraintMap.clear(R.id.map, ConstraintSet.START);
constraintMap.clear(R.id.map, ConstraintSet.TOP);
constraintMap.applyTo(constraintLayout);
constraintMap.connect(R.id.map, ConstraintSet.START, R.id.main_fragment_constraint_layout, ConstraintSet.START);
constraintMap.connect(R.id.map, ConstraintSet.TOP, R.id.main_fragment_constraint_layout, ConstraintSet.TOP);

constraintMap.setAlpha(R.id.linearLayoutRoamIOLatLong,100);
System.out.println(isViewExpanded());

linearLayoutRoamIOLatLong = findViewById(R.id.linearLayoutRoamIOLatLong);

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

if (!isMapExpanded()) {
constraintMap.constrainHeight(R.id.map, height);
constraintMap.constrainWidth(R.id.map, width);
constraintMap.connect(R.id.map, ConstraintSet.START, R.id.main_fragment_constraint_layout, ConstraintSet.START);
constraintMap.connect(R.id.map, ConstraintSet.TOP, R.id.main_fragment_constraint_layout, ConstraintSet.TOP);
constraintMap.setMargin(R.id.map, ConstraintSet.START, 0);
constraintMap.setMargin(R.id.map, ConstraintSet.TOP, 150);
constraintMap.connect(R.id.linearLayoutRoamIOLatLong, ConstraintSet.BOTTOM, R.id.main_fragment_constraint_layout, ConstraintSet.TOP);

linearLayoutRoamIOLatLong.setVisibility(View.INVISIBLE);
leftFragHolder.setVisibility(View.INVISIBLE);

storageAccess.logMessage("Map Layout Expanded");
setMapExpanded(true);
System.out.println("Map Fragment Expanded");
} else {
constraintMap.constrainHeight(R.id.map, 850);
constraintMap.constrainWidth(R.id.map, 890);
constraintMap.connect(R.id.map, ConstraintSet.START, R.id.main_fragment_constraint_layout, ConstraintSet.START);
constraintMap.connect(R.id.map, ConstraintSet.TOP, R.id.main_fragment_constraint_layout, ConstraintSet.TOP);
constraintMap.connect(R.id.linearLayoutRoamIOLatLong, ConstraintSet.LEFT, R.id.main_fragment_constraint_layout, ConstraintSet.LEFT);
constraintMap.connect(R.id.linearLayoutRoamIOLatLong, ConstraintSet.BOTTOM, R.id.main_fragment_constraint_layout, ConstraintSet.BOTTOM);
constraintMap.setMargin(R.id.map, ConstraintSet.START, 24);
constraintMap.setMargin(R.id.map, ConstraintSet.TOP, 150);
// constraintMap.setMargin(R.id.linearLayoutRoamIOLatLong, ConstraintSet.TOP, 75);
constraintMap.setMargin(R.id.linearLayoutRoamIOLatLong, ConstraintSet.BOTTOM, 25);
constraintMap.setMargin(R.id.linearLayoutRoamIOLatLong, ConstraintSet.START, 24);

linearLayoutRoamIOLatLong.setVisibility(View.VISIBLE);

leftFragHolder.setVisibility(View.VISIBLE);

storageAccess.logMessage("Map Layout Collapse");
setMapExpanded(false);
System.out.println("Map Fragment Collapse");
}

constraintMap.applyTo(constraintLayout);

break;


Related Topics



Leave a reply



Submit