Akshay Damle
Akshay Damle

Reputation: 1250

How to use one EditText to set the text of multiple TextViews in Android?

I have four TextView items, all of which are clickable. I also have one EditText item and one "Go" button. The text input into the EditText field by the user must be used to set the text of that TextView item which was clicked.

So if I click TextView item number 2 and then input some text into the EditText box and then hit the "Go" button, the text input must be written in TextView item number 2 only and the other 3 TextView items should be left unchanged. How do I accomplish this? Inside the "Go" button's onClick method, how do I find out which TextView was clicked?

Upvotes: 0

Views: 2653

Answers (6)

Neha - Systematix
Neha - Systematix

Reputation: 96

Find each Textview in Your Activity class like :

    TextView tv_1=(TextView)findViewById(R.id.tv_1);
    TextView tv_2=(TextView)findViewById(R.id.tv_2);
    TextView tv_3=(TextView)findViewById(R.id.tv_3);
    TextView tv_4=(TextView)findViewById(R.id.tv_4);

Get value from the second Textview with onclick function using

String value=tv_2.getText().tostring(); 

Upvotes: 1

LeMoN.xaH
LeMoN.xaH

Reputation: 571

i dont like the setting of an ID to know which textView to set. We are working on an Object Oriented Language why then use strutured data. why not just do something like this

public class MainActivity extends Activity implements OnClickListener {

    TextView textView = null;
    EditText e1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((TextView) findViewById(R.id.textView1)).setOnClickListener(this);
        ((TextView) findViewById(R.id.textView2)).setOnClickListener(this);
        ((TextView) findViewById(R.id.textView3)).setOnClickListener(this);
        ((TextView) findViewById(R.id.textView4)).setOnClickListener(this);
        e1 = (EditText) findViewById(R.id.one);
        b1 = (Button) findViewById(R.id.enter);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (editText != null) {
                    editText.setText(e1.getText().toString());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
      editText = (TextView)v;
    }

}

Upvotes: 1

SoulRayder
SoulRayder

Reputation: 5166

Here is a solution. Create four non-local variables ( say int), one for each textview and initialize it to zero. When you click a textview, set the corresponding variable to a non-zero value, in the onclick event of that textview.

Finally, when you type in edittext and click go, check which textview variable is non-zero, and change that textview accordingly ( in the onClick event of go). Also remember to set that textview variable back to zero so that the logic will work for future inputs.

EDIT: You can also simplify this method by making use of a single variable and store 1,2,3,4 etc here. But the advantage of the method I suggested is that it would work even if you want to set the text of more than one but not all textviews when go is clicked.

Upvotes: 1

Chintan Soni
Chintan Soni

Reputation: 25267

Say you have these four textviews:

TextView1
TextView2
TextView3
TextView4

Specify the onClick() attribute of these TextViews in xml itself with the value of, say, selectedTextView, as below:

<TextView
...
android:onClick="selectedTextView"
/>

Now, inside your activity,

take a global String variable as,

TextView mTextView;

Then, create a method outside onCreate() named,

public void selectedTextView(View view)
{
    mTextView = (TextView) view;
}

And inside onClick of button Go, simply set the text using:

mTextView.setText("Your value inside EditText goes here");

Upvotes: 1

Tamilselvan Kalimuthu
Tamilselvan Kalimuthu

Reputation: 1532

Create a single int variable in the class

set onclick listener for all the text view and set the id to the variable and use it in the button click.

here i a example that i tried.

public class MainActivity extends Activity implements OnClickListener {

TextView t1, t2, t3;
EditText e1;
Button b1;
private static int ids = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1 = (TextView) findViewById(R.id.textView1);
    t2 = (TextView) findViewById(R.id.textView2);
    t3 = (TextView) findViewById(R.id.textView3);
    e1 = (EditText) findViewById(R.id.one);
    b1 = (Button) findViewById(R.id.enter);
    t1.setOnClickListener(this);
    t2.setOnClickListener(this);
    t3.setOnClickListener(this);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (ids != 0) {
                TextView tx = (TextView) findViewById(ids);
                tx.setText(e1.getText().toString());
            }
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.textView1:
    case R.id.textView2:
    case R.id.textView3:
        ids = v.getId();
        break;
    }
}

}

Upvotes: 1

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

I think you need to declare one integer to keep track of the textview which was clicked and then in onclicklistener of button you should check that integer value and according to that value set the text of particular textview which was clicked.I just created demo code which gives you the idea enough to get you on path..:)

Solution:--

    int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

    TextView tv1, tv2, tv3, tv4;
    tv1 = (TextView) findViewById(R.id.tv1);
    tv1 = (TextView) findViewById(R.id.tv2);
    tv1 = (TextView) findViewById(R.id.tv3);
    tv1 = (TextView) findViewById(R.id.tv4);
    EditText et = (EditText)findViewById(R.id.et);

    Button btnGo = (Button)findViewById(R.id.btnGo);

    tv1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            i = 0;
        }
    });
    tv2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            i = 1;
        }
    });
    tv3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            i = 2;
        }
    });
    tv4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            i = 3;
        }
    });

    btnGo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (i) {
            case 0:
                tv1.setText(et.getText().toString());
                break;

            case 1:
                tv2.setText(et.getText().toString());
                break;

            case 2:
                tv3.setText(et.getText().toString());
                break;

            case 3:
                tv4.setText(et.getText().toString());
                break;

            }
        }
    });

}

Upvotes: 0

Related Questions