Question

I'm trying to create a page with an edit mode, what i mean by this, is that you can click on a button and all textview transforms into edittext. So i created a custom view for this with view switcher :

custom view :

public class EditabeText extends ViewSwitcher{

private Context m_context;
private View m_view;
private TextView tv;
private EditText edit;
public EditabeText(Context context, AttributeSet attrs) {
    super(context, attrs);
    m_context = context;
    lauch();
}
public EditabeText(Context context) {
    super(context);
    m_context = context;
    lauch();
}
public void lauch(){
    inflate();
    bind();
}
public void inflate(){
    LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    m_view = inflater.inflate(R.layout.editable_text_view, this);
}
public void bind(){
    tv = (TextView) m_view.findViewById(R.id.tv_editable_text);
    edit = (EditText) m_view.findViewById(R.id.edit_editable_text);
}
public void init(){

}
public void showEditText(){
    if(this.getCurrentView() == tv){
        this.showNext();
        Toast.makeText(m_context, "showing ... edit mode", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(m_context, "already in tv mode", Toast.LENGTH_SHORT).show();
    }
}
public void showTextView(){
    if(this.getCurrentView() == edit){
        this.showPrevious();
        Toast.makeText(m_context, "showing ... tv mode", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(m_context, "already in edit mode", Toast.LENGTH_SHORT).show();
    }
}

}

And here's the xml :

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_editable_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Text" />

<EditText
    android:id="@+id/edit_editable_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:hint="Enter text" />

</ViewSwitcher>

And here's my main activity :

public class Test2 extends Activity {
EditabeText edit_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    LinearLayout ll_layout = new LinearLayout(getApplicationContext());
    //
    edit_tv = new EditabeText(getApplicationContext());
    //
    Button button = new Button(getApplicationContext());
    button.setText("change");
    button.setOnClickListener(listener);
    //
    ll_layout.addView(edit_tv);
    ll_layout.addView(button);
    //
    setContentView(ll_layout);
}
public OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show();
        edit_tv.showEditText();
    }
};
}

But it says "already in tv mode" while the TextView is the first view inside the view switcher.

Was it helpful?

Solution

Don't use in the layout file(R.layout.editable_text_view is what you posted I guess) another ViewSwitcher, because this will make you have a ViewSwitcher(EditabeText class) which has a single child view, another ViewSwitcher which has two child views(a TextView and an EditText). In this case, showing the TextView or the EditText will always fail because this.getCurrentView() == tv(or == edit) will match the wrapper ViewSwitcher to the TextView or EditText. Change your layout like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
    android:id="@+id/tv_editable_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Text" />

<EditText
    android:id="@+id/edit_editable_text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:hint="Enter text" />
</merge>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top