How can I automatically clear the text in EditText? - java

final TextView textView = (TextView) findViewById(R.id.textView);
final EditText editText = (EditText) findViewById(R.id.editText);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String etString = editText.getText().toString();
textView.setText(etString);
}
});
I am new to coding and I don't know how to automatically clear the text in an EditText so the user doesn't have to do it themselves. You can see in all "big" websites that when you enter text into a text box (for example: "What's your name?") you see "first name" in the text box and when you click on it, it disappears. How can I make the text disappear automatically without the user having to delete it manually?

What you're referring to isn't actually text that's being set -- it's a hint (which is why it doesn't show after the EditText is focused or text has already been entered). You can set these placeholder hints in your EditText XML, for example:
android:hint="First Name".
If you're feeling really adventerous, you can also wrap your EditText in a TextInputLayout for a Material Design style floating hint.
If that helps, be sure to accept this answer.

You can do it grammatically by modifying your code to use the hint property:
editText.setText("");
editText.setHint(<string>);
also you can change color by adding
editText.setHintTextColor(<some color>);

Related

How to set EditText Value to hide by a click when it is not empty

I have tried to create a button, actually that's what I want. I wish that when the user logs in (Name+number) 2 editTexts, He will be able to start and go to ordering activity. But when he comes turns the app off. he will find the editText hidden and get to signIn again when he presses signIn, please check this picπŸ‘‰. I have actually record signIn to preferences. just want to customise that sign IN buttonπŸ‘‰πŸ‘‰πŸ‘‰πŸ‘‰
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro1);
editText = (EditText) findViewById(R.id.signIn);
editText1 = (EditText) findViewById(R.id.myNob);
TextView definingSignIn = findViewById(R.id.definingSignIn);//Just a textView for explaining why we need signIn
TextView definingSignIn = findViewById(R.id.definingSignIn);
TextView signInAgain = findViewById(R.id.signInAgain);
signInAgain.setOnClickListener(view -> {
if (editText.getText().toString().isEmpty()){
editText.setVisibility(View.VISIBLE);
if (editText1.getText().toString().isEmpty())
editText1.setVisibility(View.VISIBLE);
} else {
editText1.setVisibility(View.GONE);
editText.setVisibility(View.GONE);
definingSignIn.setVisibility(View.GONE);
}
});
}

Passing data from one view to another using intents

I know there are other questions that have already been asked on this topic and I've already looked through a lot of them and tried their answers but none of them have worked.
I've got 2 activities, MainActivity and Main2Activity (that was the given name) (I'll refer to them as 1 and 2 respectively) each with their own respective XML files. What I'm trying to do is have a user enter something into an EditText field on 2 and then when they press enter, what they entered will appear in a TextView on 1. I'm trying to first get it to work when they press a button but it refuses to work. The ultimate goal is for it to get it to get transferred to 1 when they press/tap the enter key on the keyboard that pops up, so if it's easier to just skip the button and go straight to the enter key then by all means.
Any help that I can get is greatly appreciated. I'll put my (relevant) code as well as a couple screenshots below. Thank you.
Java code for 2 (Main2Activity):
public class Main2Activity extends AppCompatActivity {
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
et = (EditText) findViewById(R.id.editText);
}
EditText text;
public void CLICK_THIS (View v)
{
Intent intent = new Intent();
intent.putExtra("TextValue", et.getText().toString());
intent.setClass(Main2Activity.this, MainActivity.class);
startActivity(intent);
}
Intent intent = getIntent(); //i honestly forgot what this line is doing here
//i think it's here from some other thing i was trying to do and i just forgot to delete it.
}
Java code for 1 (MainActivity):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
view = (ImageView) findViewById(R.id.imageView3);
backgroundImageName = String.valueOf(view.getTag());
TextView text = (TextView) findViewById(R.id.textView2);
String s = getIntent().getStringExtra("TextValuie");
text.setText(s);
}
Screenshot 1 (this is what MainActivity (1) looks like):
The text that's highlighted is just to show where the TextView is. it's "not supposed to be here".
Screenshot 2 (this is what Main2Activity (2) looks like):
Screenshot 3 (this just shows the keyboard) (ultimately the user would type something into the "Enter Your Text Here" and then press the Check mark and it would appear where the highlighted text is in 1 instead of clicking the button in 2 for that to happen). If I've confused anyone at any point please don't hesitate to ask for more information.
First of all you are retrieving the input using a wrong label "TextValuie" instead of "TextValue"
The other thing is that are you entering the text that on Button Click the EditText can get the entered value or the default would be an empty string?
You can always debug and put a break point on the intent to see the data that you are getting back from the activity.

How to update SetContentView (Android)

I am a beginning of Android Programming, i am wring a SMS sending program today.
The program like this:
After clicking the Sent button,
The results will shown like above:
But may I shown the results below the send button, like the following one ?
Here is the java code of front page :
public class sms extends Activity {
Button sendButton;
EditText phoneTextField;
EditText msgTextField;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smslay);
msgTextField = (EditText) findViewById(R.id.msgTextField);
sendButton = (Button) findViewById(R.id.sendButton);
phoneTextField = (EditText) findViewById(R.id.phoneTextField);
}
Just take one textview below to send button. After you got result just set text to that textview. When user will again click on send button set that text to empty string.
It sounds like you want a toast message http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Instead of edittext or vise versa

Programatically set View width on button click

Right now I have an EditText with id "getUserName" and a button next to it (both in a linear view) with id "setName"
I want someone to be able to click setName, and have the EditText field disappear, the button disappear, and a TextView take it's place. Here's what I have thus far:
public void setName(View view){
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
String playerName = editText.getText().toString();
((ViewManager)editText.getParent()).removeView(editText);
((ViewManager)button.getParent()).removeView(button);
Log.d("ScoreKeeper", playerName);
}
So I am successfully removing the desired elements from the screen, but I don't know how to add the textView to take their place.
How can I do that? I'm brand new to Android, so forgive me if this seems ignorant. I've tried looking it up!
Thanks
OPSRCFTW
You can simply hide the EditText, Button and TextView using turn visibility on.
You can add textview in your xml file and keep it invisible..
On button click, just change its visibility...
So the code is on buton click like below:
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);
First -> make ur textview Gone,
textview..setVisibility(View.GONE)
when u click the button..
Second -> Make
`Make the EditText and Button GONE with` `edittext.setVisibility(View.GONE);` and make textview visible textview..setVisibility(View.VISIBLE)
What about starting with
textView.setVisibility(View.GONE);
and then set an OnClickListener to your button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
Write code onCreate method of your class
EditText editText = (EditText) findViewById(R.id.getUserName);
Button button = (Button) findViewById(R.id.setName);
TextView textView = (TextView) findViewById(R.id.displayName);
textView.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText.setVisibility(View.GONE);
button.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
});
Hope it will help you.
You can also dynamically create the text view , like textview view= new textview(context); set the height and width thru layout params; and then add this view to parent view or pare layout like parent view.addview(textview). Change the visibility of the button and edittext rather than totally removing them.
on startup
textView.setVisibility(View.GONE);
on button click
textview.setVisibility(View.VISIBLE);
edittext.setVisibility(View.GONE);
button.setVisibility(View.GONE);

Can java do printIn in Android?

I have an EditText, and when a button is clicked, I want the text to be displayed on the screen. And the EditText can be used again to add other text and on and on. Thanks!
Something like:
EditText et = (EditText)findviewById(R.id.et1);
TextView display = (TextView)findviewById(R.id.tv1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
tv1.setText(tv.getText().toString()+"\n"+et.getText().toString());
//prints the text from edittext and concatinates it to the textview previous text
}
});
Please use below code for that, it will solve your problem.
EditText editText1 = (EditText)findviewById(R.id.editText1);
TextView textView1 = (TextView)findviewById(R.id.textView1);
plusBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
textView1.append("\n"+editText1.getText().toString());
// OR
textView1.setText(textView1.getText().toString()+"\n"+editText1.getText().toString());
}
});
You can add Views to your Layout by using its addView() Method. So you could add a TextView upon each button-click which contains the content of your TextField.

Categories