Android Studio onClick button to display ImageView in another activity - android

I have setup 2 activities - one and two .
For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.
What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.
If user key in 1, it will display 1.png in activity 2
If user key in 2, it will display 2.png in activity 2
If user key in 3, it will display 3.png in activity 2
etc...
the image will get from drawable folder.
Please refer to this screenshot
[![enter image description here][1]][1]
I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.
get_image.setBackgroundResource(R.drawable.1); // here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it.
Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...
thanks in advance!
please check screenshot -> http://i.stack.imgur.com/53vjy.jpg

You said that you can pass integer value from activity 1 to 2 so just use that to find your image.
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
I may missing somethings because i can't understand when you said that "but i cant do it for imageview".
EDIT: after your addtional code.
So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id).
I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2
and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).

Try below code for your solution,
For Activity One write below code to redirect on activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 write below code in onCreate method
int selectedNumber = 1;
if(getIntent().getExtras() != null)
{
selectedNumber = getIntent().getExtras().getInt("SelectedNumber");
}
switch(selectedNumber)
{
case 1: // set your 01.png Image
break;
case 2: // set your 02.png Image
break;
// And so
}

Try this way might helps you.
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
then your Activity2 use,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
Finally,
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String mDrawableName = editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Intent ii=new Intent(Activity.this, Activity1.class);
ii.putExtra("resId", resID);
startActivity(ii);
}
});
Activity2
public class Activity2 extends Activity
{
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
img = (ImageView)findViewById(R.id.img);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
int drawableId =(int) b.get("redId");
img.setImageResource(drawableId);
}
}
}

you can pass resource value in extras. try this way.
EditText edit = (EditText) findViewById(R.id.yourEdittext);
Button btn = (Button) findViewById(R.id.yourButton);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
if(edit.getText().toString().trim().equals("1")){
i.putExtra("image", R.drawable.01);
startActivity(i);
}else if(edit.getText().toString().trim().equals("2")){
i.putExtra("image", R.drawable.02);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Please Enter Valid Value.",
Toast.LENGTH_SHORT).show();
}
}
});
and in your ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
Happy Coding.

In activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
then use switch case for values from intent then
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}

Related

Changing the display image of an imageview based on the data in a string (Such as word)

Going to briefly explain what's going on. I want to update a single imageView to change it to differently named drawable images. There is only four choices here, this is because i'm bringing data from a spinner, which has a choice of 4 countries, selected from another activity. This then changes the imageView to the custom drawable image of said country.
So far i'm unsure how i'm going to do this, tried a few very basic if else statements but they are very incorrect.
I'll post some of what I have below:
public class WorldImage extends AppCompatActivity {
TextView textCountry;
private String stringCountry;
ImageView imageCountry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_worldimage);
Intent aIntent = getIntent();
stringCountry = aIntent.getStringExtra("movetoImageActivity");
textCountry= (TextView) findViewById(R.id.movetoviewCountry);
imageCountry = (ImageView) findViewById(R.id.imageCountry);
It's around here I get lost, some really basic if & else if statements, such as:
if (textCountry.getText().toString().equals("Europian Union"))
{
imageCountry.setImageResource(R.drawable.eu_flag);
}
What would be reccomended here?
How about printing log?
Intent aIntent = getIntent();
stringCountry = aIntent.getStringExtra("movetoImageActivity");
textCountry= (TextView) findViewById(R.id.movetoviewCountry);
imageCountry = (ImageView) findViewById(R.id.imageCountry);
Log.e("TEST", "stringCountry : " + stringCountry );
Log.e("TEST", "textCountry: " + textCountry.getText().toString());
if (stringCountry.equals("Europian Union")){
Log.e("TEST", "Europian Union");
imageCountry.setImageResource(R.drawable.eu_flag);
}else if (stringCountry.equals("Something else")){
Log.e("TEST", "Something else");
imageCountry.setImageResource(R.drawable.something);
}
You can use a switch statement like this:
switch(stringCountry){
case "Europian Union":
imageCountry.setImageResource(R.drawable.eu_flag);
break;
case "United States":
imageCountry.setImageResource(R.drawable.us_flag);
break;
default:
// optional. Use if all comparisons above fail and you want to handle it
break;
}

Android application stops on button click

I am making this application and it just stops when i press some buttons
public class Menu3 extends TheMenu{
Button Buttons[] = new Button[21];
Intent open = new Intent("com.frosti.lidraedi.OPEN");
int clicked;
boolean found;
int PO1;
int GoOn;
int s,e;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Bundle b = getIntent().getExtras();
PO1 = b.getInt("PO", 0);
switch (PO1) {
case 21:
//when i press these buttons everything works well
setContentView(R.layout.menu21);
s=0;
e=11;
break;
case 22:
//the layout loads without error
//but when i press the buttons int the layout it just stops
setContentView(R.layout.menu22);
s=12;
e=15;
break;
case 23:
setContentView(R.layout.menu23);
s=16;
e=20;
break;
case 24:
setContentView(R.layout.menu24);
break;
case 25:
setContentView(R.layout.menu25);
break;
default:
break;
}
init();
}
private void init() {
for(int c=s; c <= e; c++){
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/SourceSansPro-Semibold.otf");
int d = c+1;
int viewId = getResources().getIdentifier("b"+Integer.toString(d), "id", getPackageName());
if(viewId!=0){
Buttons[c] =((Button)findViewById(viewId));
Buttons[c].setTypeface(tf);
Log.d("Buttons", Integer.toString(c)+" : "+Integer.toString(d));
}else{
Log.d("Buttons", "0");
}
}
}
public void ButtonOnClick(View v){
Log.d("ButtonOnClick", "I was clicked");
found=false;
int c= 0;
for(Button b:Buttons){
Log.d("for loop", "I got run");
if(b.getId() == v.getId()){
Log.d("if block", "I got found");
GoOn=c;
Log.d("M3:true:C", Integer.toString(c)+ " : "+Integer.toString(v.getId()));
found=true;
break;
}
Log.d("C var", "I will get incremented");
c++; //hehehe
Log.d("C var", "I got incremented");
}
Log.i("ButtonOnClick",Integer.toString(c));
if(found) {
Log.i("M3:clicked:GoOn",Integer.toString(GoOn));
Bundle b1 = new Bundle();
b1.putInt("GoOn",GoOn);
open.putExtras(b1);
Log.d("Activity", "I will get opened");
startActivity(open);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}else if(clicked != v.getId()){
clicked = v.getId();
Toast.makeText(getApplicationContext(),"Skjárinn fannst því miður ekki",Toast.LENGTH_SHORT).show();
}
}
ok so i open the application press menu 22(that executes case 22 in the switch block) press some button and it stops
These are the errors i get:
If you compare the image with the code you see it executes "Log.d("for loop", "I got run");" but then freaks out, so error should be in this line " if(b.getId() == v.getId()){" but i have no idea why this happens?
This is the xml layout code just in case if it helps.
I would really appreciate if you could help me with this
Try running the following loop right after you initialize Buttons, just to be sure of what you have:
for(Button b : Buttons){
Log.d("Have button with ID " + b.getId());
Then run the same loop at the start of your onClick(). That will at least tell you if your Buttons structure is sound. When in doubt of which item is causing a NPE, it helps to log them separately. Often times, it's a non-obvious lifetime error, especially with fragments.
you create one array of Button with:
Button Buttons[] = new Button[21];
but you have custom item in that between s to e (you get value from switch statement ).
as you don't have button in all index of your array you get NPE on b.getId(), because you don't have any button on some index.
so you can solve your problem with two way.
1- as you don't know size of your array you can use ArrayList
2- you need initialize your array in init with
Buttons[] = new Button[e - s]; // or e-s+1 test both
I suggest use ArrayList, so your code must be like:
ArrayList<Button> list = new ArrayList<Button>();
Then you can get all id in your code with:
for ( int i = 0 ; i < list.size() ; i++)
list.get(i).getId();

How will I maximize the imageView once the image is clicked on the ListView?

I have a listview, If the image is clicked on the listview I want it to be seen in a fullscreen
here is my code: that is when the listview is being clicked
ImageList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String Userid = ((TextView) view.findViewById(R.id.user_id)).getText().toString();
String ScrapBookId = ((TextView) view.findViewById(R.id.photo_id)).getText().toString();
// ---Starting new intent
Intent in = new Intent(getApplicationContext(),FullImageActivity.class);
in.putExtra("userid", Userid);
in.putExtra("IMAGE",ScrapBookId);
// ---starting new activity and expecting some response back
startActivityForResult(in, 100);
//Toast.makeText(getApplicationContext(), eventId, Toast.LENGTH_SHORT).show();
}
});
then it will turn into the FullImageActivity here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView txt = (TextView)findViewById(R.id.photo_id);
img.setImageResource(imgid);
txt.setText(imgid);
}
}
but I think there is no image passed :( how will I do it? please help me :( Thank you
You have added a String extra with the key "IMAGE" in the first activity. In the second activity you are trying to obtain it with getIntExtra("IMAGE", 0). There is no int extra with this key, hence this call will return zero.
I'm not sure exactly what you are trying to do, but in any case the type of extra you are trying to obtain should match the type that you added to the intent.
There is no image passed to the new Activity. Your image identifier is a String ("ScrapBookId") but you are treating it as a resource Identifier (Integer) in the OnCreate.
Assuming your drawables are resources (which they appear to be), record the resourceId you set for each imageView as follows:
iv.setTag(R.drawable.ponies);
Then in the onItemClick, lookup the resourceId:
ImageView iv = ((ImageView) view.findViewById(R.id.imageview1));
int resId = (Integer) iv.getTag();
in = new Intent(...)
in.putExtra("IMAGE", resId);
Then in the onCreate:
resId = getIntent().getIntExtra("IMAGE", 0);
img.setImageResource(resId);

How can I calculate the values entered in edittext and transfer from one activity to another?

I am devoloping a roof calculater. i have three edittext and calculate button in the first activity. In the second activity i have a textview that shows the result.
EditText editText1Değer1Kod;
EditText editText2Değer2Kod;
EditText editText3Değer3Kod;
Button hesaplaKod;
I want to take these three value then calculate with formula then show the result in second activity as a textview.
How can i calculate (sum or add this three value) then show in the second activity?
Use in first Activity
hesaplaKod.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int value1= Integer.parseInt(EditText01.getText().toString());
int value2= Integer.parseInt(EditText01.getText().toString());
int value3= Integer.parseInt(EditText01.getText().toString());
int add = value1+value2+value3;
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("AddValue", add);
startActivity(intent);
}
});
And in onCreate of Second Activity get the value from this
int addvalue = getIntent().getExtras().getInt("AddValue");
enter code herejust pass calculate the number and pass the using intent to other activity
in First Activity :
Intent intent=new Intent(IntentsDemo2.this,Activity2.class);
intent.putExtra("result", result);
startActivity(intent);
in Second Activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Intent sender=getIntent();
int result =sender.getExtras().getInt("result");
}

Android programming help

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?
you mean like this ??
in the method onCreate() :
btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);
and then , ovverride the onClick method like this :
#Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}
textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());
First of all, you will have to pass the edittext value to the first activity through intent.
Eg:
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);
Then inside your first activity, you will have to fetch this data as:
String value;
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
value = extras.getString("edittext_value");
textview.setText(value);
}
Hope this may help you.
From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :
Change the way you open Activity2 to
Intent EditIntent = new Intent(this, Activity2.class);
//Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);
Add override to you Activity1
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
if (data.hasExtra("myText")) {
//get your data with data.getExtras().getString("myText")
}
}
}
Change what button on your Activity2 does
{
Intent returnData= new Intent();
returnData.putExtra("myText", /*Text from your EditText*/);
if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
setResult(Activity.RESULT_OK, returnData);
} else {
getParent().setResult(Activity.RESULT_OK, returnData);
}
finish();
}
This should return text from EditText from Activity2 to Activity1. Code was not tested
create a variable for the textview to access it like
Textview txt = (Textview) finviewByid........;
implement the following code on button click listener
txt.setText(edittext.getText().toString());

Categories

Resources