Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore View to original xml state

Tags:

android

view

In Android, is there an easy way to restore a view to the state as defined in the xml?

For example, I have this view in xml:

<EditText
    android:id="@+id/set_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:ems="10"
    android:visibility="gone" >
</EditText>

Now programmatically, the visibility is set to visible, the user enters text into it, an error gets set with .setError, and now I want to simply revert the view to it's original state, without having to explicitly do:

 view.setVisibility(View.GONE);
 view.setText("");
 view.setError(null);
like image 705
wvdz Avatar asked Jun 24 '14 09:06

wvdz


People also ask

Is XML used in Android studio?

Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

What is XML layout?

XML-Based Layouts in Android In Android, an XML-based layout is a file that defines the different widgets to be used in the UI and the relations between those widgets and their containers. Android treats the layout files as resources. Hence the layouts are kept in the folder reslayout.

What is the extension of layout files in Android?

Android applications use XML to create layout files. Unlike HTML, XML is case-sensitive, requires each tag be closed, and preserves whitespace. View: An object from Android's built-in View class.

What is the layout of Android apps?

Android Layout is used to define the user interface that holds the UI controls or widgets that will appear on the screen of an android application or activity screen. Generally, every application is a combination of View and ViewGroup.


1 Answers

I don't know if you will consider this to be an easy way but I tried the following.

Define the View you want to revert in its own xml ( I tried with a Button, playing with visibilty, text, ... )

button.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adminUser2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:text="@string/admin_user" />

Then in your main layout define an insertion point

<LinearLayout
    android:id="@+id/buttonInsertLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" 
/>

In your activity load the View into the insertion point using the inflater

private Button admin;

@Override
protected void onResume() {
    super.onResume();
    admin = (Button)View.inflate(UserLoggin.this, R.layout.button, null);
    ViewGroup insert = (ViewGroup)findViewById(R.id.buttonInsertLayout);
    insert.addView(admin);
    // ...
}

Later on your reset() method you can inflate a new instance of the view, based on the xml content, and replace the modified one with the new one.

public void resetView() {
    ViewGroup parent = (ViewGroup)admin.getParent();
    int index = parent.indexOfChild(admin);
    parent.removeViewAt(index);
    // Inflate a fresh new View corresponding to the initial state of the view
    Button adminSave = (Button)View.inflate(UserLoggin.this, R.layout.button, null);
    parent.addView(adminSave, index);
}

Hope it helps...

like image 195
Guillaume Barré Avatar answered Oct 21 '22 00:10

Guillaume Barré