I'm creating an camera app here. I've managed to click an image and display in an ImageView. I have a ImageButton on whose click takes you to another page. On the other page I want to display that image with a set of scrollable images like on Instagram. But I cant load that image on the other xml file.
Help Please.
Code:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_select);
// Image clicked...
ImageView ivPic = (ImageView) findViewById (R.id.ivPic);
// Image button to go to next page...
ImageButton ib = (ImageButton) findViewById(R.id.ibNext);
// Not sure if this is right.....
Drawable iv = ivPic.getDrawable();
ib.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.rating.bar.EFFECTS"));
}
});
}}
In the other class I created this classes variable but I cant use iv(which holds the drawable of the image view). It only accepts integers I think. What should I do?
Read the URI/Path of the image in your imagebutton and add to the extras of the intent calling the next activity and read the extras in the next activity.
Related
I have a scenario in which I have to to add text and multiple images, something like below
"fooo bar lorem ipsum somtext.
and all these images have clicklisteners so I can know which image is being clicked.
Till now I am able to place images but unable to implement click listeners on that
Associate a Tag with each imageview and
Set a common onclick Listner for both imageview in onclick even find the imageview by tag associated with View in onClick() method Like
Let two imageview are iv1 and iv2
iv1.setTag("TAG1");
iv2.setTag("TAG2');
OnClickListener oc=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag=(String) v.getTag();
if(tag.equals("TAG1"))
{
// Imageview is iv1
}else{
// Imageview is iv2
}
}
};
Try it and let me know if problem exist
Can someone help me with the code for
-if i click on button it should display a full screen image then again if i click on button on 2nd page it should do the same.
i want to know how to connect different pages/activities,when you click a button it should display some image and then again clickin on that page will display another image and so on,
images should be full screen
In your activity after setting content view use this code to start your image activity.
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),
ImageActivity.class);
startActivity(intent);
}
});
Now put a ImageSwitcher or Image view and set a touch or click listener to change Images as per your logic.
Let's say you have two activities : MyActivity1 and My Activity2
In MyActivity1 class,create a button and on Button's click listener call MyActivity2.class using startActivity(new Intent(MyActivity1.this,MyActivity.class))
MyActivity2 class will contain one ImageView that will have its width and height as "fillparent" value in xml.
You can continue this process for as many activities you want.
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 am making an app in which an image is loaded from the gallery, now the user can add re-sizable and dragable rectangles on the image on the click of a button.
Right now i am able to add the image and also add a required rectangle on the image on the click of a button.
But what i want is that if the button is clicked again, a new rectangle is added, and the previous is restored as it is.
My code is as below..
public class ImageActivity extends Activity {
FrameLayout fm; //framelayout to add views on the image
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Bundle bundle = getIntent().getExtras(); //5 lines
final String value = bundle.getString("key"); //of code
Bitmap bmp = BitmapFactory.decodeFile(value); //to add
ImageView imageView = (ImageView) findViewById(R.id.imgView); //the image
imageView.setImageBitmap(bmp); //from gallery
Button bd = (Button)findViewById(R.id.buttonDraw); //button to add rectangle
fm = (FrameLayout)findViewById(R.id.main_view); //framelayout object
bd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
DrawPanel dview=new DrawPanel(ImageActivity.this);
fm.addView(dview);
}});
}
}
I am creating a new object of mycustom view for the rectangle each time the button is clicked. The problem here is that when the button is clicked for the first time, the rectangle is drawn with each of its functionalities.
But when i click the button again the previous one vanishes and no new rectangle is drawn.
There is no message in the console or in the log cat. What am i doing wrong...
I want to create simply application where user can review some pictures. In list I have some pictures and I want to show next pictures after click button. So this is the way: user start application and after click on the button, previous picture is replaced by next picture from the list. I want to use ImageView to show pictures. Can anyone help me?
I tried:
ImageView img = (ImageView)findViewById(R.id.image);
img.setImageResource(picturelist.get(pictureLeft));
where picturelist is list with pictures, and pictureLeft is int variable represents my index. To my list I add elements by:
picturelist.add(R.drawable.car);
do something like this:
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
if(pictureLeft < picturelist.size())
{
img.setImageResource(picturelist.get(pictureLeft));
pictureLeft++;
}
}
});