I write an travel APP.There are 20 QR Code in every spot.
When tourist use QR Code scanner scan the QR Code,the ImageButton's image have to change into another image.
The problem on this line : spot1.setImageResource(R.drawable.hotspot1);
If I delete this line, there is no problem.
I don't know how to fix it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (0 == requestCode && null != data && data.getExtras() != null) {
String result = data.getExtras().getString("la.droid.qr.result");
int spotnum=Integer.valueOf(result);
switch(spotnum){
case 1:
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
setContentView(R.layout.hotspot1);
break;
case 2:
setContentView(R.layout.hotspot2);
break;
}
}
}
Here is my Logcat:http://i.stack.imgur.com/6y2UQ.png
You can not initialize any Views from xml before calling setContentView :
setContentView(R.layout.hotspot1);
ImageButton spot1=(ImageButton)findViewById(R.id.imageButton1);
spot1.setImageResource(R.drawable.hotspot1);
You are not suppose to call setContentView after setting view resource. call set contentView once in onCreate without setting any view from xml.
You can then change the layout contents, but do not call setContentView again.
You are using a image view, but not calling setContentView before it. This makes the ImageView as null, hence the error.
Try following above suggestions,and it will go. Happy Coding.
Related
I have a textview: Click to add an image behind an imageview.
when user click, it will open the gallery. If user select an image it will be displayed in the imageview.
I want to set the textview text to blank if an image has been selected.
I tried inside imageview on click:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
ImageViewText.setText("");
the problem here is the textview will be blank in the moment user click on the imageview. What I want is to reset it if user really selected an image.
Any ideas?
As you are using startActivityForResult(), you will get an image path imageUri from Intent in onActivityResult() method if an image selected successfully.
Add condition to Check RESULT_OK and REQUEST_CODE. If both true then set image to imageView and set textView value blank("") or change visibility to GONE or INVISIBLE.
Try this:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
// Set image
imageView.setImageBitmap(selectedImage);
// Set text
textView.setText("");
// Alternative
// textView.setVisibility(View.GONE); or
// textView.setVisibility(View.INVISIBLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Hope this will help~
Since you're using startActivityForResult, you should have an onActivityResult method overridden. You can check there if the user has actually selected something which is where you can call setText("") or preferably setVisibility(View.GONE)
help me to solve this question in android :
android : How to select multiple Images from gallery and then add into horizontal scroll able Multiple imageView ...!
You can use this library for selecting multiple images
Just call the intent using
private void pickImages(){
final Intent pickIntent = new Intent(this, PickerActivity.class);
pickIntent.putExtra(PickerActivity.LIMIT_KEY, 6); // Set a limit , you can skip that if you want no limit
startActivityForResult(pickIntent, PickerActivity.PICK_REQUEST); //Open gallery
}
And in your onActivityResult,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, requestCode, data);
if (requestCode == PickerActivity.PICK_REQUEST && resultCode == RESULT_OK) {
//No problemo
final String[] paths = data.getStringArrayExtra(PickerActivity.PICKED_IMAGES_KEY);//Paths for chosen images (Organized)
//Do what you want with paths
For displaying them horizontally u can use this link that shows implementation of horizontal listview using a custom library
in your xml use
<com.devsmart.android.ui.HorizontialListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"
/>
and set adapter using
HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);
listview.setAdapter(myAdapter);
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());
I have one activity with an EditText and a button. When the button is pressed, I call
myEditText.setClickable(false);
myEditText.setFocusable(false);
I have another button, which when pressed, changes the activity.
Intent myIntent = new Intent(view.getContext(), DestinationScreen.class);
startActivityForResult(myIntent, 0);
When I return from activity2 to my main activity which has the EditText, I want it to regain the focus. That is, I want to be able to type in some new values in it. Any idea how that is possible?
I tried to do this in my main Activity
startActivityForResult(myIntent, 0);
myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();
It doesn't seem to work.
As you said, you'd like the EditText to regain focus when you return from the second activity.
Then probably that's what you should try: since you are already invoking the activity2 with the startActivityForResult method (requestCode: 0), you could take advantage of it:
You should override the
onActivityResult(int requestCode, int resultCode, Intent data)
method inside your main activity, check whether the requestCode == 0, and if so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 0:
EditText myEditText = (EditText) findViewById(R.id.textBox);
myEditText.setClickable(true);
myEditText.setFocusable(true);
myEditText.requestFocus();
default:
break;
}
}
I haven't stepped through the Android source to fact check this, but the symptoms imply to me that:
#onActivityResult() is called sometime before #onResume(), and
requesting focus probably requires the view to be shown, which isn't true yet (because the activity's view hierarchy isn't attached to its window, see View#isShown()).
As such, you can fix it by requesting focus from a runnable on the main thread, which will run immediately after the activity is resumed. In your onActivityResult() definition:
final EditText myEditText = (EditText) findViewById(R.id.textBox);
runOnUiThread(new Runnable() {
#Override
public void run() {
myEditText.requestFocus();
// Also move the cursor to the end
myEditText.setSelection(myEditText.length());
}
});
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);