Pass brush size and brush shape between MainActivity and ChangeBrush.
Main:
static final int ACTIVITY_BRUSH_SIZE_REQUEST_CODE = 2;
static final int ACTIVITY_BRUSH_SHAPE_REQUEST_CODE = 3;
public void onClickBrush(View view) {
Intent intent = new Intent(MainActivity.this, ChangeBrush.class);
startActivityForResult(intent, ACTIVITY_BRUSH_SIZE_REQUEST_CODE);
startActivityForResult(intent, ACTIVITY_BRUSH_SHAPE_REQUEST_CODE);
}
ChangeBrush:
public void onClickChangeBrushSize(View view) {
String size = view.getTag().toString().;
// return the brush size to main activity
Bundle bundle = new Bundle();
bundle.putString("size", size);
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
public void onClickChangeBrushShape(View view) {
String shape = view.getTag().toString();
// return the brush shape to main activity
Bundle bundle = new Bundle();
bundle.putString("shape", shape);
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
SelectBrush activity layout:
When user click the size button and shape button in ChangeBrush activity, they could pass size and shape to MainActivity. In MainActivity, I use onActivityResult to set the brush size and shape.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String color = bundle.getString("color");
String size = bundle.getString("size");
String shape = bundle.getString("shape");
switch(requestCode) {
case ACTIVITY_COLOR_REQUEST_CODE:
fingerPainterView.setColour(Color.parseColor(color)); // set new color value
case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush size to " + size);
case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush shape to " + shape);
}
} else if(resultCode == RESULT_CANCELED) {
Log.d("FingerPainter", "MainActivity canceled");
}
}
This will log the return value:
D/FingerPainter: Main activity change brush shape to null
D/FingerPainter: Main activity change brush size to null
D/FingerPainter: Main activity change brush shape to SQUARE
It seems the return value could correspond to the right intent. I don't know why it log the shape information twice. Is there anything wrong with finish()? I want the user select both size and shape, and then return to the main activity.
If the method is wrong, what should I do to pass these two values?
Two problems I see:
1: You are calling startActivityForResult twice. You can pass extras using intent.putExtra(...).
2: You don't have a break in your switch statement:
switch(requestCode) {
case ACTIVITY_COLOR_REQUEST_CODE:
fingerPainterView.setColour(Color.parseColor(color)); // set new color value
break;
case ACTIVITY_BRUSH_SIZE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush size to " + size);
break;
case ACTIVITY_BRUSH_SHAPE_REQUEST_CODE:
Log.d("FingerPainter", "Main activity change brush shape to " + shape);
break;
}
If you are trying to update multiple things from a single Intent, I think you need to save off the current data that already exists in the Activity.
public void onClickBrush(View view) {
Intent intent = new Intent(MainActivity.this, ChangeBrush.class);
// intent.putExtra("shape", currentShape); // for example
// then, only start one Activity, with a generic request code, not one Activity twice with two codes
In other words, you start one Activity capable of changing all the data (even though you only update a single value at a time). So, save the current values when you start the next Activity, pass those back as unchanged while you update the new values. Then you shouldn't get null values.
For example, for the shape button.
public void onClickChangeBrushShape(View view) {
String shape = view.getTag().toString();
// return the brush shape to main activity
Bundle bundle = new Bundle();
bundle.putString("shape", shape); // Set the new shape
bundle.putString("color", color); // I assume you've saved this
bundle.putString("size", size); // and this...
// You could put string extras into the intent directly, but whatever
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
}
Another option is using SharedPreferences to store all the values and forget about Intents and Bundles.
Related
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;
}
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");
}
I made this application go from one activity to the next. and then come back, but after it comes back to my main activity the button to go to the next view again does not do anything? I thought it was from startActivityForResult but I did it a different way and its still not working...
Here is some code: if button is pushed
if (search.isPressed() && searchPressed == false) {
// show search list
switch1 = new Intent(MainActivity.this, SearchActivity.class);
// startActivityForResult(switch1, 0);
startActivity(switch1);
}
in next activity:
private OnItemClickListener listListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String text = (String) ((TextView) arg1).getText();
String[] selected = text.split(" - ");
selected[0] = selected[0].replace(' ', '_');
Log.w("COMPANY", selected[0]);
Log.w("PART", selected[1]);
// Intent data = new Intent(SearchActivity.this,
// MainActivity.class);
// data.putExtra("key", selected);
// setResult(RESULT_OK, data);
MainActivity.searchData = selected;
finish();
// startActivity(switch2);
}
};
////\ when item is pushed it goes back to main screen
My guess from what you've posted so far is that you are actually having trouble because of the if statement, not the startActivity().
Try putting a log output inside this if statement:
if (search.isPressed() && searchPressed == false) {
Log.d(TAG, "Search has been pressed");
// show search list
switch1 = new Intent(MainActivity.this, SearchActivity.class);
// startActivityForResult(switch1, 0);
startActivity(switch1);
}
If you don't see your out put in the log cat then the problem is with the if statement. If you post some more of the code from around this if I can try to help figure it out for you. But it seems like your condition is contradicting. To me it looks like you are checking to see if search is both pressed and not pressed.
Post a bit more of the MainActivity code, especially where the searchPressed boolean gets set.
One of the two conditions in your first part of the code will fail after the first time.
So either condition
search.isPressed()
or condition
searchPressed == false
is not true
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());
HI all,
this is my class A, where on button click , i m sending a int variable to class B
Intent bgIntent = new Intent(Background.this, MainScreen.class);
bgIntent.putExtra("background", bgColor);
startActivity(bgIntent);
and on class B
Intent bgIntent = getIntent();
bgGlobal = bgIntent.getIntExtra("background",-1 );
if(bgGlobal == 0)
{
DetailsTextView.setBackgroundResource(R.color.a0);
}
else
if(bgGlobal == 1)
{
DetailsTextView.setBackgroundResource(R.color.a1);
}
But the problem is i am getting a blank view.My view is not coming up with textview.
is this proper to set background
"DetailsTextView.setBackgroundResource"???
If you want to change the color of a View use http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)
for example:
DetailsTextView.setBackgroundColor(getResources().getColor(R.color.txt_green));
Anyway, it's not clear if you want to change the screen's background or the textview's background.
Also
if(bgGlobal == 0){...} else ...
is wrong. You should do something of the like
if(bgGlobal != -1)
{
[Use intent to read color]
}else{
[set default color]
}
If you see a blank view it's possibly due to a wrong XML layout.
Edit: To retrieve the extra
getIntent().getExtras().getInt("background",-1);