as in image I've a listView of flags (for example). When I click one of this in 2nd Activity I get the text name and medium image of this flag. all work fine and as I wanted. But now in 2nd Activity when I click on the image I would like to see the respective Big Image on the 3th Activity but I can not do it. How I can do?
activitys screenshots
public class MainActivity extends AppCompatActivity
{
ListView mListView;
String [] Names = {"France", "Germany", "Italy", "USA"};
int [] Small = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};
int [] Medium = {R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4};
int [] Big = {R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listview);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, Names, Small);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent mIntent = new Intent(MainActivity.this, SecondActivity.class);
mIntent.putExtra("flagName", Names[i]);
mIntent.putExtra("flagSmall", Small[i]);
mIntent.putExtra("flagMedium", Medium[i]);
mIntent.putExtra("flagBig", Big[i]);
startActivity(mIntent);
}
});
}
this is how call the images on 2nd Activity:
public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;
TextView mTextView;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mImageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);
Bundle mBundle = getIntent().getExtras();
if (mBundle != null)
{
mImageView.setImageResource(mBundle.getInt("flagMedium"));
mTextView.setText(mBundle.getString("flagName"));
}
my simple solution with mIndex:
public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;
TextView mTextView;
ImageView mImageView;
int mIndex;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mImageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);
Bundle mBundle = getIntent().getExtras();
if (mBundle != null)
{
mImageView.setImageResource(mBundle.getInt("flagMedium"));
mTextView.setText(mBundle.getString("flagName"));
mIndex = mBundle.getInt("flagBig");
}
and I've send the value with intent to other activity.
Intent mIntent = new Intent(SecondActivity.this, ThirdActivity.class);
mIntent.putExtra("fullImg", mIndex);
startActivity(mIntent);
Related
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I'm using viewpager to display the slideshow of the images but when I'm clicked on any one of the image it need to opened in new activity. Can anyone help me?
I'm displaying toast message when clicked on image but my actually requirement is to open the image in new activity.
The code is below:
class MyCustomPagerAdapter extends PagerAdapter{
private Context context;
private int images[];
private LayoutInflater layoutInflater;
public int i;
MyCustomPagerAdapter(Context context, int images[]) {
this.context = context;
this.images = images;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
View itemView = layoutInflater.inflate(R.layout.full_profile, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.expandedImage);
imageView.setImageResource(images[position]);
container.addView(itemView);
//listening to image click
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, "you clicked image " + (position + 1), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, ImageOpeningActivity.class);
intent.putExtra("MY_IMAGE", images[position]);
context.startActivity(intent);
}
});
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
ImageOpeningActivity.class
class ImageOpeningActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageopening);
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
if(getIntent()!=null){
int image = getIntent().getExtras().getInt("MY_IMAGE");
imageView.setImageResource(image);
}
}
}
although i changed my code i getting an error, the error is below.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.vmax.kalyanam/com.vmax.kalyanam.ImageOpeningActivity}: java.lang.IllegalAccessException: java.lang.Class<com.vmax.kalyanam.ImageOpeningActivity> is not accessible from java.lang.Class<android.app.Instrumentation>
public class Full_profile extends Activity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayout;
//public String title = "KANDIBANDA ROJA";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_profile);
ViewPager viewPager;
int images[] = {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.one
};
MyCustomPagerAdapter myCustomPagerAdapter;
viewPager = (ViewPager)findViewById(R.id.viewPager);
myCustomPagerAdapter = new MyCustomPagerAdapter(Full_profile.this, images);
viewPager.setAdapter(myCustomPagerAdapter);
put intent here in "listening to image click"
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,PhotoViewerActivity.class);
intent.putExtra("image", imageurl);
startActivity(intent);
}
});
public class PhotoViewerActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra("image");
// you can show image from url using library like glide or picasso
}
#Override
protected int getLayoutResourceId() {
return R.layout.activity_photo_viewer;
}
#Override
public void onBackPressed() {
PhotoViewerActivity.this.finish();
}
}
First of all do this in your code instead of Toast. Comment out Toast and paste these lines.
Intent intent = new Intent(this, ImageOpeningActivity.class);
intent.putExtra("MY_IMAGE", images[position]);
context.startActivity(intent);
step2: Create activity with the name of ImageOpeningActivity and in its xml take a Imageview , let suppose its Id is myImageView then do following in your on create
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
if(getIntent()!=null){
int image = getIntent().getExtras().getInt("MY_IMAGE");
imageView.setImageResource(image);
}
I am supposing that your images[position] is integer array.
Like #Pratap said you can send image url from one activity to another. But if you wanted to pass image itself as bitmap follow this method.
Bitmap implements Parcelable, so you can pass it as intent extra.
Intent intent = new Intent(this, ImageDetailActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it in ImageDetailActivity by calling,
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Warning: Do not pass data more than 500Kb through Intent Refer this site
create new activity with imageview in xml layout and call this from your viewpager image onclick
Intent intent = new Intent(this, ImageDetailActivity.class);
intent.putExtra("image",images[i]);
startActivity(intent);
ImageDetailActivity.java
public class ImageDetailActivity extends AppCompatActivity {
Drawable imageDrawable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_detail);
Intent intent = getIntent();
imageDrawable= getResources().getDrawable(intent.getIntExtra("image",-1));
imageView.setImageDrawable(imageDrawable);
}
}
Make sure you add this ImageDetailActivity to your android manifest file
I'm trying to send a variable from one activity to another. When I try to test the app on my phone, it crashes and says the app has stopped working.
Code from sending side
if (id==R.id.action_cart){
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
}
and code on receiving side
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
If you know what the issue is please let me know. I've spent hours looking for a solution. Thank you
you need to move this TextView btn = (TextView) findViewById(R.id.text); inside oncreate because before oncreate there is no layout attached to Scroll activity and finding textview by using this findViewById(R.id.text) will result in a failure hence crash
public class Scroll extends AppCompatActivity {
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text)
//^^^^
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(""+intValue);
}
}
Note : Use appropriate naming convention for clarity , like textViewVar for TextView instead of btn and convert your int value to text using ""+value because TextView allow only String to use
I think the problem is in this call: btn.setText(intValue);: string resource ID with value 5 does not exist. Try replacing it with btn.setText(Integer.toString(intValue));
Try this
if (id==R.id.action_cart){
//hide views before moving
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
}
and in Recieving activity
put this in oncreate
btn = (TextView) findViewById(R.id.text);
Like this
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
change a line to: btn.setText(String.valueOf(intValue));
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
In your activity
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text); // initialize your button here
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
Replace your code by this
public class Scroll extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
TextView btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}
}
I have a problem with sending data from fragment to fragment. I have DialogFragment named fmonday, it is Viewpager's fragment.
I call other DialogFragment, named AlertDFragment to add some data to my fragment. I can read the data from spinner, it's working good.
Now I need to send one variable, type string, from AlertDFragment to fmonday.
Here is the code of fmonday:
public class fmonday extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor> {
DB db;
Button button12;
DialogFragment dlg1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dlg1 = new AlertDFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fmonday, container, false);
button12 = (Button) rootView.findViewById(R.id.button12);
button12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dlg1.show(getFragmentManager(), "dlg1");
}
});
return rootView;
}
And code of AlertDFragment:
public class AlertDFragment extends DialogFragment {
Spinner spin;
DB db;
String string1;
Button button13;
private String namestr;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View fdfS = li.inflate(R.layout.fdf, null);
adb.setView(fdfS);
spin=(Spinner)fdfS.findViewById(R.id.spinner);
db = new DB(getActivity());
db.open();
spin.setOnItemSelectedListener(new OnSpinnerItemClicked());
loadSpinnerData();
button13 = (Button) fdfS.findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity().getApplicationContext(), "Clicked : " +
string1, Toast.LENGTH_LONG).show();
getDialog().dismiss();
}
});
return adb.create();
}
I can't find the way to send this string1 variable to my fmonday DialogFragment, because simple intent doesn't work with non-activity things. Also read some advices about bundle, but couldn't find out how to work with it.
Thanks
UPDATE
fmonday:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dlg1 = new AlertDFragment();
string1 = getArguments().getString("latitude");
}
AlertDFragment:
public class AlertDFragment extends DialogFragment {
Spinner spin;
DB db;
String string1;
Button button13;
private String namestr;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View fdfS = li.inflate(R.layout.fdf, null);
adb.setView(fdfS);
spin=(Spinner)fdfS.findViewById(R.id.spinner);
db = new DB(getActivity());
db.open();
spin.setOnItemSelectedListener(new OnSpinnerItemClicked());
loadSpinnerData();
button13 = (Button) fdfS.findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getActivity().getApplicationContext(), "Clicked : " +
string1, Toast.LENGTH_LONG).show();
Bundle bundle = new Bundle();
bundle.putString("latitude", string1);
fmonday alertdfragment= new fmonday();
alertdfragment.setArguments(bundle);
getDialog().dismiss();
}
});
return adb.create();
}
To Set Data:
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
MapFragment mapFragment = new MapFragment();
mapFragment.setArguments(bundle);
To Get Data
String latitude = getArguments().getString("latitude")
You can crate a setter to your second dialogFragment ex:
public void setSmth(String value)
{
this.myData = value;
}
, than on your first dialog
DialogFragment dlg1 = new AlertDFragment();
dlg1.setSmth(your data here);
before showing the second one;
I am working on an app and I need to pass the contents of some textviews to a new activity, but I want to save the content I pass while the app is open so that the user can select more items from other activities and send them to the final checkout activity.
right now I have spinners which save the selection to a Textview
public class Americano extends AppCompatActivity {
// MyDBHandler dbHandler;
String result;
TextView tvSize;
Spinner spinner;
int mPos;
String mSelection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_americano);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner spinnerSize = (Spinner) findViewById(R.id.spinner_size);
AdapterView.OnItemSelectedListener listener = new myOnItemSelectedListener();
spinnerSize.setOnItemSelectedListener(listener);
Spinner spinnerSyrups = (Spinner) findViewById(R.id.spinner_syrups);
AdapterView.OnItemSelectedListener listenerSyrups = new myOnItemSelectedListener2();
spinnerSyrups.setOnItemSelectedListener(listenerSyrups);
Spinner spinnerTopping = (Spinner) findViewById(R.id.spinner_toppings);
AdapterView.OnItemSelectedListener listenerTopping = new myOnItemSelectedListener3();
spinnerTopping.setOnItemSelectedListener(listenerTopping);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DBAdapter dbAdapter = new DBAdapter(view.getContext());
}
});
}
public class myOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvSize);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
public class myOnItemSelectedListener2 implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvSyrup);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
public class myOnItemSelectedListener3 implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvTopping);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I have more menu item classes exactly like this one
I want to pass the textview data to the checkout page. But save that data in that page until the user closes the app.
thanks
You can do it sending the information through an Intent.
In your listener you would go to another actvity like this:
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("RESULT_TEXT", resultText);
startActivity(intent);
And on the NewActivity you can get the information like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getExtras();
String resultText = bundle.getString("RESULT_TEXT", "");
}
Hope it helps.