I have two activities, Activity 1 and Activity 2. Activity 1 contains 8 buttons, each button sends some data to Activity 2 through intents and Activity 2 takes data and shows in listview. I want to show data in listview based on the condition so in Activity 2 I put every button data in a if blockSo my problem isWhat condition I should place in if(condition?) to know which button is clicked in Activity 1 my Activity 1
public void hmoral(View v){
moralref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
List<String> moraldata = new ArrayList<>();
Map<String,Object> map = task.getResult().getData();
for (Map.Entry<String,Object> entry:map.entrySet()){
moraldata.add(entry.getKey());
Log.d(TAG,entry.getKey());
}
Intent intent1 = new Intent(Activity1.this,Activity2.class);
intent1.putExtra("moraldata", (Serializable) moraldata);
startActivity(intent1);
}
}
});
}
public void hhorror(View v){
horrorref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
List<String> horrordata = new ArrayList<>();
Map<String,Object> map = task.getResult().getData();
for (Map.Entry<String,Object> entry:map.entrySet()){
horrordata.add(entry.getKey());
Log.d(TAG,entry.getKey());
}
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("horrordata", (Serializable) horrordata);
startActivity(intent);
}
}
});
}
}
code of Activity2 below:
public class ListAcivity extends AppCompatActivity {
ListView originallist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_acivity);
originallist = findViewById(R.id.originallist);
if (){
ArrayList<String> morallist = getIntent().getStringArrayListExtra("moraldata");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,morallist);
originallist.setAdapter(arrayAdapter);
}
if (){
ArrayList<String> horrorlist = getIntent().getStringArrayListExtra("horrordata");
ArrayAdapter<String>arrayAdapter1 = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,horrorlist);
originallist.setAdapter(arrayAdapter1);
}
}
how to know which button is clicked in other activity
Send Button id using intent to next Activity :
intent.putExtra("clicked_view_id", view.getId());
Now use clicked_view_id with switch case to compare with Button ids:
int clickedButtonID = getIntent().getIntExtra("clicked_view_id", 0);
switch(clickedButtonID){
case R.id.button1:
...
break;
case R.id.button2:
...
break;
}
In your activity1 :
Private String buttonText ="";
yourButtonClick.setOnClickListener(this);
#Override
public void onClick(View view) {
buttonText = getIntent().getStringExtra("btn_text");
switch (view.getId()) {
case R.id.button1:
buttonText = "buttonOne";
break;
case R.id.button2:
buttonText = "buttonTwo";
break;
case R.id.button8:
buttonText = "buttonEight";
break;
}
}
}
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("btnClicked", buttonText);
startActivity(intent);
// In list activity
String btnClicked = getIntent().getStringArrayListExtra("btnClicked");
Related
Hello I have an activity which shows some listviews, and I want them not to reload/refresh every time I get into it, as it is programmed to show different items every time.
But I want it not to refresh until a button which is in another activity is pushed.
I've not tried anything yet as I don't know what to start with.
Here I leave you the code of the java.class:
public class Comida extends AppCompatActivity implements Adaptador2.OnRecipeListener {
private RecyclerView recyclerView1;
List<Entidad2> listItems;
Adaptador2 adaptor;
private Entidad2 entidad1,entidad2,entidad3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String myValue = savedInstanceState.getString("key");
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_comida);
recyclerView1 = findViewById(R.id.lv_1);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView1.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);
listItems.add(entidad1);
listItems.add(entidad2);
listItems.add(entidad3);
adaptor = new Adaptador2(listItems, this);
recyclerView1.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
pickEntidad();
}
#Override
public void OnRecipe(int priority) {
if (priority == 20) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
if (priority == 50) {
Intent in = new Intent(this, Entrecot.class);
startActivity(in);
}
if (priority == 100) {
Intent in = new Intent(this, Hamburguesa.class);
startActivity(in);
}
}
private void pickEntidad(){
final int random = new Random().nextInt(101);
int priority1 = entidad1.getPriority();
int priority2 = entidad2.getPriority();
int priority3 = entidad3.getPriority();
listItems.clear();
if(random < priority1){
listItems.add(entidad1);
}else if(random < priority2){
listItems.add(entidad2);
}else if (random <= priority3){
listItems.add(entidad3);
}
adaptor.notifyDataSetChanged();
}
}
And then here there is the java.class of the other activity(the one which contains the button that has to refresh the other activity):
The button which I want to use to refresh the activity is the boton_prueba.
public class Menu extends AppCompatActivity {
Button boton_start;
Button boton_refresh;
Button boton_prueba;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
boton_start=(Button) findViewById(R.id.boton_platos);
boton_refresh = (Button) findViewById(R.id.boton_cambiarmenu);
boton_prueba=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Menu.this,Dishes.class);
startActivity(in);
}
});
boton_prueba.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Menu.this,Comida.class);
startActivity(in);
}
});
boton_refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to do?
}
});
}
}
Please if anyone has any idea of how to do it help me and in case you need more code or information just tell me.
Thank you.
If I really understand you then you want to execute pickEntidad() method when pressing in refresh button and go to this activity so you can do that using send any number or data with the intent like:
Intent in = new Intent(currentActivity.this,targetActivity.class);
in.putExtra("number",2);
startActivity(in);
and in target activity use somthing like this:
if(getIntent().getIntExtra("number",-1) ==2)
{
// what do you want to do...
}
I have 4 radiogroups (like a couple of yes or no) in Main3Activity and I would like these choices to be sent to another intent (Main4Activity). I use the button12 for change intent.
I tried to do this code
public class Main3Activity extends AppCompatActivity {
int snoring = 0;
int pressure = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button btnNew = (Button) findViewById(R.id.button12);
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
if(snoring == 1) {intent.putExtra("snoring", "1");} else {intent.putExtra("snoring", "0");}
if(pressure == 1) {intent.putExtra("pressure", "1");} else {intent.putExtra("pressure", "0");}
startActivity(intent);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioButton1:
if (checked)
snoring = 1;
break;
case R.id.radioButton2:
if (checked)
snoring = 0;
break;
case R.id.radioButton3:
if (checked)
pressure = 1;
break;
case R.id.radioButton4:
if (checked)
pressure = 0;
break;
}
}
}
In next intent i try to show the result with method similiar to this:
String snoring = getIntent().getExtras().getString("snoring");
String pressure= getIntent().getExtras().getString("pressure");
but "snoring" and "pressure" is always 0.
How can I recover snoring and pressure data from previous intent?
Just do-:
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(Main3Activity.this, Main4Activity.class);
intent.putExtra("snoring",snoring); //value that came from radio button
intent.putExtra("pressure",pressure) //value that came from radio button
startActivity(intent);
}
});
and on other activity-:
if(intent.hasEXtra("snoring))
{
get the snoring value
}
else if(intent.hasEXtra("pressure))
{
get the pressure value
}
So I have a Spinner and a EditText to login. The array of the Spinner owns "Anonymous" and " Owner" (means "Anonymous" is 0 and "Owner" is 1 in the array). When you choose "Anonymous" the password is "0000" and when you choose "Owner" the password is "1234".
But when I choose "Owner", the password "1234" is wrong and the Logcat shows "Anonymous". How can I make "Owner" selected? Maybe getSelectedItemPosition() is wrong?
My Code:
public class PinEnterActivity extends AppCompatActivity {
Button nextButton;
EditText pinEditText;
Spinner pinRoleSpinner = null;
private String TAG = "PinEnterActivity";
private Byte selectedUserRole = 0;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pin_enter);
nextButton = findViewById(R.id.nextActivity)
pinEditText = findViewById(R.id.pinET);
pinRoleSpinner = findViewById(R.id.roleSpinner);
selectedUserRole = (byte) pinRoleSpinner.getSelectedItemPosition();
switch (selectedUserRole) {
case 0:
Log.i(TAG, "Anonymous");
SharedPreferences sharedpreferences = getSharedPreferences("My_Prefs", 0);
final String password = sharedpreferences.getString("pass", "");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (pinEditText.getText().toString().equals("0000")) {
Intent intent = new Intent(PinEnterActivity.this, NextActivity.class);
startActivity(intent);
} else {
pinEditText.setError("Password incorrect");
Animation shake = AnimationUtils.loadAnimation(PinEnterActivity.this, R.anim.shake);
pinEditText.startAnimation(shake);
return;
}
}
});
break;
case 1:
Log.i(TAG, "Owner");
SharedPreferences preferences = getSharedPreferences("My_Prefs", 0);
final String password2 = preferences.getString("pass", "");
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (pinEditText.getText().toString().equals("1234")) {
Intent intent = new Intent(PinEnterActivity.this, NextActivity.class);
startActivity(intent);
}else{
pinEditText.setError("Password incorrect");
Animation shake = AnimationUtils.loadAnimation(PinEnterActivity.this, R.anim.shake);
pinEditText.startAnimation(shake);
return;
}
}
});
}
}
I finally found out the right answer.
pinRoleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(selectedUserRole) {
case 0:
Log.i(TAG, "Anonymous");
// Code
break;
case 1:
Log.i(TAG, "Owner");
// Code
break;
default;
// Code
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I added a variable that pass form the first activity to the second one.
I want to use the info that has accepted from the first activity, at the new activity, and will save it on new double variable, display it as permanent on a textView.
Now, it appears only when I am clicking on the regular button that start the new activity.
As first step, I guess, I need to remove - "startActivity(intent1);".
How should I move on from here?
Java code:
First Activity (Name : settings.java)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
public void onClick (View v){
Intent intent = new Intent(settings.this, WaitressRecord.class);
startActivity(intent);
}
protected void onClickWait (View v) {
//--- Casting & Converting EditText "etSalaryWaitress" to Double "doubleSW".
btnWaitress =(Button)findViewById(R.id.btnWaitress);
etSalaryWaitress = (EditText) findViewById(R.id.etSalaryWaitress);
doubleSW = Double.parseDouble(etSalaryWaitress.getText().toString());
//---Casting Radio Button(s).
rbPercentage = (RadioButton)findViewById(R.id.rbPercentage);
rbShekel = (RadioButton)findViewById(R.id.rbShekel);
if (doubleSW < 100 ) {
if (rbPercentage.isChecked()) {
HafrashaP = 1 - (doubleSW / 100.0);
strHafPer = String.valueOf(HafrashaP);
Toast.makeText(settings.this, strHafPer, Toast.LENGTH_SHORT).show();
// start the SecondActivity
Intent intent1 = new Intent(this, WaitressRecord.class);
intent1.putExtra(Intent.EXTRA_TEXT, strHafPer);
startActivity(intent1);
} else if (rbShekel.isChecked()) {
HafrashaS = -doubleSW;
strHafShek = String.valueOf(HafrashaS);
Toast.makeText(settings.this, strHafShek, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(settings.this, "לא הוזנה סוג ההפרשה", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(settings.this, "מספר שגוי", Toast.LENGTH_SHORT).show();
}
}
New Activity: (Name : WaitressRecord.java)
public class WaitressRecord extends AppCompatActivity {
String strHafPer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waitress_record);
// get the text from MainActivity
Intent intent1 = getIntent();
strHafPer = intent1.getStringExtra(Intent.EXTRA_TEXT);
// use the text in a TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(strHafPer);
}
}
//First Activity
Intent intent= new Intent(this, SecondActivity.class);
Bundle extra = new Bundle();
mBundle.putString(VARIABLE_KEY, value);
intent.putExtras(mBundle);
startActivity(intent);
//on the second Acitivty
intent intent = getIntent();
Bundle bundleExtra;
//Null Checking
if (intent != null ) {
bundleExtra = getIntent().getExtras();
// Be sure your check your "VARIABLE KEY SAME AS in THE FIRST ACTIVITY
String resultString = extras.getString("VARIABLE_KEY");
}
I'm making a custom dialer and everything till now was perfect. Now while testing, I saw that the "#" sign was not working properly. It shows on the dialer but while calling, it goes away. For eg, if I dial *121#, it becomes *121 when the call is being made through the stock dialer. Here is the code for my activity-
public class DialPadActivity extends Activity {
private EditText numberField = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeButtonArrowClickListeners();
}
public void dialButtonClick(View v) {
int buttonId = v.getId();
putNumberToEditText(buttonId);
}
public void buttonPhone_click(View v) {
if (numberField != null) {
String phone = numberField.getText().toString();
String uriString = "tel:" + phone;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uriString));
startActivity(intent);
}
}
public void putNumberToEditText(int buttonId) {
switch (buttonId) {
case R.id.dial_button0:
putNumber("0");
break;
case R.id.dial_button1:
putNumber("1");
break;
case R.id.dial_button2:
putNumber("2");
break;
case R.id.dial_button3:
putNumber("3");
break;
case R.id.dial_button4:
putNumber("4");
break;
case R.id.dial_button5:
putNumber("5");
break;
case R.id.dial_button6:
putNumber("6");
break;
case R.id.dial_button7:
putNumber("7");
break;
case R.id.dial_button8:
putNumber("8");
break;
case R.id.dial_button9:
putNumber("9");
break;
case R.id.dial_button_s:
putNumber("*");
break;
case R.id.dial_button_p:
putNumber("#");
break;
}
}
public void putNumber(String number) {
if (numberField == null) {
numberField = (EditText) findViewById(R.id.phone_number_field);
}
numberField.append(number);
}
private void initializeButtonArrowClickListeners() {
ImageButton buttonArrow = (ImageButton) findViewById(R.id.button_arrow);
buttonArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (numberField != null && numberField.length() > 0) {
String previousNumbers = numberField.getText().toString();
String numbersMinusTheLast = previousNumbers.substring(0,
numberField.length() - 1);
numberField.setText(numbersMinusTheLast);
}
}
});
buttonArrow.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (numberField != null) {
numberField.setText("");
}
return false;
}
});
}
Where am I doing the wrong?
Use URLEncoder.encode(string, "UTF-8");
For example:
String uriString = "tel:" + URLEncoder.encode(phone, "UTF-8");
Replace your # symbol with %23.
That is: *121%2311 instead of *121#11
String uri = "tel:" + "*6133%235345";
Intent intent;
intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);
the output for this is : *6133#5345