I want to display links which have format such as/sdcard/Pictures/ etc in textview as clickable link with highlight and underline. I often use Linkifyfor display links, however, this does not support storage links (only web, email etc).
Is there any possible way for that?
I would recommend displaying the image in an ImageView in an Activity.
You can put logic in your onClick(View v) to open the contents of the TextView in a new activity:
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ShowImageActivity.class);
intent.putExtra("imageLocation", v.getText());
context.startActivity(intent);
}
Then in your ShowImageActivity's onCreate() :
ImageView jpgView = (ImageView)findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/sample-1.jpg");
jpgView.setImageBitmap(bitmap);
setContentView(R.layout.main);
https://stackoverflow.com/a/9509948/5486718
If you need to you can alternatively add a custom intent to the linkify:
String newActivityURL = "content://com.example.yourapp.yourtargetactivity/";
Pattern pattern = Pattern.compile("[/sdcard/]+[[a-f][0-9]]+[.jpg]");
Linkify.addLinks(hashView, pattern, newActivityURL);
Related
I am trying to add a URL on my Android App login page which redirects user to recover password.
I guess you want to show the user a text looking like a url so that when the user taps on it you can redirect him to a web page.
In your xml layout, declare a TextView with text attribute set
<TextView android:id = "#+id/txt"
...
android:text= "Click me !">
And in your activity class,
txt = (TextView)findViewById(R.id.txt);
SpannableString content = new SpannableString(txt.getText());
content.setSpan(new UnderlineSpan(), 0, txt.length(), 0);
txt.setText(content);
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.google.com"));
startActivity(i);
}
});
You are thinking in terms of how a web page would do it, but it doesn't necessarily have to be a text link like a web page. You could simply have a button (or any view really) that takes the user to password recovery when touched.
As an example, this is how you would do it with a button in your layout:
Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.your-password-recovery-page.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
This will launch the default browser of the device and navigate to your password recovery url.
I am creating an android studio colouring app and need to have it so that the user can select which image they want to colour. I want it so that they click on an image button on one activity and this changes the background of the next activity that the button takes them to however I have no idea how to go about this. Any help would be greatly appreciated. Thankyou
From what I understand, you want to change the color of the next activity on button click of current activity. So, what you can do is:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredColor to get the color according to your logic
intent.putExtra("color", getDesiredColor());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("color")){
rootLayout.setBackgroundColor(intent.getExtra("color"));
}
...
}
Let me know in case you have any doubts.
UPDATE:
With drawable your code will become something like this:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable());
mContext.startActivity(intent);
}
});
In your second activity's onCreate
onCreate(...){
...
View rootLayout = //Initialize root layout here
Intent intent = getIntent();
if(intent.hasExtra("drawable")){
rootLayout.setBackground(intent.getExtra("drawable"));
//Or if you are using ImageView in your root layout to set the background image (I'm using Picasso here):
//Picasso.with(mContext).load(intent.getExtra("drawable")).into(myBackgroundImageView);
}
...
}
UPDATE 2:
You can have a hashmap mapping each imagebutton with a drawable.
e.g.
HashMap<Integer, Integer> mViewIdToDrawableMap = new HashMap<>();
mViewIdToDrawableMap.put(mImageButton1.getId(), R.drawable.image1);
mViewIdToDrawableMap.put(mImageButton2.getId(), R.drawable.image2);
mViewIdToDrawableMap.put(mImageButton3.getId(), R.drawable.image3);
public int getDesiredDrawable(View view){
return mViewIdToDrawableMap.get(view.getId());
}
How will you call this function:
button1.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
Intent intent = new Intent(mContext, MyActivity2.class);
//Implement getDesiredDrawable to get the drawable according to your logic
intent.putExtra("drawable", getDesiredDrawable(view));
mContext.startActivity(intent);
}
});
Now, your last question what is rootLayout?
Lets say your activity2 where you want to show this image has somehitng like this as layyout:
<RelativeLayout>
<ImageView
...
android:id="id+/background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
...
/>
</RelativeLayout>
In your Activity2's onCreate, do somehting like this (after getting the drawable as I explained earlier):
//Here mBackgroundImageView is the background_imageview in your layout
Picasso.with(mContext).load(drawable).into(mBackgroundImageView);
I have an activity where the user can select their character. I have three image buttons on the screen and when the user clicks the character they want, I want the game to load another activity that will then just show an image of the character they selected in the previous screen. Basically this is just the forerunner for what I am going to do later. I found some examples on this website for how to accomplish this but I haven't quite gotten it all ironed out.
This is what I have in my character selection activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
intent.putExtra("#drawable/archer", pathToImage);
startActivity(intent);
finish();
}
});
The pathToImage line is throwing an error. What exactly am I supposed to put here?
The LevelOne activity which is supposed to just display the image chosen has this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
String path = getIntent().getStringExtra("imagePath");
Drawable image = Drawable.createFromPath(path);
Character_Chosen.setImageDrawable(image);
}
I'm a bit confused on this section as well. The Character_Chosen entry is the name of the imageview which should house the selected image.
I'm also confused on this line of code:
String path = getIntent().getStringExtra("imagePath");
Does this mean I have to manually enter the image path every time? What if they choose a different image?
Does anyone have a link to an actual working example? Pseudo code doesn't really help me very much when I'm a novice and don't know what needs to stay and what needs to go.
I would take an alternative approach, instead of passing the path to every activity you want to show the image(or say avatar).
I would save it in Global Application Object(by extending Application) either a bitmap loaded into memory or path to image and just access or change it in one place and access that value in all the activities.
First of all convert the resource into id
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id =getResources().getIdentifier("imagename", "drawable", getPackageName());
Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
intent.putExtra("image_id", id);
startActivity(intent);
finish();
}
});
LevelOne.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
int imageId=getIntent().getIntExtra("image_id", 0);
Character_Chosen.setImageResource(imageId)
}
Same way can be done for all the remaining buttons.
Happy Coding :)
I made it running by doing a different approach.
MainActivity.java
What I did is I pass the R.drawable reference in the intent instead using the string path.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnArcher = (Button)findViewById(R.id.button_archer);
btnArcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,LevelOne.class);
intent.putExtra("archer_drawable", R.drawable.archer);
startActivity(intent);
}
});
}
On levelOne.java is I used the getIntExtra using the name that I specified in the MainActivity.java to get the R.drawable resource reference of the archer image( you can assign a name whatever you want ). Finally use the integer value that you got from getIntExtra and use it on the the image view by calling the method setImageResource(int resId).
ImageView img;
int drwResource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level_one);
//find the ImageView fron level_one.xml
img = (ImageView)findViewById(R.id.character_image);
drwResource = getIntent().getIntExtra("archer_drawable", -1);
img.setImageResource(drwResource);
}
level_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/character_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
I hope this helps. :)
I guess that you know the ICS contact application.
There is a fancy and usefull way to display pictures in a cropped way and then on a click, the full picture appear.
I just wanted to know how to achieve this..
Is that a new Activity?
Should I create a popup with the full picture and create the animation in the same Activity?
Thank a lot for any help..
I have already tried:
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ActivityDetail.this);
ImageView view = new ImageView(ActivityDetail.this);
//build the view
builder.setView(view);
builder.create().show();
}
});
The big problem is the animation!
The dialog is not as neat as the Contact apps.
you can use scaleType attribute of ImageView like this,
<ImageView android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="matrix">
</ImageView>
android:scaleType has different values given on android:scaleType samples
then,
You can pass the imagesourceid as intent extra somewhat like this.
Intent newintent = new Intent(test.this, image.class);
newintent.putExtra("IMAGE", R.drawable.yor_image);
and then accept in the destination activity and use as below
int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.img);
img.setImageResource(imgid);
I have an Imageview that when user clicks on it a dialog box opens and it is suppose to show image larger within it.
the Imageview I have is in a layout and the code is this:
ImageView image_terrain = (ImageView)findViewById(R.id.imageView2);
image_terrain.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Universitymap.class);
intent.putExtra("imageName", "sattelite");
Dialog d = new Dialog(Universitymap.this);
d.setContentView(R.layout.image_dialog);
d.show();
}
});
I use intent for sending which picture clicked by sending it's name.
now in image_dialog layout I have this code:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
loadImage(fileName);
and the function that loads image in image_dialog layout class is:
private void loadImage(String fileName){
ImageView img = (ImageView)findViewById(R.id.img_Picture);
int resID = getResources().getIdentifier(fileName, "drawable", "com.neema.smobile.Main");
img.setImageResource(resID);
}
one word = it doesn't work. I would be happy if anyone help.
Try this
int imageResource = getResources().getIdentifier(fileName, null, getPackageName());
imageview = (ImageView)findViewById(R.id.img_Picture);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
Why are you passing the filename via an Intent? If you are creating the Dialog in the same activity you could create a custom Dialog with a custom Constructor and overgive the filename.
Also i think this line doesn't make sense:
Intent intent = new Intent();
String fileName = intent.getExtras().getString("imageName");
Since you are trying to catch the String from a new Intent, instead of the Intent you sent.
Or did I missunderstood youre way?
The Intent is for starting Activities not for Dialogs :)
Edit:
class CustomDialog extends Dialog{
private String fileName;
public CustomDialog(String fileName){
this.fileName = fileName;
this.setContentView(R.layout.your_dialog_layout);
}
#Override
public void onCreate(Bundle savedInstanceState){
ImageView image = (ImageView) findById(R.id.image);
image.setBackgroundResource(....//set your Image like mentioned from kumaand using your saved fileName
}
}
Should work. For more Information you could look at http://about-android.blogspot.de/2010/02/create-custom-dialog.html