im coding an android app for a board game. The user can choose in the main activity how many dices he want to roll(1 up to 3).
I would like to use only one activity to display the result of the dices as images. The user can go back to the main activity and choose a other amount of dices.
My problem is, how can i dynamically add the needed imageviews for the result images into the second activity where i want to display the results of the choosen amount of dices?
Create a new ImageView object, set its properties, and then add it to an existing layout. Here is the most basic form, but you will need to add LayoutParams as well to get the desired location.
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(R.id.myimage);
someView.add(id);
See the ImageView Documentation for more usage details.
Related
I am trying a make notepad app so every time a new note is opened the layout will be the same. also, the number of activity(new note) should not be defined as many possible
If the activity is always the same, you should probably create an adapter that allows you to change the texts and images of your activity without the need to create several activities.
I want to develop an Android app, where start page of the app GUI, will contain 4 vertical layouts in the main layout. Now, in each layout, I want to add buttons/slider dynamically from the app (instead of adding buttons/slider dynamically in the source code). That means, initially all these 4 layouts will be blank and when user will select any button or slider in another layout, to add it in any of this 4 layouts, the button or slider will be added in that layout. User will be able to add max 10 views in any vertical layout and the views can be either button, slider or custom view.
My attempt:
First I tried to create 4 vertical layout under the main layout for startup page and I got succeed.
I also find after searching that its possible to add views dynamically in layouts in android.
dynamically adding a view to activity layout
But most examples, add views dynamically in android by running loops, instantiating the desired view class and then add it in the main layout. Although, in this way, views are added dynamically in the layout, it is done by modifying the source code.
Is it possible to write the source code in a way, so that it can be done directly from the app? So that when user will click on Add a slider in "layout 1", a slider will be added in layout 1 and then again, when the user will click on "Add a button" in layout 1, a button will be added at the end of the slider. User will be able to customize button or slider properties. Also, if the user change the value of the slider, the app will remember its value.
Now, next time, when the app will be opened, those views will be there in the layouts, they will not be deleted and the values will remain unchanged (for example, a ticked check box will remain ticked), so I think I also need some kind of storage or properties manager.
My question is, is it possible to do this in android (because I never seen such apps in android) and if possible, any idea, how can I implement it?
I am totally new to android, so my knowledge is limited but I completed the basic tutorials on android app development and I have plugin development experience in eclipse.
Thanks a lot in. I will highly appreciate your help.
Of course it is possible:
Every layout (like LinearLayout, RelativeLayout etc.) extends the ViewGroup-class, which offers the addView-method.
To add a new view (like a Slider) to one of your layouts, just instantiate it programmatically (via new) in your activity and assign the appropriate LayoutParams to it
To store the state of user added content, it is the easiest to use SharedPreferences - a simple key-value-store which holds data over the application's lifecycle
Yes. This is possible. To create the Views dynamically, you simply have to either extend the class of the View or just say new Button(Context, AttributeSet); (Not only for Button's every View has a constructor that takes an attribute set and a context).
Using Layout.addView() you can add any View to the Layout.
Using SharedPreferences you can indicate what View belongs in what Layout.
If you decide to extend the View's class, make sure not to do too much in it. I tried that once and it just gave me an OOM (OutOfMemory Error) because I had a ton a Views trying to do stuff at the same time.
I have a ListView that is like an inbox. The information comes from a user,so the ListView displays text and a userPicture.To display userPictures, I display the userPicture if exists or a imageView of a letter of the first name.
when I receive data from WS I launch an asynctask that compare userPicture ids with DB to ask the WS for ones I don't have or one's that have changed.
After the asyncTask finishes I save pictures locally and notify listView that the data set has changed, in order to look for the new pictures.
my problem comes when there is only 1 user on the listview and the picture wasn't found but was downloaded from WS. after calling notifyDataSetChanged, also calling refreshDrawableState and invalidateViews,that acording to documentation "Causes all the views to be rebuilt and redrawn". the image is not changed and imageView still shows the letter ImageView,
neither scrolling through the listView its updated. (I guess is part of the Viewholder that recycle the views that is meant for performance and a good practive acording to ).
on the getView of the adapter I have a the Following statement to processThe Picture
if(mIDataPersistence.userHasPicture(userId)
ViewHolder.userImg.setImageBitmap(mIDataPersistence.getUserPicture(userId));
else
ViewHolder.userImg.setImageBitmap(BitmapUtils.createBitMap(userName);
note: mIDataPersistence is persistence data interface to access all the data locally stored that I'm using here. and BitmapUtils.createBitMap() makes a bitmap from the first letter of the user.
can anyone help me or point me on a way to make listView update images in this particular case.
I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.
I am trying to insert multiple images in one editText. I have used following code to attach image to EditText
txt = (EditText)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeFile(attach);
Drawable drawable = new BitmapDrawable(bitmap);
txt.setBackgroundDrawable(drawable);
But the problem is it only attach one file and show, If i used array then only the last image is only show. at any how it only shows one Image. Is there any way to show multiple images in one Editbox. Thanks in advance.
Because you can only have one background.... if you want more then there is a layer Drawable which you can use and also you can put the button in a frame layout and add a couple of imageViews below/over it for the rest of the images.
But the best solution will probably be instead of having a couple of bitmaps to just make a single one in Photoshop or equivalent photo editing app and place that one.
According to docs: http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)
You are not able to provide more than one Drawable item in argument
And I don't know if you understand what is .setBackgroundDrawable(d) method purpose, but it is not meant to be used for displaying images inside text but to set Background of EditText View.
So you're not inserting images in EditText but setting its background,
look for some Rich Text Edit components usable for Android
such as http://code.google.com/p/android-richtexteditor/
or others..
You can probably use a LayerDrawable with the constructor LayerDrawable(Drawable[] layers) to chain together several images and display them as an EditText's background, though as Marek said, I suspect you're looking for something other than setting images in the background.