I’m trying to get a view to change on an activity based on which view the user drags in the previous activity, but something is ill in my code and it refuses to work.
The integer is stored as a public static int _id, and the integer is set for each view like so:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 250;
layoutParams.topMargin = 250;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
_view.setId(1);
The integer is then sent to the next activity through this snippet of code.
case MotionEvent.ACTION_UP:
_id = view.getId();
Intent intent = new Intent(MainActivity.this, Exit_Activity.class);
intent.putExtra("smiley", _id);
startActivity(intent);
And then it should theoretically be recieved in the next activity and define what view is visible at the top of the screen.
public class Exit_Activity extends Activity {
ImageView _view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exit_);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent Intent = getIntent();
int intValue = Intent.getIntExtra("smiley", 0);
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.smile);
if (intValue == 1) {img.setImageResource(R.drawable.smile);}
else if (intValue == 2) {img.setImageResource(R.drawable.frown);}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_exit_, menu);
return true;
}
Right now it doesnt seem to do anything regardless of what view is dragged. Any input would be appreciated.
EDIT:
for bonus brownie points, I’m trying to do the same for text, it looks a little like this:
TextView textView = (TextView) findViewById(R.id.DynRes);
setContentView(R.id.DynRes);
if (intValue == 1) {DynRes.setText("Good to hear! have a nice day!");}
else
if (intValue == 2) {DynRes.appendText("We're very sorry, we'll try harder next time!");}
}
I’m getting an error in eclipse that says “DynRes cannot be resolved” and it isn’t entirely clear to me why. Thanks again.
The reason it does nothing is because you aren’t grabbing an existing ImageView and manipulating it (e.g. by findViewById()). Instead you’re just creating a new ImageView. You need to either grab an existing imageview or add that newly created one to to your contentview’s viewgroup.
EDIT:
You have a basic misunderstanding about how to set the content view of your Activity and what finding a view by id means.
public static final class layoutandpublic static final class id. The The Android Asset Packaging Tool (aapt) takes your application resource files (things defined in XML) and compiles them so you can reference them in code. So, there is no type or variable, etc with the name Dynres, for example. All there is, is a public int that you can references as R.id.Dynres. BUT, again, you can think of that int as a compiled representation of your xml resource. Which leads me to my next point.TextView tv = (TextView)findViewById(R.id.my_textview)simply uses the id to find that view then you cast it to an object of type TextView assigned to the variable “tv” so now tv is what you need to use to call methods on, etc. So, your code needs tobeDynRes.setText("Good to hear! have a nice day!")