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

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

Related

How can I make a button in one activity to modify components in other

I'm trying to make some layouts and listviews gone and visible by pressing a button located in another activity.
Also I want to input some text in an edittext located in the same activity as the layouts
I've looked all over this place and I found several examples but I just couldn't make them work so I'm posting my own code.
btnnext1.setOnClickListener(new OnClickListener() {
val next1 = Intent(this, next1::class.java)
startActivity(next1)
categories.visibility = View.GONE
listacats.visibility = View.VISIBLE
ListView.visibility = View.VISIBLE
e_search.setText("blackcat")
})
From what I found out this can be achieved by using intent.PutExtra, but I just couldn't make it work.
Thank you in advance for your support
The main task is pass "blackcat" to another activity, you could do like this:
In MainActivity:
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("passed_str", "blackcat");
startActivity(intent);
}
});
In OtherActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
String receivedStr = getIntent().getStringExtra("passed_str");//receive string
final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(receivedStr);
categories.setVisibility(View.GONE); //change wiget's visibility
listacats.setVisibility(View.INVISIBLE);
ListView.setVisibility(View.INVISIBLE);
}

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

Does android have a server where users of my app can view what other users post to app?

I am tyring to figure out how to make an app that lets the users of the app post items for sale with pictures where other users that search it can view those items. For example something like craigslist for android. I want to set up an app where users can fill in edit texts like Category, Title, Price, Description and a Picture of the item they want to post for sale. How can I achieve this so users can search for items as well. From what I understand I have to take what a user is inputting for sale and send to a server so others can search it, right? Can anyone direct in a direction to get started, this is what I have for a user to post an item:
public class Post extends Activity implements OnClickListener {
ImageButton ibutton1;
ImageButton ibutton2;
ImageButton ibutton3;
ImageButton ibutton4;
ImageButton ibutton5;
ImageButton ibutton6;
Intent i1;
Intent i2;
Intent i3;
Intent i4;
Intent i5;
Intent i6;
final static int cameraData1 = 1;
final static int cameraData2 = 2;
final static int cameraData3 = 3;
final static int cameraData4 = 4;
final static int cameraData5 = 5;
final static int cameraData6 = 6;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
Button button = (Button) findViewById(R.id.bNext);
button.setOnClickListener(mNextListener);
// Category spinner set up
Spinner cSpinner = (Spinner) findViewById(R.id.spCategoryPost);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.category_array,
android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cSpinner.setAdapter(adapter2);
cSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener2());
initialize();
}
private void initialize() {
ibutton1 = (ImageButton) findViewById(R.id.imageButton1);
ibutton2 = (ImageButton) findViewById(R.id.imageButton2);
ibutton3 = (ImageButton) findViewById(R.id.imageButton3);
ibutton4 = (ImageButton) findViewById(R.id.imageButton4);
ibutton5 = (ImageButton) findViewById(R.id.imageButton5);
ibutton6 = (ImageButton) findViewById(R.id.imageButton6);
ibutton1.setOnClickListener(this);
ibutton2.setOnClickListener(this);
ibutton3.setOnClickListener(this);
ibutton4.setOnClickListener(this);
ibutton5.setOnClickListener(this);
ibutton6.setOnClickListener(this);
}
public void onClick(View Images) {
switch (Images.getId()) {
case R.id.imageButton1:
i1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i1, cameraData1);
break;
case R.id.imageButton2:
i2 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i2, cameraData2);
break;
case R.id.imageButton3:
i3 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i3, cameraData3);
break;
case R.id.imageButton4:
i4 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i4, cameraData4);
break;
case R.id.imageButton5:
i5 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i5, cameraData5);
break;
case R.id.imageButton6:
i6 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i6, cameraData6);
break;
}
}
private OnClickListener mNextListener = new OnClickListener() {
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.etTitle);
EditText editP = (EditText) findViewById(R.id.etPrice);
EditText editD = (EditText) findViewById(R.id.etDescription);
long currentTime = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTime);
String showTime=String.format("%1$tI:%1$tM:%1$tS %1$Tp",cal);
Intent intent = new Intent(Post.this, PostSet.class);
intent.putExtra("com.mandam.isellibuy.TITLE", edit.getText().toString());
intent.putExtra("com.mandam.isellibuy.PRICE", editP.getText().toString());
intent.putExtra("com.mandam.isellibuy.DESC", editD.getText().toString());
intent.putExtra("currentTime", currentTime);
startActivity(intent);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
ibutton1.setImageBitmap(bmp);
}
}
}
Now I take the value of all these edit texts, spinner, and images and return it to another activity where the user can view all of this to make sure it looks good and post the item for sale, here is the activity:
public class PostSet extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post_set);
// Setting text of edit text tvTitle from class Post
TextView tv1 = (TextView) findViewById (R.id.tvTitleText);
TextView tv2 = (TextView) findViewById (R.id.tvPrice);
TextView tv3 = (TextView) findViewById (R.id.tvTimeStamp);
TextView tv4 = (TextView) findViewById (R.id.tvDescription);
Intent intent = getIntent();
String edit = intent.getStringExtra("com.mandam.isellibuy.TITLE");
String editP = intent.getStringExtra("com.mandam.isellibuy.PRICE");
String editD = intent.getStringExtra("com.mandam.isellibuy.DESC");
//long currentTime = extras.getLong("currentTime");
tv1.setText(edit);
tv2.setText(editP);
tv4.setText(editD);
//tv3.setText(currentTime);
}
}
Now this Activity just displays all the input from the user to look it over and there is a button "Submit" that the user clicks to submit the item for sale so others can view the item on my app. This is where I get confused how do I achieve that?
So the answer to your question in the title "does Android have a server..." is no. There's no server that Google provides specifically for Android. However, Android can do standard network communication--you can use any accessible server of your choice. There are plenty of free (tiered) servers including Google App Engine, Heroku and AWS. You'll have to create your own server-side component though.
You're probably getting downvotes because the actual body of your question is extremely broad. I could probably reword your question as "How can I create Craigslist but for Android?". There's a lot involved and there are a huge range of possibilities and differing requirements. You need to break down your problem into pieces, fill in what you can and then if you have any specific technical questions about pieces of that, then ask those here on StackOverflow.
I will try to help you get started in the right direction as you asked though, you should take a look at learning how to implement a web service, then create one to post your listings to and to retrieve listings. In that web service you'll have to interpret the request, and most likely save the data into a database. Likewise when retrieving listings, you'll have to go to the database to retrieve previously stored listings, potentially filter/sort them and then return them in some form back.
This is not a trivial task, good luck.

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

Categories

Resources