Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically hide android some, but not all, view items in an XML output?

I'm trying to program a disc golf scoring app on Eclipse for my android phone. I'd like to set it up for up to 6 players, but mostly 2 people will use it for a game. The data is being stored in a sqlite DB, and I am using a SimpleCursorAdapter to populate the data for holes that have already been scored. here is that code:

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)

    String[] from = new String[]{DiscGolfDbAdapter.KEY_HOLE,
            DiscGolfDbAdapter.KEY_PAR,
            DiscGolfDbAdapter.KEY_TOM_HOLE,
            DiscGolfDbAdapter.KEY_TOM_GAME,
            DiscGolfDbAdapter.KEY_CRAIG_HOLE,
            DiscGolfDbAdapter.KEY_CRAIG_GAME,
            DiscGolfDbAdapter.KEY_TOMS_POSITION,
            DiscGolfDbAdapter.KEY_SKIP_PLAYER
    };

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.schole, R.id.scpar, R.id.scth, R.id.sctg, R.id.scch, R.id.sccg, R.id.sctp,
            R.id.skip};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.hole_info, notesCursor, from, to);
    setListAdapter(notes);
}

From searching the internet I've found what I think are two posibilities that SHOULD work, but do not.

First I've tried the XML Attribute: android.visibility. It looks like this in the PORTION of the view that I am trying to "test" hide:

<LinearLayout android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.visibility="GONE">
    <TextView android:id="@+id/scch"
        android:layout_width="45dip"
        android:gravity="right"
        android:textSize="25sp"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/sccg"
        android:layout_width="45dip"
        android:gravity="right"
        android:textSize="25sp"
        android:layout_height="wrap_content"/>
</LinearLayout>

I have tried it with "GONE", "Gone" and "gone". NONE of them work in the eclipse emulator OR on my actual phone. So, there is no point in trying to parameterize this attribute.

Next I've tried setting the XML attribute for android:layout_height to "0dip". This indeed works in the emulator and on my phone WHEN IT IS HARDCODED.

Then I moved to the next logical step (as I see it), storing a parameter in the DB so that I can "show" or "not show" the item DEPENDING on conditions within the record. So, I've stored a field in the DB with two values "0dip" and "wrap_content". I pass these to the layout as shown in the java above as R.id.skip. I've also added these to the output just to audit that they are really there. Here is that XML:

<LinearLayout android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@+id/skip">
    <TextView android:id="@+id/scch"
        android:layout_width="45dip"
        android:gravity="right"
        android:textSize="25sp"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/sccg"
        android:layout_width="45dip"
        android:gravity="right"
        android:textSize="25sp"
        android:layout_height="wrap_content"/>
     </LinearLayout>

     <TextView android:id="@+id/skip"
    android:gravity="center"
    android:layout_width="315dip"
    android:textSize="10sp"
    android:layout_height="wrap_content"/>

In the above test, both via the Eclipse emulator and my android phone, the last TextView confirms that the DB contains either "0dip" or "wrap_content", BUT the LinearLayout with:

      android:layout_height="@+id/skip">

behaves as if it were "0dip" ALL of the TIME. In other words, I cannot PROGRAMMATICALLY" affect the XML attribute for android:layout_height.

If there is a better/more standard way of accomplishing what I am trying to do, please share - BUT BE CLEAR. I am new, so CODE EXAMPLES wwill work best for me.


May 29th - It seems to me (based on testing) that you cannot alter layout attributes for the layout specified in this code:

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, 
                                                    R.layout.hole_info, 
                                                    notesCursor, from, to);
setListAdapter(notes);

Anything I try leads to some error ort another. So, I've seen examples of custom list adapters where these attributes are altered, so I'm trying to convert to a custom list adapter.

like image 388
Tom Wruble Avatar asked Dec 05 '22 22:12

Tom Wruble


1 Answers

Why not do it in code?

LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
ll.setVisibility(View.GONE);
like image 140
Gabriel Negut Avatar answered Feb 15 '23 09:02

Gabriel Negut