Is there a method to concatenate different activity TextView in EditText? - java

There are 3 activities. The text value of Textview in the first and second activity is shown in EditText of third activity.
Mainactivity - Textview1 text
Secondactivity - Textview2 text
Displayactivity - Edittext=Textview1+Textview2
Using intent I have tried to pass the the TextView values of both activities to third activity. In the third activity I concatenated by simply using + in EditText. I am trying to show previous textview values in one paragraph i.e. EditText.
This code is on third activity :
Display activity
Intent intent = getIntent();
String displayingtext = intent.getStringExtra("message");
String displayingsecondtext = intent.getStringExtra("hey");
editText.setText(displayingtext+displayingsecondtext);
displayingtext name: message is from first activity
displaysecondtext name : hey is from second activity
The output displayed is from the first textview and the next word null.
In the code it shows
"Don't use concatenate on setText. Use android resources"
Input:
Textview1= Hello,monday.
Textview2=Bye, monday.
Expected Output:
Editext=Hello,monday.Bye monday.

Use concat() method from java.lang.String insted of "+" operator.
String displayingtext = intent.getStringExtra("message");
String displayingsecondtext = intent.getStringExtra("hey");
editText.setText(displayingtext.concat(displayingsecondtext));

U need to send the "message" got form Activity1 & "hey" in the Activity2 startActivity intent param to Activity3.
It is just a warning as Android Studio Lint tool check the setText method param use a '+' operator. Consider fix it like this:
String resultText = displayingtext+displayingsecondtext;
editText.setText(resultText);

Using greenbot event bus you can do it better...
best example for event bus try it...

OK I got it.. It is saying that you have to put them in resources..
If your application doesn't use multiple language support, then you can simply neglect the warning

Related

Passing Filepath to second activity in andriod studio

I am new to Android programming and having a problem in passing a filepath from my main activity to second activity.
Here is my code in Main activity to pass two variables walletDir(contains path) and password.
private File walletDir;
private final String password = "pass";
Intent intent= new Intent(Create_Wallet.this,Wallet_Address.class);
intent.putExtra("EXTRA_WALLET_DIR", walletDir);
intent.putExtra("EXTRA_PASSWORD", password);
startActivity(intent);
Here is my code for second activity
Intent intent = getIntent();
String walletDir = intent.getStringExtra("EXTRA_WALLET_DIR");
String password = intent.getStringExtra("EXTRA_PASSWORD");
Now password is being passed but not walletDir its null and the error i am getting is:
W/Bundle: Key EXTRA_WALLET_DIR expected String but value was a java.io.File. The default value <null> was returned.
private File walletDir;
walletDir is an instance of File.
intent.putExtra("EXTRA_WALLET_DIR", walletDir);
This puts an instance of File into your Intent as an extra. This works because File implements the Serializable interface, and there is a putExtra() method that takes a Serializable.
String walletDir = intent.getStringExtra("EXTRA_WALLET_DIR");
The extra is not a String. Yet, you call getStringExtra() to read it. This is not going to work, because a File is not a String.
Change that line to:
File walletDir = (File)intent.getSerializableExtra("EXTRA_WALLET_DIR");
(it is possible that you do not need the cast — it has been quite some time since I did this in Java)
You might also consider whether you really should be using two activities here. Android is moving very strongly towards having fewer activities, with fragments or composables representing individual screens displayed by those activities.

pass variable from view to activity

I have been studying some code trying to learn more, the basic code is very simple. There is a xml layout file, and activity file and view file.
Eg.
simpleprog_layout.xml, SimpleProgView.java and SimpleProgActivity.java
I was looking at how a number of things are done and was trying to work out how a value could be passed from the view file for use in the activity file.
Can some one point me in the right direction?
Value being passed from View to Activity .
If it's to do with attaining reference of the View . It can be done using 2 Approaches
FindviewById()
ViewBinding
And if you'd wish to share a View's value to Another Activity , or to another view of Any other activity it can be achieved majorly in two ways .
Intents
Global Variables
Let's say for example there exists a String "name" and you wish to pass it to the ProfilePage of your application
Intent intent = new Intent(getBaseContext(), ProfileActivity.class);
intent.putExtra("user_name", username); // where Tag is user_name and the variable that holds value is username
startActivity(intent);
and the activity is able to receive the value using :
String userNameFromAnotherActivity= getIntent().getStringExtra("user_name"); // same Tag should be used
And with respect to global variable , any Activity is able to access the value with held in that variable.

Get text of textView from another activity

I am trying to get text from textview which is located in class 2 to use it in class 1 by pressing a button. I do this by sending an Intent, but i got the error of my content. That is what i am trying to send (from class 2 to class 1):
public static void intent_send(){
Intent i = new Intent();
i.putExtra("number",Integer.parseInt(text_view_current_page.getText().toString()));
class2.startActivity(i);
}
text_view_current_page is a static TextView, otherwise it has an error in this void. I call this void by pressing a button in class 1:
Class2.intent_send();
Intent i = getIntent();
Bundle b = i.getExtras();
PagerNumber = b.getInt("number");
I have an error in the line of the content definition:
i.putExtra("number",Integer.parseInt(text_view_current_page.getText().toString()));
What should i do with this textView to be able get it's text from another class by pressing a button? Should it be static or should i declare it in that class which receives an intent?
In Android activities are fully separated components and hence those cannot access each other's stuff directly. Those have their own window and view hierarchy which are private only to the owner activity itself.
Nevertheless there are a couple of ways for activities to interact with each other.
Sending data when you are starting second activity via Intent.
Utilizing Application object as a share object among all app's components.
Registering in-activity broadcast receivers by which you can send signals between activities.
My Recommendation for your case:
If in your case, the second activity should have a very close relation with the first one (e.g. accessing its view hierarchy), you could implement the second activity as a dialog or fragment not an activity.

Reading string values under if/else commands

Im new to android so im gonna need a little help.
I created four activities in my app.
First activity is the MainActivity.
I assigned a ListView in the first Activity.
From the listView you are redirected to the second activity with a string which says which item you clicked. It doesn't matter which item you selected, but you will be redirected to that second activity only. Only diffrence is that the values of the strings passed are different.
I used this code in the ListView's onItemClick function:
String item = (String) listView.getAdapter().getItem(position);;
Intent i = new Intent(MainActivity.this, Activity2.class);
i.putExtra("item_selected", item);
startActivity(i);
This code redirects me to the second activiy with the string without any problem.
In the second Activity there are two options "Launch Type 1" and "Launch type 2" inside a radioButton group and a button to perform the function.
So i used this code inside the button's onClick method to determine which Activity to go next:
Intent intent = getIntent();
String item = intent.getExtras().getString("item");
RadioButton launch1 = (RadioButton) findViewById(R.id.launch1);
//The problem code:..
if(launch1.isChecked()){
if(item=="ListView_Item1"){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item=="Item 1"){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}
In Eclipse it shows that there are no errors in the code. But when I run it in the Emulator it starts off fine till it reaches the second activity.
When i click the button nothing is done and it does not redirect to any new page...
:(
Please help me in constructing the "if/else" statements in the button...and please do tell me if there are any better ways to achive the task...
Thanks in advance.
Waiting for replies....
In Java, you compare strings using equals() and not ==.
if(item.equals("ListView_Item1"))
Using == you're comparing references and not content.
Use Object#equals() for checking if an object contains same data as another one and == for comparing if two references are referencing the same object.
use equals() for comparison of String or Objects in java !
so your code should be :
if(launch1.isChecked()){
if(item.equals("ListView_Item1")){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item.equals("Item 1")){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}

Unable to pass values between Activities in Android

I am using 2.3.3 version of the Android SDK and I'm trying to pass data between two activities using the following code
Intent myIntent = new Intent(MainAct.this, FriendsActivity.class);
myIntent.putExtra(USER_NAME, ((EditText)findViewById(R.id.username)).getText());
MainAct.this.startActivity(myIntent);
In the FriendsActivity i'm retrieving the value using
Bundle b = getIntent().getExtras();
String user = b.getString(MainAct.USER_NAME);
But the user is null after these lines are executed. Not sure whats wrong here. Read similar questions on SO http://goo.gl/zOJfa but still the problem continue to be seen.
You need to pass (EditText)findViewById(R.id.username)).getText().toString() instead. getText() on an EditText View doesn't return a string, it returns an Editable Object.
just do (EditText)findViewById(R.id.username)).getText().toString() you are able to get string value.

Categories