2

I have a listView in my activity_main.xml . I used a layout(list_layout) for my listview's row. list_layout contains 3 textView. I added a activity called "Setting" into my Mainactivity. I want change visibility of list_layout's 3. textView from settin.java with a button.

I mean when I click button (button code is into setting.java(button is into activity_setting.xml)) list_layout's 3.textview must invisible.

This is from activity_main.xml

    <ListView
        android:id="@+id/listem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

This is list_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
   <TextView
.../>
   <TextView
.../>
   <TextView
        android:id="@+id/turkish_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:visibility="visible"/>

</LinearLayout>

MainActivity.Java

     ...    listview = (ListView) findViewById(R.id.listem);
DataHelper.Database data = new DataHelper.Database(MainActivity.this);
            ArrayList<HashMap<String, String>> Liste = data.Listele();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, Liste, R.layout.list_layout, new String[]{"id", "title", "subtitle"}, new int[]{R.id.kelime_id, R.id.english_id, R.id.turkish_id});
            listview.setAdapter(adapter);
      ...
     public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {

                case R.id.settings:
                    Intent intent = new Intent(MainActivity.this, Setting.class);
                    startActivity(intent);
                    break;  ...

//Setting.Java

public class Setting extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }

    public void click(View view) {//<-----Here is my button's code
        textView=(TextView)view.findViewById(R.id.turkish_id);
        textView.setVisibility(View.INVISIBLE);
    }
}

activity_setting.xml

 <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY BUTTON"
    android:onClick="click"/>
Zoe
  • 23,712
  • 16
  • 99
  • 132
  • probably because you are searching R.id.turkish_id inside the wrong view, the right view is the one inside the adapter, refer to this link on how to find the right view by position https://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position, Another way to go would also be to extend the adapter so that each row of listview would change visibility based on data provided, you could change data at runtime and call notifyDataSetChanged(); to refresh the listviewitem visibility. – S.Bozzoni Apr 14 '19 at 08:07
  • Thank you again but I want hide a column not row. Every row has 3 textview. If I hide a textview of my row(list_layout. xml) I can hide a column of my listview. – Hacettepe Hesabı Apr 15 '19 at 21:41
  • Probably I must use Sharedpreference for settings. But I don't develop this project furter more. Newermind – Hacettepe Hesabı May 27 '19 at 11:27

1 Answers1

2

There are different methods for adding a click listener to a button , refer to this link: add onclick listener to predefined button?

in your case you could implements in your activity the interface OnClickListener

public class Setting extends AppCompatActivity implements OnClickListener

then you should rename your method click to onClick

and in onCreate of your activity you should add the line

findViewById(R.id.yourIdButton).setOnClickListener(this);

don't forget to give an id to your button

<Button
    android:"@+id/yourIdButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MY BUTTON"
    android:onClick="click"/>

you can also remove completely the TextView from layout using "View.GONE" in place of "View.INVISIBLE"

S.Bozzoni
  • 860
  • 8
  • 15