images from button press,images enlarged - android

new to android development and run into a hurdle,im making an app that will have a lot of pictures, what i want to achieve is after i have pressed a button in the main activity to take me to another activity, i would like a page full of buttons on this new activity that once pressed each button will show a image and have some text i would like to add, i would like to do this without having to create a new activity for each photo so i am hoping somebody could kindly help me with this! think of it like a sounbank app that plays a sound when the buttons from each row is pressed! in my case it will show an image for each button, could this be done with just the main activity and an extra activity or will i end up with an activity open in project explorer for each picture.
your help is greatly appreaciated thank you.

Ok lets assume you are using drawables from your resource folder.
In that way you deal with integer ids that represent the images.
These id can be transmitted per Intent to a second activity, that
retrieves the intent, parses the id and loads / shows the image.
In your Activity A, where you have all your buttons, I assume further that
you've added OnClicklisteners to the button like:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//something
}
});
In your click listener you replace //something with:
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, R.drawable.oneOfYourImages);
startActivity(i);
The first argument is the key to retrieve the id later and the second is the id.
The R.drawable.oneOfYourImages depends on which button got clicked and shoud
be different for every button.
You can do this for every button, or you create a method that returns an
OnClickListener with the id as parameter.
private View.OnClickListener showImageFor(final int resIdOfImage) {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext, ActivityToShowImage.class);
i.putExtra(ActivityToShowImage.IMAGE_KEY_SOME_STRING, resIdOfImage);
startActivity(i);
}
};
}
Now, in Your ActivityToShowImage, you override the onCreate like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
int imageId = 0;
Bundle extras = getIntent().getExtras();
if(extras != null){
imageId = extras.getInt(IMAGE_KEY_SOME_STRING, 0);
}
if(imageId == 0){
//some log message or exception and don't forget to finish in onResume()
}else{
loadImage(imageId);
}
}
And in your loadImage method you load the image via the id into an ImageView.
I hope that is the answer to your question and sorry for the late response.

Related

Using the same activity for different buttons but different action using xml resource

I am doing a project in Android Studio. I will have 2/3 activities at most. On main activity I will have some buttons. There will be a second activity. So for each button that second activity will pop up and show some more buttons or imageButton but they will be different. The buttons on the second activity will also be meant for different actions but all of it will be on a third activity. The third activity will have some images and a text view. So image view and text view will show different data from the "XML resource" for different buttons. Any ideas how I can do it?I am using Android Studio.
Can it be done using base Adapter?
Also when all the data are stored on sql database, what different thing do I need to do?
Thank You!
When you go from main activity to the second activity, you must be using intent.
You can add extra info in your intent to to modify accordingly using putExtra()
Then in your second activity, you can simply retrieve the extra info and make changes accordingly.
in main activity,
//your first button
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load first set of images");
startActivity(newintent);
}
});
//your second button
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent newintent= new Intent(MainActivity.this, SecondActivity.class);
newintent.putExtra("details", "load second set of images");
startActivity(newintent);
}
});
in second activity,
String extrainfo=getIntent().getStringExtra("details");
if(extrainfo.equals("load first set of images"))
{
//make your changes accordingly
}
else if(extrainfo.equals("load second set of images"))
{
//make your changes accordingly
}
else
{
//make according changes
}
This is how you can achieve it.
Hope this helps!

Unable to show list-view when clicking back button in android

I am showing some data in list-view,actually i am getting data from the web service by consuming it.
If i click any item from that list-view it should go to the next page and will have to show some data on the next page (say next_activity.java),well its working fine.
But on the next_activity.java page is having one back button,if i click that button it should have to go to previous list-view activity okay.
Well the intent is working fine,
but
its not showing the list-view.why?
Here is the code i have used for the back button.
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i_listagain = new Intent(TabhostActivity.this,StationsListActivity.class);
startActivity(i_listagain);
}
see what you are doing .
1- you are creating new Intent for previous List-activity .there is no need of that.
2- instead of creating new Intent use.
finish()//this ll finish your current activity
3-This will finish your current activity and will go your last activity on stack i.e your List-activity
Just write this:
list-view activity
mlistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this,new_Activity.class);
startActivity(intent);
}
});
new_activity.java
btn_back = (Button)findViewById(R.id.button_back_tab);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
this.finish(); // just write this
}

Using dynamically created buttons in android to take user to another activity with dynamically generated content

I have a list of buttons in one of my activities that are dynamically generated, and I was wondering how i would get one of those buttons to, when clicked, open another activity and display text based on which button in the list was clicked.
I generate the buttons using a for loop (I've ommited details relating to TextViews in the loop for easier reading, it also used some variables defined elsewhere)
for (int i = 0; i < N; i++) {
// create a new Button
final Button rowButton = new Button(this);
// Set properties of rowButton
rowButton.setText("See Recipe");
rowButton.setId(RecipeArray.get(i));
// add the Button to the LinearLayout
myLinearLayout.addView(rowButton);
// save a reference to the Button for later
myButtons[i] = rowButton;
}
The buttons represent a certain recipe and when clicked they should take the user to a new activity "HowToMake" and generate a textview with the information relating to that recipe only. They are stored in an array at the bottom of the code snippet "myButtons[i] = rowButton" But I'm not sure how I would use this.
Thanks for any help.
You'll have to add an onclicklistener to each button as you add it. Then in the onclick event, you can call the startActivity method of your current Activity. When you create your Intent which will open the new activity, you can add "extras" to it (in other words, you can add some extra data which will be passed into the new activity). New data, such as the ID of the recipe that you want to open :)
Example (Untested, but should be along the right lines):
rowButton.setTag("the unique ID by which you can read back your recipe");
rowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(YourActivity.this, TheViewRecipeClass.class);
intent.putExtra("id", v.getTag());
startActivity(intent);
}
}

Use single xml layout for multiple activities with different datas

I know this is a very basic question, however as a newbie i cant get to work around it.
So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).
And can somebody tell me what is the most practical way to learn android programming?
UPDATE
This is my current code:
public class TemakiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
}
}
For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?
You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.
If you want a different URL to open for each image button you could set it at runtime as follows
public void onCreate(Bundle b) {
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//different action for each activity
}
});
}
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.same_layout);
TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
aButton.setOnClickListener(aButtonListener);
}
private OnClickListener aButtonListener = new OnClickListener() {
public void onClick(View v) {
// go open url_1 here. In other activities, open url_x, url_y, url_z
finish();
}
};
Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.

Android show listview

i want to show this array as a listview in a new screen when a button is clicked.
ArrayList<String> favorite = new ArrayList<String>();
this ListView is a small part of my class. i cant seem to figure out how to implement it with my code (i can figure out how to create a listview in a separate application, and set the onitemclicklistner just for that listview)
i want to display that listview when.
case R.id.ShowFavButton:
Your question isn't entirely clear.... but you would build a separate Activity, possibly subclassing ListActivity (documentation here), and then load it when you click on the button. If your Activity was named FavoritesActivity, it would be something like this:
Button fav = (Button)findViewById(R.id.ShowFavButton);
fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, FavoritesActivity.class));
}
});
If you want to return something from your FavoritesActivity to your FirstActivity (or whatever it is called), you can use startActivityForResult instead of just startActivity.

Categories

Resources