I'm attempting to pull user input from a couple EditText fields into another activity. In particular, I'd like to utilize team names input by the user in another activity of the app.
So, taking input from here:
and using it here:
As seen in the photo above, I've been successful in implementing this when the user enters in a couple team names, which I'm handling with getIntent().getExtras().
However, if the user clicks the "Start Scorekeeper" button without entering in any team names, I get the following:
However, I'd like to set default team names of "Team A" and "Team B".
I've tried to accomplish this with the following (which hasn't worked):
TeamSelector.java:
public void onClick(View view) {
Intent i = new Intent(this, ScoreKeeper.class);
final EditText teamAInput = (EditText) findViewById(R.id.team_a_input);
String teamAName = teamAInput.getText().toString();
i.putExtra("teamA", teamAName);
final EditText teamBInput = (EditText) findViewById(R.id.team_b_input);
String teamBName = teamBInput.getText().toString();
i.putExtra("teamB", teamBName);
startActivity(i);
}
Then creating a conditional as I pick up the intent.
ScoreKeeper.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_keeper);
Bundle teamNames = getIntent().getExtras();
if(teamNames==null){
final TextView teamAText = (TextView) findViewById(R.id.teamAText);
teamAText.setText(getString(R.string.team_a_name));
final TextView teamBText = (TextView) findViewById(R.id.teamBText);
teamBText.setText(getString(R.string.team_b_name));
}
else {
String teamA = teamNames.getString("teamA");
final TextView teamAText = (TextView) findViewById(R.id.teamAText);
teamAText.setText(teamA);
String teamB = teamNames.getString("teamB");
final TextView teamBText = (TextView) findViewById(R.id.teamBText);
teamBText.setText(teamB);
}
}
I've also tried coding default strings in the XML, which hasn't been effective (only one team included for brevity):
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="16dp"
android:text="#string/team_a_name"
android:id="#+id/teamAText"
android:textColor="#android:color/black"
android:textSize="14sp" />
Is there a way to effectively set a default when .getIntent().getExtras() is null?
A String from EditText is never null. It can be empty but not null.
A simple check like this would work
teamAText.setText(!teamA.isEmpty() ? teamA : "Team A");
Try this:
public void onClick(View view) {
Intent i = new Intent(this, ScoreKeeper.class);
final EditText teamAInput = (EditText) findViewById(R.id.team_a_input);
String teamAName = teamAInput.getText().toString();
if(teamAName.trim().equals("")) teamAName="Team A";
i.putExtra("teamA", teamAName);
final EditText teamBInput = (EditText) findViewById(R.id.team_b_input);
String teamBName = teamBInput.getText().toString();
if(teamBName.trim().equals("")) teamBName="Team B";
i.putExtra("teamB", teamBName);
startActivity(i);
}
The strings will not be null, nor will the intent necessarily. The strings will be zero length strings, which you can use String.isEmpty() to test against.
Try to change this:
teamAText.setText(getString(R.string.team_a_name));
teamBText.setText(getString(R.string.team_b_name));
to this:
teamAText.setText(R.string.team_a_name);
teamBText.setText(R.string.team_b_name);
It is because getString needs an int as parameter and your R.string.team_a_name and R.string.team_b_name are Strings so you don't have to convert it again (Further that they are not ints).
I expect it will be helpful for you!
Related
After login, I want to change text in TextView near profile on name_user.
But it doesn't change textView visually.
It is worth to mention, that when outputting (Toast), it gives out the data that is needed, but does not visually display it. Everything is fine with the TextView parameters (I think), because if you set the finished text in the parameters( i mean android:text="smth"), it visually displays it.
Java code:
`protected void onCreate(Bundle savedInstanceState) {
yourLayout = getLayoutInflater().inflate(R.layout.layout_navigation_header, null);
profileName = yourLayout.findViewById(R.id.profName); //
Intent intent = getIntent(); // Get data from previous activity.
String name_user = intent.getStringExtra("name");
String email_user = intent.getStringExtra("email");
String password_user = intent.getStringExtra("password");
profileName.setText(name_user); //0 changes, textView still don't change.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
Toast toast = Toast.makeText(getApplicationContext(), profileName.getText().toString(),
Toast.LENGTH_SHORT); // for debug, it works and show profileName that contains name_user, but.
toast.show();
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
}`
Part of main XML
`<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/layout_navigation_header"// layour_navigation_header -here is TextView
app:menu='#menu/navigation_menu'
android:layout_gravity="start"/>`
Part of layour_navigation_header with TextView that I need to change.
`<TextView
android:id="#+id/profName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:textColor="#color/black"
android:textSize="18sp"
android:text="Temporary"
app:layout_constraintBottom_toTopOf="#id/viewSupporter"
app:layout_constraintStart_toEndOf="#id/imageProfile"/>`
Hope you could help me
I tried to move
`yourLayout = getLayoutInflater().inflate(R.layout.layout_navigation_header, null);
profileName = yourLayout.findViewById(R.id.profName); //
Intent intent = getIntent(); // Get data from previous activity.
String name_user = intent.getStringExtra("name");
String email_user = intent.getStringExtra("email");
String password_user = intent.getStringExtra("password");
profileName.setText(name_user); //0 changes, textView still don't change`
before
`super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu2);`
but final result remains the same. It contains data, but not visually displays it.
You're inflating layout_navigation_header layout and setting a value in one of its textviews. But you never seem to place the layout on screen, the layout instance simply gets discarded.
What gets displayed is the activity_menu2 layout you inflate and set as content view with setContentView(). If that layout includes layout_navigation_header or its look-a-like with some mechanism, it's not the same instance you inflated earlier.
To solve the issue, just call setContentView() to set your desired layout, call findViewById() to find the textview and set a text to it.
I have an edittext in first activity (apples.java). User can write anything using edittext which is passed as a string to second activity (Bacon.java) on clicking a button. Second activity has a textview written Bacon. When a string is passed from first activity, Bacon is replaced with the passed string/text.
What is happening : When nothing is written using editText and button is clicked to go to second activity, Bacon disappears.
My requirment is that if nothing is passed using EditText and only button is clicked, Bacon should not get replaced.If something is passed, it should replace Bacon.
apples.java:
Intent i = new Intent(apples.this,Bacon.class);
final EditText applesInput = (EditText) findViewById(R.id.applesInput);
String userMessage = applesInput.getText().toString();
i.putExtra("applesMessage",userMessage);
startActivity(i);
Bacon.java
final TextView baconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras();
if(applesData==null){
Toast.makeText(Bacon.this, "Love u", Toast.LENGTH_SHORT).show();
return;
}
String applesMessage = applesData.getString("applesMessage");
baconText.setText(applesMessage);
This is first activity
This is second activity
Do this:
if (!TextUtils.isEmpty(applesMessage))
baconText.setText(applesMessage);
In apples.java write if(applesInput.getText().toString().equals(""); { i.putExtra("applesMessage",userMessage);} just add this..
your problem is that Bundle is not null, so the condition is never satisfied.no string is set does not mean that your bundle is Null
change it to
final TextView baconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras();
if(applesData!=null){
String applesMessage =applesData.getStrng("applesMessage")
if(applesMessage ==null ||applesMessage .trim().equals("")){ //trim to delete spaces
Toast.makeText(Bacon.this, "Love u", Toast.LENGTH_SHORT)
.show();
return;
}
baconText.setText(applesMessage);
}
}
This question already has answers here:
java.lang.IllegalStateException : Could not find a method onClick handler with id_button
(4 answers)
Closed 8 years ago.
I have to make an app where the user inputs and then the AI responds but when I input some text and press send it gives me the message "Unfortunately, app has stopped".
Here is my code:
Here is the code for sending:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button"
android:id="#+id/Send_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="Zdenka" />...
Here is the start of the .java file:
EditText Text, OdgBox;
String odg;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText Text = (EditText) findViewById(R.id.Txt); //User input
Button Btn = (Button) findViewById(R.id.Send_btn); //Send button
TextView Output = (TextView) findViewById(R.id.TextView); //AI output
}...
And the last part of the java file:
...
public void Zdenka (TextView Output, EditText Text, String odg) {
String Text1 = Text.toString().toLowerCase();
if (Text1 == "živjo") {
odg = "Živjo";
}
else if (Text1 == "zivjo") {
odg = "oj";
}
else{ odg = "Ne razumem."; }
Output.setText(odg);
Thanks for the help!
What I can see from your code is: You need to change your method of onClick:
You will need to replace
public void Zdenka (TextView Output, EditText Text, String odg) {...}
with
public void Zdenka (View v) {...}
Hope it helps.
And after you do this, make sure to use equals or equalsIgnoreCase for comparing String as Squonk said. == will compare objects, not the actual String.
I'll go over this as quick as posible. I'm developing an app that needs a comprobation from the text of a button. Let me explain it in a code-way. If my button has "This text", then do that. I've been trying for the past two days several diferent things, and I'm on a dead end, so I'm asking. Here is my code:
XML:
<Button
android:id="#+id/bHiddenL1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_margin="18dp"
android:text="#string/NotUsed"
android:maxLength="10" />
Java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newnotebookbutton);
initializeNotebookNewSubject();
}
public void initializeNotebookNewSubject() {
NewTextInput = (EditText) findViewById(R.id.etNewNotebookButtonCreateSubjectButton);
OKButton = (Button) findViewById(R.id.bOkButton);
Button1L = (Button) findViewById(R.id.bHiddenL1);
Button2L = (Button) findViewById(R.id.bHiddenL2);
Button3L = (Button) findViewById(R.id.bHiddenL3);
Button4L = (Button) findViewById(R.id.bHiddenL4);
Button5L = (Button) findViewById(R.id.bHiddenL5);
Button1R = (Button) findViewById(R.id.bHiddenR1);
Button2R = (Button) findViewById(R.id.bHiddenR2);
Button3R = (Button) findViewById(R.id.bHiddenR3);
Button4R = (Button) findViewById(R.id.bHiddenR4);
Button5R = (Button) findViewById(R.id.bHiddenR5);
OKButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String b1L = Button1L.getText().toString();
String b2L = Button2L.getText().toString();
String b3L = Button3L.getText().toString();
String b4L = Button4L.getText().toString();
String b5L = Button5L.getText().toString();
String b1R = Button1R.getText().toString();
String b2R = Button2R.getText().toString();
String b3R = Button3R.getText().toString();
String b4R = Button4R.getText().toString();
String b5R = Button5R.getText().toString();
if(b1L == "Not Used" && NewTextInput.getText().toString() != null){
NewSubjectBundle.putString("title1L", NewTextInput.getText().toString());
NewSubjectBundle.putInt("int", 1);
mIntent.putExtras(NewSubjectBundle);
setResult(RESULT_OK, mIntent);
finish();
Let me specify better my problem. Whenever I'm trying to do the "String b1L = Button1L.getText().toString();", I get an error in the logcat saying that the pointer is Null. How should that be fixed?
Don't compare content of Strings using ==.
b1L == "Not Used"
should be
b1L.equals("Not Used")
or even better
"Not Used".equals(b1L)
How do I compare strings in Java ?
ZouZou is right. But in your case, even better is:
b1L.equals(getResources().getString(R.string.NotUsed));
I have a user type in information such as a UPC in the EditText box and then click a button next to it. How can I get the text from the box to pass to my other activity when they click the button? I'm assuming I use an Intent to launch the activity.
The edit text element:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:cursorVisible="true"
android:ems="10"
android:inputType="text" />
Code for search button:
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:onClick="onClickSearch"
android:text="#string/search" />
Code in main.java:
public void onClickSearch(View view) {
String UPC = R.id.editText1.getText().toString();
Intent intent = new Intent("net.example.glutefree.Networking");
intent.putExtra("UPCA", UPC); //text is some key used to retrieve value in NextActivity
startActivity(intent);
}
If you need anymore code let me know
Ok, so you were off to a good start, the important parts are to:
1) Get a reference to the EditText object in your activity code:
private EditText mMyEditText;
which should be initialized during onCreate like this:
mMyEditText = (EditText) findViewById(R.id.editText1);
2) Handle the click:
public void onClickSearch(View view) {
//String UPC = R.id.editText1.getText().toString();
String UPC = mMyEditText.getText().toString();
//Intent intent = new Intent("net.example.glutefree.Networking");
Intent intent = new Intent(this, YourOtherClass.class);
intent.putExtra("UPCA", UPC); //text is some key used to retrieve value in NextActivity
startActivity(intent);
}
The difference is that you were calling getText on an Identifier: R.id.editText1, which is an int, where now, you will be accessing the actual instance of an EditText.
You assume correctly. This is basically how you do it depending on exactly what you have and need. But you really should read Here
String et = editText.getText().toString();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("text", et); //text is some key used to retrieve value in NextActivity
startActivity(intent);
R.id.editText1 returns an int, which is the reason for your error. Try something like this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.name_of_your_layout);
EditText editText = (EditText) findViewById(R.id.editText1);
}
then use the code I provided originally. You have to inflate the layout using setContentView() then create a variable, editText here, you might want to declare it at the class level. Then initialize it to the id you set in your xml. The way you have it, you are trying to call getText() on the int which is what is returned by R.id.some_id. Once you have the variable and cast it to EditText with (EditText) findViewById(R.id.editText1); then you can call getText() on that variable.
Also note that you have to call setContentView() as I have shown before trying to access a View in that xml or you will get a NPE when you try to use the variable created from the xml since the View, EditText here, exists in the layout