I got the first-page working but got a youtube tutorial for the second but can't seem to get it working the way I want it to.the pages are done differently.
This is the working page
private void setSingleEvent(GridLayout mainGrid) {
//Loop all child item of Main Grid
for (int i = 0; i < mainGrid.getChildCount(); i++) {
//You can see , all child item is CardView , so we just cast object to CardView
CardView cardView = (CardView) mainGrid.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finalI == 0) // open page 1
{
Intent intent = new Intent (MainActivity.this, StarterVapes.class);
startActivity(intent);
}
else if (finalI == 1) // open page 2
{
Intent intent = new Intent (MainActivity.this, UpgradeVapes.class);
startActivity(intent);
}
This is the nonworking page
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
viewHolder.imgView.setImageResource(imagesList.get(i));
viewHolder.txtTitle.setText(titleList.get(i));
final int finalI = i;
viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finalI == 0) // open page 1
{
Intent intent = new Intent (context, AboutUS.class);
context.startActivity(intent);
}
else if (finalI == 1) // open page 2
{
Intent intent = new Intent (context.this, AboutUS.class);
context.startActivity(intent);
}
// Intent intent = new Intent(context, DetailActivity.class);
// intent.putExtra("title", titleList.get(i));
// context.startActivity(intent);
}
});
I want it to were you click on the first tile it goes to a page, you click on the second tile it goes to a different page and so on.
Got it working
Heading
````
if (finalI == 0) // open page 1
{
Intent intent = new Intent (context, AboutUS.class);
intent.putExtra("title", titleList.get(i));
context.startActivity(intent);
}
````
Related
So, I have an activity with 2 buttons marsPhoto and viewByDate. After the buttons are clicked they go to a new activity page where I want to take the name from the putExtra (Daily or Date) and printout Daily or Date in the TextView. I only want to set the text to match the string for the button (which is Daily for intent2 clicked and Date for intent3 clicked).
Activity 1
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("Daily",1);
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("Date",2);
startActivity(intent3);
break;
}
}
Activity 2
TextView type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
Intent i = getIntent();
String s1 = i.getStringExtra("Daily");
String s2 = i.getStringExtra("Date");
type.setText(s1);
}
Change the key for the passed StringExtra to something like "button_text" and set the value to the text you want to display, i.e. "Date" or "Daily". There doesn't seem to be any meaning to the integer you are transferring.
modify the first code to this
Activity 1
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("dataSent","Daily");
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("dataSent","Date");
startActivity(intent3);
break;
}
}
Activity 2
TextView type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
String s1="";
Bundle extras = getIntent().getExtras();
if(extras == null) {
s1 = "";
} else {
s1 = extras.getString("dataSent","");
}
type.setText(s1);
}
I'm building a small app that scans barcodes and put them in a database (using ZXing library). After the scan is taken, and the barcode doesn't exist in the database I want to show a popup dialog that allows an user to enter the name manually.
The dialog works perfectly if it's invoked in an onClickListener, however when I try to put it in an 'if' statement within another method (which checks if the scan result is in the database) the dialog doesn't show.
What's strange, I put tags in the beginning of the method and at the end, both of them are shown in the log, but the alertdialog doesn't appear and it gets me back to the main activity without any error...
public void popUP (String barcode) {
Log.d(TAG, "method started");
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_input, null);
TextView tv = (TextView) mView.findViewById(R.id.textView2);
final EditText et = (EditText) mView.findViewById(R.id.item1);
Button bt1 = (Button) mView.findViewById(R.id.button2);
Button bt2 = (Button) mView.findViewById(R.id.button3);
final String code = barcode1;
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mydb.addNewItem(et.getText().toString(), code);
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
Log.d(TAG,"dialog shown");
}
And here is the method from which I try to inflate the popUP
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
final String barcode = scanResult.getContents();
if (scanResult != null) {
if(scanResult.getContents() == null) {
Log.d(TAG, "Cancelled Scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d(TAG, "SCANNED");
int rows = mydb.checkRows(barcode);
if (rows == -1){
Log.d(TAG, "popUP should appear");
popUP(barcode); //INVOKING THE ALERT DIALOG
} else {
Log.d(TAG, "found and attempting adding");
mydb.addItem(barcode, rows);
}
}
} else {
Toast.makeText(this, "oddal zero", Toast.LENGTH_LONG);
}
Intent intynt = new Intent(this, MainActivity.class);
startActivity(intynt);
}
I have created two activities:
Play
MorpionAI
decide and checkedplayer are both some integers.
How can I make my app so that when the button is clicked the app selects and calls one of the intents based on following:
if(decide==checkedplayer){
public void MorpionAI_Acitivity(View view) {
Clean();
deleteplayer();
Intent play = new Intent(this, MorpionAI.class);
startActivity(play);
}}
else{
public void Play_Activity(View view) {
Clean();
deleteplayer();
Intent play = new Intent(this, Play.class);
startActivity(play);
}}
I tried to do this using following code:
morpionbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (decide==checkedplayer){
Clean();
deleteplayer();
Intent play = new Intent(view.getContext(), Play.class);
startActivity(play);
}
else{
Clean();
deleteplayer();
Intent play = new Intent(view.getContext(), MorpionAI.class);
startActivity(play);
}
}
});
But it did not worked.
In your activity , defined a Context class field :
private Context mContext;
in the onCreate() function of this activity , initialize the field :
mContext = this;
then
morpionbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (decide == checkedplayer){
Clean();
deleteplayer();
Intent play = new Intent(mContext, Play.class);
mContext.startActivity(play);
}
else {
Clean();
deleteplayer();
Intent play = new Intent(mContext, MorpionAI.class);
mContext.startActivity(play);
}
}
});
Hope this helps.
I have this function that call RadioButton value from the Group.java to the Add.java. I use the same function on another activity called Status.java. Now, every time I click from either Group.java or Status.java, the result become duplicate. And every time I click the RadioButton, my EditText will dissappear.
Group.java
RadioGroup radiog1;
RadioButton radio1, radio2, radio3, radio4, radio5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
radiog1 = (RadioGroup) findViewById(R.id.radiog1);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radio5 = (RadioButton) findViewById(R.id.radio5);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
radio1.setOnClickListener(this);
radio2.setOnClickListener(this);
radio3.setOnClickListener(this);
radio4.setOnClickListener(this);
radio5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.radio1):
returnIntent.putExtra("GroupTag", "" + radio1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio2):
returnIntent.putExtra("GroupTag","" + radio2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio3):
returnIntent.putExtra("GroupTag", "" + radio3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio4):
returnIntent.putExtra("GroupTag", "" + radio4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio5):
returnIntent.putExtra("GroupTag","" + radio5.getText());
setResult(RESULT_OK,returnIntent);
finish();
break;
}
}}
Status.java
RadioButton rb1, rb2, rb3, rb4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb4 = (RadioButton) findViewById(R.id.rb4);
rb1.setOnClickListener(this);
rb2.setOnClickListener(this);
rb3.setOnClickListener(this);
rb4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.rb1):
returnIntent.putExtra("StatusTag", "" + rb1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb2):
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb3):
returnIntent.putExtra("StatusTag", "" + rb3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb4):
returnIntent.putExtra("StatusTag","" + rb4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
}
}}
Add.java
ImageButton ibtn, ibtn2, ibtn3, ibtn4,ibtn5;
TextView tvgroup;
TextView tvstatus;
int groupRequestCode;
int statusRequestCode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
tvgroup = (TextView) findViewById(R.id.tvgroup);
tvstatus = (TextView) findViewById(R.id.tvstatus);
ibtn = (ImageButton) findViewById(R.id.ibtn);
ibtn2 = (ImageButton) findViewById(R.id.ibtn2);
ibtn3 = (ImageButton) findViewById(R.id.ibtn3);
ibtn4 = (ImageButton) findViewById(R.id.ibtn4);
ibtn5 = (ImageButton) findViewById(R.id.ibtn5);
ibtn.setOnClickListener(this);
ibtn2.setOnClickListener(this);
ibtn3.setOnClickListener(this);
ibtn4.setOnClickListener(this);
ibtn5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.ibtn):
startActivityForResult(new Intent(this, Group.class), groupRequestCode);
break;
case (R.id.ibtn2):
startActivity(new Intent(this, Due_Date.class));
break;
case (R.id.ibtn3):
startActivity(new Intent(this,DueTime.class));
break;
case (R.id.ibtn4):
startActivityForResult(new Intent(this, Status.class), statusRequestCode);
break;
case (R.id.ibtn5):
startActivity(new Intent(this,Assignees.class));
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}}
The result :
Any kind of help would really be appreciated.
I suggest you to use startActivityForResult() method to pass data between activities.
So you need to change your code like that:
In Add.java
1) Remove this code:
Bundle extra = getIntent().getExtras();
if (extra != null) {
String textViewName = extra.getString("SomeTag");
tvgroup.setText(textViewName);
}
Bundle extra2 = getIntent().getExtras();
if (extra2 != null) {
String status = extra2.getString("SomeTag");
tvstatus.setText(status);
}
2)
Change startActivity(new Intent(this,Group.class));
to startActivityForResult(new Intent(this,Group.class), groupRequestCode);
Also startActivity(new Intent(this,Status.class));
to startActivityForResult(new Intent(this,Status.class), statusRequestCode);
PS: groupRequestCode should be different from statusRequestCode ( for example 1 and 2).
3) Overrid onAcitivtyResult() method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}
PS: you can use switch instead of if blocs if you have many requestcode.
In Group.java
Change
Intent intent = new Intent(Group.this, Add.class);
intent.putExtra("SomeTag", "" + radio1.getText());
startActivity(intent);
to
Intent returnIntent = getIntent();
returnIntent.putExtra("GroupTag","" + radio1.getText());
setResult(RESULT_OK,returnIntent);
finish();
=> Do the same thing for the rest of the RadioButtons
In Status.java
Change
Intent intent2 = new Intent(Status.this, Add.class);
intent2.putExtra("SomeTag", "" + rb2.getText());
startActivity(intent2);
to
Intent returnIntent = getIntent();
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
=> Do the same thing for the rest of the RadioButtons
I have my main activity using the startActivityForResult method which calls an activity that i need to return two string values from. I have it working to return one, but even with all the tutorials and other questions on here i have read i cant seem to get it to return the two values. Below is my code.
here is where i start the second activity:
Button button = (Button) findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(addM, 1);
}
});
Here is the activity it starts, i need to return the text that is in the titleField(which works now) and the yearField
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.setData(Uri.parse(titleField.getText().toString()));
setResult(RESULT_OK, data);
//data.setData(Uri.parse(yearField.getText().toString()));
//setResult(RESULT_OK, data);
finish();
}
});
}
}
Here is the method in my main class that receives results
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getData().toString();
//tempYear = data.getStringExtra("movieYear");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
The code that is commented out was one attempt at making it receive multiple values, although they failed. Any help with this situation would be great. Thanks!
You should use something like:
Intent returnIntent = new Intent();
returnIntent.putExtra("title",titleField.getText().toString());
returnIntent.putExtra("year",yearField.getText().toString());
setResult(RESULT_OK,returnIntent);
finish();
And on your main activity, onActivityResult:
tempTitle = data.getStringExtra("title");
tempYear = data.getStringExtra("year");
use data.putExtra("keys", data); instead of data.setData(.....)
and get data like;
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
You should be able be put extras on your intent and get that data through an associated tag. This is how I'm doing it from an Activity's onCreate.
Intent mIntent = new Intent(this, MainActivity.class);
mIntent.putExtra("your_tag", "the string you want to get");
startActivity(mIntent);
finish();
Then in the activity you want that data (the MainActivity or whatever you're calling it)....
Intent intent = getIntent();
String value = intent.getExtras().getString("your_tag");
It may also be worth noting that you can Override the onNewIntent in your MainActivity to handle new Intents coming in, mine currently looks like...
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Try this
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("title_field", titleField.getText().toString);
data.putExtra("year_field", yearField.getText().toString);
setResult(RESULT_OK, data);
finish();
}
});
}
And this
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getStringExtra("title_field");
tempYear = data.getStringExtra("year_field");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
You can use a putExtra method on your Intent:
data.putExtra("title", movieTitle);
data.putExtra("year", movieYear);
Then in the Activity that opens, you can get this data as follows:
String title, year;
Intent data = getIntent();
if (data != null) {
title = data.getExtras().getString("title");
year = data.getExtras().getString("year");
}
You can create a new intent and then add your two variables
Intent i = new Intent(getApplicationContext(), asd8.as.hwapp.timemonday.class);
i.putExtra("username",username);
i.putExtra("password", password);
startActivity(i);
And then retrieving the information in your new activity as so;
public void getInformation(){
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("username");
password = extras.getString("password");
}
}