I have some photos in drawable and view all one by one in ImageView
by create 3 buttons
next
back
Share Image
now I want to share photo that in ImageView to whatsapp .
I want to share directly without saving it.
I tried to implement this but it sent some time file to whatsapp
so I deleted my code and needed help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final Button bt_next = (Button) findViewById(R.id.btn_next);
final Button bt_back = (Button) findViewById(R.id.btn_back);
Random rnd = new Random();
int R = rnd.nextInt(6);
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(R)),"drawable",getPackageName()));
bt_next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (photo_number <6){
photo_number ++;
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(photo_number)),"drawable",getPackageName()));
bt_back.setEnabled(true);
}else
{
bt_next.setEnabled(false);
}
}
});
bt_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (photo_number >1){
photo_number --;
imageView.setImageResource(getResources().getIdentifier("s".concat(String.valueOf(photo_number)),"drawable",getPackageName()));
bt_next.setEnabled(true);
}else
{
bt_back.setEnabled(false);
}
}
});
}
public void btn_share (View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
Uri uri = Uri.parse(String.format("android.resource://" + getPackageName() + R.id.imageView ));
intent.putExtra(Intent.EXTRA_INDEX, uri);
intent.putExtra(Intent.EXTRA_TEXT, "share photo");
Intent chooser = Intent.createChooser(intent, "مشاركة الصورة");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}
}
Related
I have six images. If I press the first image intent is working. But if I press the third image firstly it is not working. it is working in order.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
You are setting other view click listener inside R.id.indoor_activities
In above code, you are saying that assign click listener every time to outdoor_activities if user click on indoor_activities first.
To fix this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
});// click listener for indoor activities
// click listener for outdoor activities when onCreated is called
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
});
I think you should do like this.This will certainly works as your requirement.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.indoor_activities);
imageView1.bringToFront();
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent1 = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent1);
}
});
ImageView imageView2 = (ImageView) findViewById(R.id.outdoor_activities);
imageView2.bringToFront();
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent2 = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent2); }
});
Hope this helps you.
So far I managed to get the ImageView in my Fragment to update when it's selected in an Activity. But now when I switch from my Fragment PeopleFragment to another fragment like TaskFragment and then back, the ImageView I selected is no longer there, but rather the default ImageView that loads when the Fragment is first initialized.
PeopleFragment.java
public class PeopleFragment extends Fragment {
ImageButton profileImage;
private FirebaseAuth mAuth;
TextView fullName;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_people2, container,
false);
profileImage = (ImageButton) view.findViewById(R.id.imageButton4);
profileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent settingsClick = new Intent(getActivity(),
ProfileSettingsActivity.class);
startActivityForResult(settingsClick,0);
}
});
fullName = (TextView) view.findViewById(R.id.userProfileFullName);
mAuth = FirebaseAuth.getInstance();
return view;
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
/**
* Updates the view according to the authentication status.
* #param user the current FirebaseUser
*/
private void updateUI(FirebaseUser user) {
if (user != null) {
fullName.setText(user.getDisplayName());
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED) return;
//Getting the Avatar Image we show to our users
ImageView avatarImage =
(ImageView)getView().findViewById(R.id.imageButton4);
//Figuring out the correct image
String drawableName = "profile1";
switch (data.getIntExtra("imageID",R.id.teamid00)) {
case R.id.teamid00:
drawableName = "profile1";
break;
case R.id.teamid01:
drawableName = "profile2";
break;
case R.id.teamid02:
drawableName = "profile3";
break;
case R.id.teamid03:
drawableName = "profile4";
break;
case R.id.teamid04:
drawableName = "profile5";
break;
case R.id.teamid05:
drawableName = "profile6";
break;
default:
drawableName = "profile1";
break;
}
int resID = getResources().getIdentifier(drawableName, "drawable",
getActivity().getPackageName());
avatarImage.setImageResource(resID);
}
}
ProfileActivity.java
public class ProfileSettingsActivity extends AppCompatActivity {
//ImageButton targetImage;
Button loadButton;
ImageView profileImage1;
ImageView profileImage2;
ImageView profileImage3;
ImageView profileImage4;
ImageView profileImage5;
ImageView profileImage6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_settings);
profileImage1 = (ImageView) findViewById(R.id.teamid00);
profileImage2 = (ImageView) findViewById(R.id.teamid01);
profileImage3 = (ImageView) findViewById(R.id.teamid02);
profileImage4 = (ImageView) findViewById(R.id.teamid03);
profileImage5 = (ImageView) findViewById(R.id.teamid04);
profileImage6 = (ImageView) findViewById(R.id.teamid05);
// Makes the arrow image act as a back button.
ImageView backButton = (ImageView) findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
profileImage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
profileImage6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setProfilePicture(view);
}
});
}
public void setProfilePicture(View view) {
//Creating a Return intent to pass to the Main Activity
Intent returnIntent = new Intent();
//Figuring out which image was clicked
ImageView selectedImage = (ImageView) view;
//Adding stuff to the return intent
returnIntent.putExtra("imageID", selectedImage.getId());
setResult(RESULT_OK, returnIntent);
//Finishing Activity and return to main screen!
finish();
}
My question is relative to this question Clearing intent
but I'm having problems implementing it.
My first class TodaysExercise.java has a button, when the button is clicked I putExtra intent.putExtra("highlegs", "High Legs"); and startActivity(intent);
TodaysExercise.java
final Intent intent = getIntent();
if (intent != null) {
String clicked = intent.getStringExtra("button");
if (clicked.equals("btn1")) {
intent.setClass(TodaysExercise.this, DoExercise.class);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When btn1 is clicked I putExtra
intent.putExtra("highlegs", "High Legs");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
The next class DoExercise.java then get the string String text = bundle.getString("highlegs"); and set it to the textView textView.setText(text);. I then check if TextView is equal to if (textView.getText().equals("High Legs")) and if it is, I once again putExtra i.putExtra("next", "Leg Curls X 20"); and start next class startActivity(intent);
DoExercise.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_do_the_exercise);
textView = (TextView) findViewById(R.id.replace_this);
descheader = (TextView) findViewById(R.id.desc_header_id);
fab = (FloatingActionButton) findViewById(R.id.fab);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
descheader.setTypeface(custom_font);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("highlegs");
if (text != null)
textView.setText(text);
}
if (textView.getText().equals("High Legs"))
{
ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
imLoading.setBackgroundResource(R.drawable.workout);
AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
.getBackground();
frameAnimation.start();
final Intent i = new Intent();
i.setClass(DoExercise.this, ReadyForNext.class);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
I get String text = bundle.getString("next"); and set tv_nextdesc.setText(text); the string extras to the TextView. I also have a button in this class that should return to DoExercise.java and this is where my question is.
ReadyForNext.java
public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ready_for_next);
btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
tv_nextdesc.setTypeface(custom_font);
tv_nexttxt.setTypeface(custom_font);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("next");
if (text != null)
tv_nextdesc.setText(text);
}
final Intent i = new Intent();
i.setClass(ReadyForNext.this, DoExercise.class);
btn_next_exercise.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//I first check what the title is to determine what to put extra
if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//then here I putExtra
i.putExtra("legcurls", "Leg Curls");
startActivity(i);
}
}
});
}
So as you can see I have 3 classes that basically run in a circle, TodaysExercise.java opens DoExercise.java and it opens ReadyForNext.java from here I want to open DoExercise.java again (Reuse DoExercise.java) but that is when I want to clear the intent to put a new intent extra?
Any help on how to achieve this?
Just finish your DoExercise activity here
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
}
});
and start from ReadyForNext activity like you're doing.
Now you have to do some changes in DoExercise class because your intent putExtra key change with different class otherwise you'll get null pointerException
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}
if (text != null)
textView.setText(text);
}
Hope this will help you.
Thanks for checking out my question!
I have a relativly ismple loop which will add a onClickListener to each ImageView I add to my Layout, however when trying to add a new Intent to it, it gives me one of the following errors:
String[] imageURLs = imageURLsString.split("/");
for (int i = 0; i < imageURLs.length; i++){
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
final String imageURL = ".../images/" + imageURLs[i];
Picasso.with(this).load(imageURL).into(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(this, ImageActivity.class);
intent.putExtra("URL", imageURL);
startActivity(intent);
}
});
linearLayout.addView(image);
}
Will result in the "Cannot resolve Constructor 'Intent ...." error.
So when I was looking for a solution, people suggested to change "this" to "MainActivity.this", but...
String[] imageURLs = imageURLsString.split("/");
for (int i = 0; i < imageURLs.length; i++){
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
final String imageURL = ".../images/" + imageURLs[i];
Picasso.with(this).load(imageURL).into(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(MainActivity.this, ImageActivity.class);
intent.putExtra("URL", imageURL);
startActivity(intent);
}
});
linearLayout.addView(image);
}
resulted into: com.myName.appName.MainActivity is not an enclosing class
Here is the ImageActivity class:
public class ImageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
String URL = getIntent().getExtras().getString("URL");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.getLayoutParams().height = (int) (imageView.getLayoutParams().width * (Float.valueOf(String.valueOf(1.41))));
Picasso.with(this).load(URL).into(imageView);
}
The weird thing is that I have done numerous new Intent's during my play time with creating apps, but I cant seem to solve this one. What am I missing here?
FULL CODE FROM HERE
public class DetailsActivity extends AppCompatActivity {
TextView TVWeChatIdValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
String dataString = getIntent().getExtras().getString("dataString");
String[] itemData = dataString.split(",");
String category = itemData[0];
Log.e("DetailsActivity", category);
String itemID = itemData[1];
Log.e("DetailsActivity", itemID);
String imageURLsString = itemData[2];
Log.e("DetailsActivity", imageURLsString);
String description = itemData[3];
Log.e("DetailsActivity", description);
String price = itemData[4];
Log.e("DetailsActivity", price);
String itemCode = category + itemID;
TextView textView;
textView = (TextView) findViewById(R.id.Title);
textView.setText(getResources().getString(R.string.app_name));
textView = (TextView) findViewById(R.id.ItemReferralValue);
textView.setText(itemCode);
textView = (TextView) findViewById(R.id.ItemPriceValue);
textView.setText(price);
TVWeChatIdValue = (TextView) findViewById(R.id.WeChatIDValue);
new DatabaseTask(this, "details", "GETWECHATID").execute();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ItemDetailsContent);
String[] imageURLs = imageURLsString.split("/");
for (int i = 0; i < imageURLs.length; i++){
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(182),getPx(256)));
image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
final String imageURL = ".../images/" + imageURLs[i];
Picasso.with(this).load(imageURL).into(image);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(this, ImageActivity.class);
intent.putExtra("URL", imageURL);
startActivity(intent);
}
});
linearLayout.addView(image);
}
ImageView imageView = (ImageView) findViewById(R.id.ItemDetailsDescription);
String imageURL = ".../images/" + description;
Picasso.with(this).load(imageURL).into(imageView);
}
public void SetWeChatId(String mValue) {
TVWeChatIdValue.setText(mValue);
}
public int getPx(int dimensionDp) {
float density = getResources().getDisplayMetrics().density;
return (int) (dimensionDp * density + 0.5f);
}
}
WORKING CODE
public class MainActivity extends AppCompatActivity {
TextView TVWeChatIdValue;
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView;
textView = (TextView) findViewById(R.id.Title);
textView.setText(getResources().getString(R.string.app_name));
textView = (TextView) findViewById(R.id.SubTitle);
textView.setText(getResources().getString(R.string.slogan));
textView = (TextView) findViewById(R.id.MoreForWomen);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenCatalog("women");
}
});
textView = (TextView) findViewById(R.id.MoreForMen);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenCatalog("men");
}
});
textView = (TextView) findViewById(R.id.MoreForKids);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenCatalog("kids");
}
});
}
public void GetData() {
TVWeChatIdValue = (TextView) findViewById(R.id.WeChatIDValue);
new DatabaseTask(this, "main", "GETWECHATID").execute();
linearLayout = (LinearLayout) findViewById(R.id.NewItemsForWomenContent);
linearLayout.removeAllViewsInLayout();
new DatabaseTask(this, linearLayout, "WOMEN", "GETNEWESTITEMS").execute();
linearLayout = (LinearLayout) findViewById(R.id.NewItemsForMenContent);
linearLayout.removeAllViewsInLayout();
new DatabaseTask(this, linearLayout, "MEN", "GETNEWESTITEMS").execute();
linearLayout = (LinearLayout) findViewById(R.id.NewItemsForKidsContent);
linearLayout.removeAllViewsInLayout();
new DatabaseTask(this, linearLayout, "KIDS", "GETNEWESTITEMS").execute();
}
public void SetWeChatId(String mValue) {
TVWeChatIdValue.setText(mValue);
}
public void ProcessNewItems(LinearLayout mLinearLayout, final String mCategory, final HashMap<Integer, Item> mItems){
for (int i = 0; i < mItems.size(); i++) {
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(getPx(96),getPx(128)));
image.setPadding(getPx(3),getPx(3),getPx(3),getPx(3));
String imageURL = ".../images/" + mItems.get(i).getImageURLs()[0];
Picasso.with(this).load(imageURL).into(image);
mLinearLayout.addView(image);
final int index = i;
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowItemDetails(mCategory, mItems.get(index));
}
});
}
}
void ShowItemDetails(String mCategory, Item mItem){
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("dataString", mItem.GetDataString());
Log.e("CatalogActivity", mItem.GetDataString());
startActivity(intent);
}
void OpenCatalog(String mCategory){
//show catalog
Intent intent = new Intent(this, CatalogActivity.class);
intent.putExtra("category", mCategory);
startActivity(intent);
}
public int getPx(int dimensionDp) {
float density = getResources().getDisplayMetrics().density;
return (int) (dimensionDp * density + 0.5f);
}
#Override
public void onResume() {
super.onResume();
GetData();
}
}
You need to use the name of the Activity subclass which contains the code that creates the OnClickListener. In many examples, this is MainActivity. However, this doesn't seem to be the case in your code. Since you are in a class named DetailsActivity then use that name:
Intent intent = new Intent(DetailsActivity.this, ImageActivity.class);
From what I can see of your code here, I strongly suggest that you learn about ListView and RecyclerView. They are a bit complex, but once you understand them, they do much of the work for you similar to what you are trying to do here. For one thing, they are more efficient than your own code because they only create as many views as are visible.
Let's look more closely at your other example:
public class MainActivity extends AppCompatActivity {
// ...
void ShowItemDetails(String mCategory, Item mItem){
Intent intent = new Intent(this, DetailsActivity.class);
intent.putExtra("dataString", mItem.GetDataString());
startActivity(intent);
}
}
Notice that the ShowItemDetails() is inside the MainActivity class. So this refers to an instance of that class which can be used wherever a Context is needed. (For more details about this you need to read about inheritance and polymorphism.)
On the other hand, your original code creates an Intent in a method which is inside an anonymous subclass of OnClickListener which cannot be used as a Context. However the anonymous inner class is in a method inside DetailsActivity which inherits from Context. In order to access an instance of this outer class, you have to use DetailsActivity.this.
For more details you should learn about the special this reference and inner classes.
You have this block
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent;
intent = new Intent(MainActivity.this, ImageActivity.class);
intent.putExtra("URL", imageURL);
startActivity(intent);
}
});
Change MainActivity.this to whatever Activity subclass that is setting the OnClickListener. That would be DetailsActivity.this in your case. Basically the enclosing activity just means the name of the class file (which is also the name of the Activity class) in which the Activity is being defined, this Activity encapsulates the call to .setOnClickListener() to which you are passing an anonymous inner class, which is being enclosed inside your DetailsActivity, hence the name enclosing activity. Hope that makes sense
so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have
First activity
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle = getIntent().getExtras();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second activity
Button btn;
RadioGroup rg2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz2);
btn = (Button) findViewById(R.id.nextBtn2);
rg2= (RadioGroup) findViewById(R.id.rg2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz3.class);
Bundle bundle = getIntent().getExtras();
int id = rg2.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg2", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
This continues for about 7 activities
Final activity (where the result and the character are shown)
public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}
Now, in the final activity, I want to have a code where if for example:
if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),
Quiz2: options 2 or 4
Quiz3: options 1 or 4
Quiz2: options 2 or 3
and so on...
then change a textview to something specific like "your character is x"
I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.
Any help would be appreciated, thank you <3
EDIT:
TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
&& bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
|| bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
&& bundle.getString("rg6").equals("Yes"))) {
textviewResult.setText("x");
}else{
textviewResult.setText("not x");
}
The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)
You can use the String equals() method inside your if statement. It returns true if both Strings are equal.
E.g.:
if(bundle.getString("rg").equals("YourRadioButtonText")){
...
}
Your code could look something like this:
`public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
// NEW
TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
textviewResult.setText("It is not A");
}
else {
textviewResult.setText("Mhhh");
}
// END
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}`