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.
Related
I am new to programming and trying to understand why sometimes it appears that objects are instantiated without a "new" keyword. For instance the basic tutorial app from google's android tutorial:
In the "Build an Intent" example, an Intent is created using the new keyword:
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
...
}
But later in "Display the Message", they make an Intent object this way if I am understanding correctly:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
...
}
And use the getIntent() method. Also, where is getIntent() method defined? It is a method from the Intent class?
Also on the previous section (https://developer.android.com/training/basics/firstapp/starting-activity.html) they make a new editText but I still don't understand why the "new" keyword isn't used:
EditText editText = (EditText) findViewById(R.id.editText);
Thanks for any help.
Good to learn that you want to start learning code.. I have added the solutions to your two questions, best is to just keep going and then everything will start to look logical, step by step.. The beginning is the hardest..
Intent intent = getIntent();
This instantiates an Intent object, the value comes from the function getIntent().. In the following link you can find that it is a method from the class Activity and returns the intent that started this activity.
https://developer.android.com/reference/android/app/Activity.html
EditText editText = (EditText) findViewById(R.id.editText);
This is a referral to a field "editText", defined in your Layout File.
You refer to the ressource in the layout..
Better and clear is to use another naming..
EditText "how you want to name the field" = (EditText) findViewById (R.id."name of the field in the layout")
As suggested below, try to take some basic classes online, watch some tutorials and hang in there!
Good luck!
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>);
I replaced an EditText with an AutoCompleteTextView in my app to make things a little more user friendly, but I am having an error.
In the app, the user types in the name of a plant, and then clicks a button to be taken to a new activity where some information about the plant is displayed.
The error: the app crashes after I press the button to be taken to the next activity after the user types in the name of the plant. This didnt happen when I still had an EditText. Perhaps I am using the AutoCompleteTextView wrong?
Heres the relevant code:
public class Main2Activity extends AppCompatActivity {
AutoCompleteTextView edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//this is the AutoComplete
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
//this is the list of suggestions for the AutoComplete
String[] items = getResources().getStringArray(R.array.items_array);
java.util.Arrays.sort(items);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
edit.setAdapter(adapter);
//this is the method that is called when the button is pressed
public void find(View view) {
String name = edit.getText().toString();
//basically, whatever is typed into the AutoComplete is turned into a string, and
//if the string matches one of the existing plants, the user is taken to
//the next activity
if(name.equalsIgnoreCase("Sunflower")){
Intent intent = new Intent(this, Sunflower.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Cactus")){
Intent intent = new Intent(this, Cactus.class);
startActivity(intent);
}
Can anyone see why this does not work?
Change below:
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
To:
edit = (AutoCompleteTextView) findViewById(R.id.et_item);
Your autocomplete textview scope is limited to onCreate() and
edit.getText().toString();
You are trying to get text from it which is not initialized yet. So it will get null pointer exception.
i was wondering how can i make a textview viewable where the user can click on it and when he click on it the textview creates another page where the text is by her own and the user can select the textview now, because i have more than 30 textviews and i want to make each of them when they get clicked they have their own layout where user can select them now and add more stuff like sharing the textview and this is all in one layout for each textview alone
i hope you can help me and thanks in advance
Make a new activity and use put/get extra to pass on data to new activity.
try following
http://developer.android.com/training/basics/firstapp/index.html
you should create list view
and in on item click of the list you can simply start another activity which will have layout according to the item you have clicked.
click here
and there onitemclick you can start new activity
Intent intent = new Intent(this, NewActivity.class);
intent.purStringExtra("key",rowItems.get(position));
startActivity(intent);
try it and ask if you have any doubt or it is not clear
You can use this code to make your TextView visible on click event
myTextView.setVisibility(View.VISIBLE);
try using Buttons instead of TextViews for getting TextViews...
example
you have Buttons "A" and "B" and want to show Button "B" only when when A Clicked and then want to show TextView "C" only when when B Clicked
in this way (A->B->C )
then try using these codes:
only important parts are written...
in activity_main.xml :
<Button
android:id="#+id/button_A"
android:onClick="do_A"/>
<Button
android:id="#+id/button_B"
android:onClick="do_B"/>
<TextView
android:id="#+id/Textview_C" />
in MainActivity.java :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button A = findViewById(R.id.button_A);
Button B = findViewById(R.id.button_B);
TextView C = findViewById(R.id.Textview_C);
B.setVisibility(View.GONE);
C.setVisibility(View.GONE);
}
public void do_A (View v){
B.setVisibility(View.VISIBLE);
}
public void do_B (View v){
C.setVisibility(View.VISIBLE);
}
}
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