How to get text from edit text in Android - android

Okay.. I have 2 classes :-
1. Apple.class
2. Banana.class
And 2 .XML file:-
1. ear.xml
2. nose.xml
Now, I want to add a edit text box in apple.class
But I want to print that text that user input in banana.class

1) Get the entered value from edittext by the following
String content = EditText.getText().toString();
2) Then pass the string "content" to the next Activity using Intent.
Intent i = new Intent(Apple.this,Banana.class);
i.putstring("EditTextValue",content);
startactivity(i);
3) Get the data in Banana class and set to a textview.

Use the following code when clicked on the button , send data from AppleActivity to BananaActivty :
String value = edittext.getText().toString().trim(); //get text from editText
Intent i=new Intent(FirstDemo.this,SecondDemo.class);
i.putExtra("key", value);//you can add other key value
startActivity(i);
then in other class you have to get value from Intent like below:
String value = getIntent().getStringExtra("key"));
Hope to be useful to you

//one activity(FirstDemo)
EditText ed1,ed2;
String st="",st2="";
//onCreate method:-
st=ed1.getText().toString().trim();
st2=ed2.getText().toString().trim();
//then click on button
Intent i=new Intent(FirstDemo.this,SecondDemo.class);
i.putExtra("id", st);
i.putExtra("u_email", st2);
startActivity(i);
//then next Activity(SecondDemo):-
Edittext e1,e2;
Intent j = getIntent();
e1.setText(j.getStringExtra("id"));
e2.setText(j.getStringExtra("u_email"));

Related

How can I get the text of editText?

Im trying to make a random interger with hashcode of a particular String and I have to get the String from the editText. Below is a code I tried.
findViewById<View>(R.id.goButton).setOnClickListener{
val intent = Intent(this, Activity2::class.java)
intent.putIntegerArrayListExtra("result",
ArrayList(getNumbersFromHash(editText.getText().toString())))
startActivity(intent)
}
A List made of random integers are tossed to Activity2. The point is I have to get the text from the editText but editText.getText().toString() doesn't work. I have checked the id of the editText and there was no problem. How can I solve this problem?

Android Studio onClick button to display ImageView in another activity

I have setup 2 activities - one and two .
For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.
What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.
If user key in 1, it will display 1.png in activity 2
If user key in 2, it will display 2.png in activity 2
If user key in 3, it will display 3.png in activity 2
etc...
the image will get from drawable folder.
Please refer to this screenshot
[![enter image description here][1]][1]
I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.
get_image.setBackgroundResource(R.drawable.1); // here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it.
Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...
thanks in advance!
please check screenshot -> http://i.stack.imgur.com/53vjy.jpg
You said that you can pass integer value from activity 1 to 2 so just use that to find your image.
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
I may missing somethings because i can't understand when you said that "but i cant do it for imageview".
EDIT: after your addtional code.
So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id).
I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2
and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).
Try below code for your solution,
For Activity One write below code to redirect on activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 write below code in onCreate method
int selectedNumber = 1;
if(getIntent().getExtras() != null)
{
selectedNumber = getIntent().getExtras().getInt("SelectedNumber");
}
switch(selectedNumber)
{
case 1: // set your 01.png Image
break;
case 2: // set your 02.png Image
break;
// And so
}
Try this way might helps you.
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
then your Activity2 use,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
Finally,
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String mDrawableName = editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Intent ii=new Intent(Activity.this, Activity1.class);
ii.putExtra("resId", resID);
startActivity(ii);
}
});
Activity2
public class Activity2 extends Activity
{
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
img = (ImageView)findViewById(R.id.img);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
int drawableId =(int) b.get("redId");
img.setImageResource(drawableId);
}
}
}
you can pass resource value in extras. try this way.
EditText edit = (EditText) findViewById(R.id.yourEdittext);
Button btn = (Button) findViewById(R.id.yourButton);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
if(edit.getText().toString().trim().equals("1")){
i.putExtra("image", R.drawable.01);
startActivity(i);
}else if(edit.getText().toString().trim().equals("2")){
i.putExtra("image", R.drawable.02);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Please Enter Valid Value.",
Toast.LENGTH_SHORT).show();
}
}
});
and in your ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
Happy Coding.
In activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
then use switch case for values from intent then
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}

how can I parse a given edittext value from one activity to another? the value should overwrite a given textview in the new activity

I am sort of new to android programming. I have a series of EditText fields and I want the program to allow a user to input a value into the EditText field and whatever value that is input should be transferred to another activity where the value overwrites the content of a textview. How can I do this?
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra(AnotherActivity.KEY_EXTRAS_MESSAGE_AUTHOR, this.myEditText.getText().toString()));
startActivity(intent);
in AnotherActivity:
public class AnotherActivity extends Activity {
public static final String KEY_EXTRAS_MESSAGE_AUTHOR= "author";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
String author = getIntent().getStringExtra(KEY_EXTRAS_MESSAGE_AUTHOR);
You can do this as shown below:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Create an Intent and pass the value to another activity:
Intent intent = new Intent(this, anotherActivity.class);
//Passing the string here
intent.putExtra("value", readValue);
startActivity(intent);
In your anotherActivity catch the intent and set the value to edit text:
Intent intent = getIntent();
String result = intent.getStringExtra("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);
If you do not want to fire the activity by using startActvity(intent), you can use the following procedure:
Save the edit text value in Shared preferences:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Save the value:
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value", readValue);
editor.commit();
In another activity use it as shown below
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
String result = sharedPreferences.getString("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);

Android: Text not being applied to Card

So I'm trying to create an app where the user will go enter some text in two edittext fields and when a submit button is pressed, it will close the edit window and then add a new card to the list on the main activity. I've set up all my code and everything works without error. However, no cards are added and when the application starts, there's one card which has no text anywhere.
My Code:
MainActivity.java:
//The following lines of code are in onCreate() and setContentView() has been
//called
//Get the intent that launched the activity
Intent i = getIntent();
//Get the strings from the intent
String sig = i.getStringExtra("signature");
String lMessage = i.getStringExtra("long_message");
//Init mCardView
mCardView = (CardUI) findViewById(R.id.cardsview);
//Allow swipe to delete for cards
mCardView.setSwipeable(true);
//Add new card with the two extra strings from the edit text fields
mCardView.addCard(new MyCard(sig, lMessage));
//Draw the cards
mCardView.refresh();
AddText.java
//The following lines of code are in onCreate() and setContentView() has been
//called
//Find edit text's and button
EditText name = (EditText) findViewById(R.id.enter_name);
EditText message = (EditText) findViewById(R.id.enter_message);
button = (Button) findViewById(R.id.add_signature);
//Get the string from the edit text...whatever it may be.
final String signature = name.getText().toString();
final String longMessage = message.getText().toString();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Make a new intent to start main activity
Intent intent = new Intent(getApplication(), YearbookMain.class);
//Put extras into intent
intent.putExtra("signature", signature);
intent.putExtra("long_message", longMessage);
//Start intent
startActivity(intent);
}
});
Again, I'm not getting any crashes it's just that the card is not being edited. I've tried everything I can think of so I'm hoping someone can think of something else!
Thanks

How to get the name of a textView?

I have the following situation:
I have an activity(A) that calls a new activity(B) window. So in activity (B) I have 5 Text Fields(textbox) with their names (5 TextViews) which the user have to fill in. I want to return to the previous activity(A) with string containing the names of which textBoxes were filled (not the value put in the textbox just the name). Now my problem is how to do this? I know that for the passing of data from activity (B) to activity(A) I have to use:
Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
setResult(Activity.RESULT_OK, resultIntent);
finish();
and retrieve the data in activity (A) with:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (MY_CHILD_ACTIVITY) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
}
break;
}
}
}
But I dont know wjat to pass since the name in front of the textbox is not connected to the textBox so how can I know which textbox has been filled and what name should I pass back?
You can get the name of a View via:
Resources#getResourceName(int) or Resources#getResourcesEntryName(int)
getResources().getResourceName(theView.getId());
getResources().getResourceEntryName(theView.getId()));
You may also want to consider using setHint(CharSequence) to label the EditText instead of using TextView
Or, you could group the TextView and EditText in a layout:
<LinearLayout android:id="#+id/the_first_name_group">
<TextView android:text="The first name" />
<EditView android:id=#+id/the_first_name_value />
</LinearLayout>
and add code such as:
ViewGroup parent = (ViewGroup) theFirstNameValue.getParent();
getResources().getResourceEntryName(parent.getId());
which would return the_first_name_group
you cannot say which editbox correspondents to which textfield (name)
you have to remember this by yourself:
if i have something like this:
----------------------------
| Firstname | | [EditBox] |
----------------------------
| Lastname | | [EditBox] |
----------------------------
if the first box is called box1 and the second box2,
how should i know that box1 is a firstname and box2 a lastname?
in now way.
so because you have designed the layout, you also know that box1 is firstname
so if the user filled in his first and lastname or even if he didn't fill in anything and then goes back to your Activity A,
you can just put the editboxes texts as extras from B to A. and because you know what editbox1 corresponds to, you can pass it back with a key YOU know:
Intent i = new Intent();
i.putExtra("firstname", firstname.getText().toString());
i.putExtra("lastname", lastname.getText().toString());
setResult(Activity.RESULT_OK, i);
finish();
and in on activity result get the values youve just set, with:
String first = intent.getExtras().getString("firstname", null);
and same for lastname

Categories

Resources