How to make EditText regain focus? - android

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());
}
});

Related

How to check if a button is pressed in another activity?

In my first Activity I made that you can open a pop-up window (it's another activity), where you can put some data in and it needs to be sent back to the original Activity. Since I will need to put the data I got into a ListView, I think I need to check if the button is pressend and in the onClick add to the ListView. How to do that?
Here's the code of the pop-up Activity:
public class AddCoin extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
final Intent intentAdd = new Intent(this, PortfolioActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.add_coin_window);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8), (int)(height*0.4));
Button add_coin = findViewById(R.id.add_toport_button);
add_coin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText inputNameText = (EditText) findViewById(R.id.text_input_name);
EditText inputPriceText = (EditText) findViewById(R.id.text_input_price);
EditText inputAmmountText = (EditText) findViewById(R.id.text_input_ammount);
String inputName = inputNameText.getText().toString();
String inputPrice = inputPriceText.getText().toString();
String inputAmmount = inputAmmountText.getText().toString();
intentAdd.putExtra("Name", inputName);
intentAdd.putExtra("Price", inputPrice);
intentAdd.putExtra("Ammount", inputAmmount);
finish();
}
});
}
}
Proper way is calling activity with startActivityForResult().
But if you can not do this you can use EventBus
You can achieve this nature using startActivityForResult() method of Activity class.
Refer this link startActivityForResult
You have to use startActivityForResult(), put data into an Intent on called activity and then get results back using onActivityResult(int requestCode, int resultCode, Intent data) on the calling activity

Android Change ImageButton image

I write an travel APP.There are 20 QR Code in every spot.
When tourist use QR Code scanner scan the QR Code,the ImageButton's image have to change into another image.
The problem on this line : spot1.setImageResource(R.drawable.hotspot1);
If I delete this line, there is no problem.
I don't know how to fix it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (0 == requestCode && null != data && data.getExtras() != null) {
String result = data.getExtras().getString("la.droid.qr.result");
int spotnum=Integer.valueOf(result);
switch(spotnum){
case 1:
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
setContentView(R.layout.hotspot1);
break;
case 2:
setContentView(R.layout.hotspot2);
break;
}
}
}
Here is my Logcat:http://i.stack.imgur.com/6y2UQ.png
You can not initialize any Views from xml before calling setContentView :
setContentView(R.layout.hotspot1);
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
You are not suppose to call setContentView after setting view resource. call set contentView once in onCreate without setting any view from xml.
You can then change the layout contents, but do not call setContentView again.
You are using a image view, but not calling setContentView before it. This makes the ImageView as null, hence the error.
Try following above suggestions,and it will go. Happy Coding.

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

How can I calculate the values entered in edittext and transfer from one activity to another?

I am devoloping a roof calculater. i have three edittext and calculate button in the first activity. In the second activity i have a textview that shows the result.
EditText editText1Değer1Kod;
EditText editText2Değer2Kod;
EditText editText3Değer3Kod;
Button hesaplaKod;
I want to take these three value then calculate with formula then show the result in second activity as a textview.
How can i calculate (sum or add this three value) then show in the second activity?
Use in first Activity
hesaplaKod.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int value1= Integer.parseInt(EditText01.getText().toString());
int value2= Integer.parseInt(EditText01.getText().toString());
int value3= Integer.parseInt(EditText01.getText().toString());
int add = value1+value2+value3;
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("AddValue", add);
startActivity(intent);
}
});
And in onCreate of Second Activity get the value from this
int addvalue = getIntent().getExtras().getInt("AddValue");
enter code herejust pass calculate the number and pass the using intent to other activity
in First Activity :
Intent intent=new Intent(IntentsDemo2.this,Activity2.class);
intent.putExtra("result", result);
startActivity(intent);
in Second Activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Intent sender=getIntent();
int result =sender.getExtras().getInt("result");
}

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());

Categories

Resources