简体   繁体   中英

How to permanently remove a linearlayout?

In this program, I want to delete ll2 (LinearLayout) when mButton is pressed. Ie I don't want this layout to appear the second time I enter this activity. When I push the button, the layout is gone for as long as I am in the activity, but when I come back into the activity, the layout is there.

How do I permanently delete it? Thanks in advance!

LinearLayout ll,ll2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    location_btn = (Button)findViewById(R.id.location_btn);
    menu_btn = (Button)findViewById(R.id.bt_menu);
    mButton = (Button) findViewById(R.id.buttone);
    mEdit = (EditText) findViewById(R.id.edittexte);
    ll2 = (LinearLayout)findViewById(R.id.llayout);

    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            number = mEdit.getText().toString();
            mEdit.setText("");
            ll2.setVisibility(View.GONE);
            ll2.removeAllViewsInLayout();
        }
    });
}

My layout file

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/llayout"
    android:visibility="visible">

    <EditText
        android:id="@+id/edittexte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SAVE"
        android:id="@+id/buttone"/>

</LinearLayout>

You can try saving a boolean in SharedPreferences ... For eg Keep the boolean as false in the beginning.

As soon as you press the button, remove the View (LinearLayout), change the boolean to true & save it to SharedPreferences...

In the onCreate(), try as

if(booleanisTrue) {
   ll2.setVisibility(View.GONE);
   ll2.removeAllViewsInLayout();
 }

Just keep a SharedPreference and save the state that you have pressed the button earlier. Then each time you enter the activity, check the value stored in your SharedPreference and if its found that the button is already pressed before, just hide the LinearLayout .

LinearLayout ll,ll2;
SharedPreference pref; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    pref = getSharedPreferences("MyApplication", Activity.MODE_PRIVATE);

    location_btn = (Button)findViewById(R.id.location_btn);
    menu_btn = (Button)findViewById(R.id.bt_menu);
    mButton = (Button) findViewById(R.id.buttone);
    mEdit = (EditText) findViewById(R.id.edittexte);
    ll2 = (LinearLayout)findViewById(R.id.llayout);

    // Check the preference value when activity is launched each time and hide of the button was pressed before
    if(pref.getBoolean("ButtonPressed", false)) ll2.setVisibility(View.GONE);

    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            number = mEdit.getText().toString();
            mEdit.setText("");
            // Save the sate of the button pressed in the SharedPreference
            pref.edit().putBoolean("ButtonPressed", true).apply();

            ll2.setVisibility(View.GONE);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM