How to get the name of a textView? - android

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

Related

How to get text from edit text in 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"));

Calling activity from two buttons returns different results

Suppose you have two activities A and B. Activity A has two buttons X and Y.Is there any way to receive different results based on what button is calling activity B;
To achieve this you have to put extras on your intent in the buttons onclicklisteners
Intent i = new Intent(context,.class);
i.putExtra("action", "button x") //for button x listener on activity A
i.putExtra("action", "button y") //for button y listener on activity A
to know wich one was clicked just do this on activity B
Intent i = getIntent();
String result = i.getStringExtra("action");
if(result.equals("button x")) do something
else if(result.equals("button y")) do something
try something like this :
when button X is pressed
intent.putExtra("Data for Activity A", data);
when button Y is pressed
intent.putExtra("Data for Activity B", data);

Android programming help

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?
you mean like this ??
in the method onCreate() :
btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);
and then , ovverride the onClick method like this :
#Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
First of all, you will have to pass the edittext value to the first activity through intent.
Eg:
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);
Then inside your first activity, you will have to fetch this data as:
String value;
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
value = extras.getString("edittext_value");
textview.setText(value);
}
Hope this may help you.
From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :
Change the way you open Activity2 to
Intent EditIntent = new Intent(this, Activity2.class);
//Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);
Add override to you Activity1
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
if (data.hasExtra("myText")) {
//get your data with data.getExtras().getString("myText")
}
}
}
Change what button on your Activity2 does
{
Intent returnData= new Intent();
returnData.putExtra("myText", /*Text from your EditText*/);
if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
setResult(Activity.RESULT_OK, returnData);
} else {
getParent().setResult(Activity.RESULT_OK, returnData);
}
finish();
}
This should return text from EditText from Activity2 to Activity1. Code was not tested
create a variable for the textview to access it like
Textview txt = (Textview) finviewByid........;
implement the following code on button click listener
txt.setText(edittext.getText().toString());

How to make EditText regain focus?

I have one activity with an EditText and a button. When the button is pressed, I call
myEditText.setClickable(false);
myEditText.setFocusable(false);
I have another button, which when pressed, changes the activity.
Intent myIntent = new Intent(view.getContext(), DestinationScreen.class);
startActivityForResult(myIntent, 0);
When I return from activity2 to my main activity which has the EditText, I want it to regain the focus. That is, I want to be able to type in some new values in it. Any idea how that is possible?
I tried to do this in my main Activity
startActivityForResult(myIntent, 0);
myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();
It doesn't seem to work.
As you said, you'd like the EditText to regain focus when you return from the second activity.
Then probably that's what you should try: since you are already invoking the activity2 with the startActivityForResult method (requestCode: 0), you could take advantage of it:
You should override the
onActivityResult(int requestCode, int resultCode, Intent data)
method inside your main activity, check whether the requestCode == 0, and if so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 0:
EditText myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();
default:
break;
}
}
I haven't stepped through the Android source to fact check this, but the symptoms imply to me that:
#onActivityResult() is called sometime before #onResume(), and
requesting focus probably requires the view to be shown, which isn't true yet (because the activity's view hierarchy isn't attached to its window, see View#isShown()).
As such, you can fix it by requesting focus from a runnable on the main thread, which will run immediately after the activity is resumed. In your onActivityResult() definition:
final EditText myEditText = (EditText) findViewById(R.id.textBox);
runOnUiThread(new Runnable() {
#Override
public void run() {
myEditText.requestFocus();
// Also move the cursor to the end
myEditText.setSelection(myEditText.length());
}
});

How to pass value through Intent

HI all,
this is my class A, where on button click , i m sending a int variable to class B
Intent bgIntent = new Intent(Background.this, MainScreen.class);
bgIntent.putExtra("background", bgColor);
startActivity(bgIntent);
and on class B
Intent bgIntent = getIntent();
bgGlobal = bgIntent.getIntExtra("background",-1 );
if(bgGlobal == 0)
{
DetailsTextView.setBackgroundResource(R.color.a0);
}
else
if(bgGlobal == 1)
{
DetailsTextView.setBackgroundResource(R.color.a1);
}
But the problem is i am getting a blank view.My view is not coming up with textview.
is this proper to set background
"DetailsTextView.setBackgroundResource"???
If you want to change the color of a View use http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)
for example:
DetailsTextView.setBackgroundColor(getResources().getColor(R.color.txt_green));
Anyway, it's not clear if you want to change the screen's background or the textview's background.
Also
if(bgGlobal == 0){...} else ...
is wrong. You should do something of the like
if(bgGlobal != -1)
{
[Use intent to read color]
}else{
[set default color]
}
If you see a blank view it's possibly due to a wrong XML layout.
Edit: To retrieve the extra
getIntent().getExtras().getInt("background",-1);

Categories

Resources