1

I was wondering how you could make the TextView and EditText run in sequence so that I wouldn't need one row for the TextView and another for the EditView. I just feel it would make the app i'm creating a lot more clean. Any advice on that.

So far I'd connected the EditText to a button which then outputs text.I simply just want it in one row. This is the code I have so far.

public class EventActivity extends AppCompatActivity {
private EditText enter_txt;
private TextView txt_title;

public void buttonOnClick(View v) {
    Button button = (Button) v;
    enter_txt = (EditText) findViewById(R.id.enter_txt);
    txt_title = (TextView) findViewById(R.id.txt_title);
    txt_title.setText(enter_txt.getText());

}

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



}

Here's a picture of what I've got so far. Thanks. :D

Image

Isaiah Reid
  • 19
  • 1
  • 2

2 Answers2

0

Even though it was very hard to understand you, finally I see what you are trying to achieve. Here is how exactly you do, what you desire:

your activity_event.xml file layout for example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button android:id="@+id/button"
    android:text="Button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="button"/>

<EditText android:id="@+id/edittext"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"/>

<TextView android:id="@+id/textview"
    android:text="Text from EditText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:visibility="gone"/>

and your EventActivity.class:

public class EventActivity extends Activity {

Button button;
TextView textView;
EditText editText;

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

    button = (Button)findViewById(R.id.button);
    textView = (TextView)findViewById(R.id.textview);
    editText = (EditText)findViewById(R.id.edittext);

}

public void button(View view) {
    textView.setText(editText.getText().toString());
    editText.setVisibility(View.GONE);
    textView.setVisibility(View.VISIBLE);
}

The result is:

Image Output

The thing is that you set your EditText visibility to GONE and at the same time the visibility of your TextView to VISIBLE when button is pressed.

more information about the Views visibilities you can find at Google API: setVisibility API or for example at already discussed thread: Stackoverflow: How can I remove a button or make it invisible in Android?

Community
  • 1
  • 1
Matcoil
  • 890
  • 11
  • 23
0
public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText etHide = (EditText) findViewById(R.id.etHide);
    final Button btnShow = (Button) findViewById(R.id.btnShow);
    btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            etHide.setVisibility(View.VISIBLE);
        }
    });
    etHide.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            etHide.setVisibility(View.INVISIBLE);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


}

}

Souvik
  • 1
  • While this answer may be technically correct, it is of very low quality. Explain what you are doing and why this answers the question. – SubliemeSiem Apr 07 '16 at 11:12