I made one TextView clickable. When it's clicked a new Intent is started.
articleURL [i].setText( articleURLArr [i] );
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent intent = new Intent(getActivity().getBaseContext(), WebViewing.class);
intent.putExtra("sourceURL", ((TextView) v).getText().toString());
startActivity(intent);
}
});
But now I wanted to pass another value to the intent. source[i]. So I tried it this way
i.putExtra("source" , ((TextView) source[v.getId()]).getText().toString());
But that gives me an ArrayIndexOutOfBoundsException with index= -1.
How can I pass another value to the intent when I handle the onclick?
In my opinion you have two options to solve this:
1- Use i inside your click listener. To do it you need to assign it to a final variable.
final int index = i;
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent i = new Intent(getActivity().getBaseContext(), WebViewing.class);
i.putExtra("sourceURL", ((TextView) v).getText().toString());
i.putExtra("source" , ((TextView) source[index]).getText().toString());
startActivity(i);
}
});
2- Store the index in the view's tag (not id):
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println ( ((TextView) v).getText().toString() );
Intent i = new Intent(getActivity().getBaseContext(), WebViewing.class);
i.putExtra("sourceURL", ((TextView) v).getText().toString());
i.putExtra("source" , ((TextView) source[Integer.parseInt(v.getTag())]).getText().toString());
startActivity(i);
}
});
Related
What I am trying here is, I want to get the string value of flevel and then when I click the search button it will proceed to the next (specific) Activity.
Goal is:
if the flevel is "Beginner" the next activity will be for the "Beginner", and if the flevel is "Experienced" the next activity will be for the "Experienced", and so on..
flevelfb = FilipinoBeginner
Bundle bnfb2 = getIntent().getExtras();
String flevelfb = bnfb2.getString("flevel");
flevel.setText(String.valueOf(flevelfb));
flevelfe = FilipinoExperienced
Bundle bnfe2 = getIntent().getExtras();
String flevelfe = bnfe2.getString("flevel");
flevel.setText(String.valueOf(flevelfe));
This part is for the search button:
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String flevels = flevel.getText().toString();
if (flevels.equals(flevelfb)){
Intent intent= new Intent(getApplicationContext(), FilipinoBeginner.class);
startActivity(intent);
}
else if (flevels.equals(flevelfe)){
Intent intent= new Intent(getApplicationContext(), FilipinoExperienced.class);
startActivity(intent);
}
}
});
you can change just search button click like this
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String flevels = flevel.getText().toString();
Intent intent = null;
if (flevels.equals("Beginner")){
intent = new Intent(getApplicationContext(), FilipinoBeginner.class);
}
else if (flevels.equals("Experienced")){
intent = new Intent(getApplicationContext(), FilipinoExperienced.class);
}
startActivity(intent);
}
});
how do I prevention the status bar from loosing it's color while a bottom sheet is showing up?
I use a bottom sheet view with custom layout.
tried modify the style of the dialog but still no luck
here is my code
if (position == 1) {
View modalbottomsheet = getActivity().getLayoutInflater().inflate(R.layout.others_sheet, null);
dialog = new BottomSheetDialog(getActivity());
dialog.setContentView(modalbottomsheet);
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
dialog.show();
contact_num = (EditText) modalbottomsheet.findViewById(editText);
choose_contact = (Button) modalbottomsheet.findViewById(choose);
choose_contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == choose_contact.getId()) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
}
});
hidetext = (EditText) modalbottomsheet.findViewById(R.id.hidetext);
recharge_btn = (Button) modalbottomsheet.findViewById(R.id.rechargeothers_btn);
recharge_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId()== recharge_btn.getId()){
String phone = ((EditText) dialog.findViewById(R.id.editText)).getText().toString();
String hidden = ((EditText) dialog.findViewById(R.id.hidetext)).getText().toString();
String pre = "*805*2";
String hash = "%23";
String dail = "tel:" + pre + phone + hidden + hash;
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dail)));
dialog.hide();
}
}
});
}
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 want to send integer value through intents , and access it on another activity ... i tried using the following code .. but its not working..!!
mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
mCheatButton.setVisibility(View.INVISIBLE);
mIsCheater = false;
int mCurrentIndex=2;
Intent i = new Intent(quizactivity.this, CheatActivity.class);
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, mCurrentIndex);
startActivityForResult(i, 0);
}
catch (Exception e)
{
}
}
});
SECOND ACTIVITY:
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(extras == null)
{ mAnswerIsTrue= 0;
updateanswer(mAnswerIsTrue);
}
else
{
mAnswerIsTrue= extras.getInt(EXTRA_ANSWER_IS_TRUE);
updateanswer(mAnswerIsTrue);
}
updateanswer(mAnswerIsTrue);
}
});
public void updateanswer(int q){
mAnswerTextView.setText(solution[q].getA());
setAnswerShownResult(true);
}
please help me solve it out..!!
i want to send integer value through intents , and access it on another activity ... i tried using the following code .. but its not working..!!
what is the error in above code.. i cant really understand...!!
Your problem is on your second activity, I guess.
Intent extras = getIntent();
int yourInt = extras.getIntExtra(EXTRA_ANSWER_IS_TRUE, -1);
In you second activity you need to do
int i = getIntent().getExtras().getInt(CheatActivity.EXTRA_ANSWER_IS_TRUE)
I create an activity with dynamic buttons in a loop. I get a list and create a button for each element in the list. The buttons go to the same activity afterward, but with each button I want to pass different string.
I did this in the loop:
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
But at the end, all the buttons get the same string (the string of the last button).
========================================
full code:
try {
JsonObject respObject = jsonParser.parse(response).getAsJsonObject();
JsonArray tourListArray = respObject.getAsJsonArray("tours");
System.out.println("tourListArray: " + tourListArray.toString());
for(int i = 0; i < tourListArray.size(); i++){
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
tour = tourListArray.get(i).getAsJsonObject();
String tourCode = tour.get("tourcode").getAsString();
Button tour_button = new Button(this);
tour_button.setText("Tour Code: " + tourCode);
tour_button.setGravity(Gravity.LEFT);
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, info);
startActivity(intent);
}
});
ll.addView(tour_button);
LinearLayout yourLL = (LinearLayout) findViewById(R.id.Tours_List);
yourLL.setOrientation(LinearLayout.VERTICAL);
yourLL.addView(ll);
}
} catch (JsonIOException e) {
e.printStackTrace();
}
When you create the button you can:
button.setTag(someString);
and then in the onClick you can:
public void onClick(View v) {
Intent intent = new Intent(TourListActivity.this,
TourMenuActivity.class);
String info = tour.toString();
intent.putExtra(TOUR_INFO, ((Button)v).getTag());
startActivity(intent);
}
The variable tour is defined outside the loop, so each button share the same variable.
At each iteration, you just change the reference stored by this variable.
You could to create a final variable inside your loop, and use it inside the OnClickListener:
for (int i = 0; i < tourListArray.size(); i++) {
...
final String tourInfo = tour.info;
tour_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
TourListActivity.this,
TourMenuActivity.class
);
intent.putExtra(TOUR_INFO, tourInfo);
startActivity(intent);
}
});
...
}