Generate Object to it's desired coordinates - android

Just need some help. I am currently making an application for our project. My application get the user input which is number because it gets the quantity of the object. My problem is how i will make object to put on it's desired coordinates. Example chair and i enter quantity of 2 then if you click the button you will be directed to next page then you will see the 2 chairs. thats the interface of application when the user use it. but we need to put coordinates to the chair so that it likes generating object and arrange them to our desired coordinates.
Thanks Guys. :)

Do you actually need to calculate screen coordinates yourself? It would probably be easiest to build a LinearLayout and then add items to it in a loop.
See the first answer here for details on how you can do that: Android: Add a textview to linear layout programmatically
You could do the same with a RelativeLayout if you need more complicated positioning, or an AbsoluteLayout if you want total control over positioning. I would be hesitant to use those unless you have a need that's not met with the LinearLayout.

Related

Android - get values from dynamically added fragments

What I have created till now is as follows:
Please see this image
Purchase Screen
As shown in the image above, the screen (PurchaseActivity) initially has:
1. spinner for customer - line 1
2. fragement containing spinner for product, rate, and a delete fab - line 2
3. linear layout for displaying fragments same as above. They will be dynamically added.- line 3
4. fabAdder, adds fragment dynamically - line 4
5. save button for saving to persistent layer. line - 5
To add views dynamically, is this the right way. If so, how do I get the values out of the views inside fragment which are dynamically added in my activity.
Otherwise what is the right way of handling/providing the user with controls to add data dynamically.
Advantages of using this method:
The user will be able to add as much product as he wants, he may delete it conveniently and also editing is easy.
Please use the comments section to if you feel the question needs more content.
Answer or lead me in right path to fulfill this requirement.
-Newbie Androider
You can make outside class similar to POJO for handling values from fragment simultaneously. Something like this answer of mine for some other question: https://stackoverflow.com/a/44051019/5577679
Otherwise, why don't you make good old POJO for values?

how to create multiple textviews inside a fragment?

I am trying to create a fragment and inside that i want to create multiple textviews whoose text is set from the data retreived from the database(mysql).
And also i want to have the feature "refresh" by which the textviews are updated with recent data from the database.Iam trying to do that but iam not getting enough idea how to do it as iam new to android programming.
Please help me with this. thank you in advance and any suggestions are acceptable.
i.e
I have database of a person and i want to get his name and address from that,then in my activity i want to create a fragment that displays a name and address in separate textboxes and if address or name exceeds some length then i can press "more" to see left information
Consider using a ListView to populate data from the database, and a SwiperRefreshLayout to refresh it. Check these links for more info :
SwipeRefreshLayout
ListViews
The answer depends on what your app will do.
If you have a fixed number of TextViews, and the number won't change in the future or depending on the answer to the database query, you could simply stick to normal TextViews; if the number changes, I suggest using ListViews or ExpandableListViews.
Regarding the layout, you can create a normal layout using AndroidStudio's layout designer, and then inflate it to your view.
For the refresh, you can add a button that calls the method in which you fill the textviews/listviews.
Actually your question is too broad: if you want a more specific answer, you should edit it asking what you really need.

Android Dev: Generate buttons and text fields based on user input

I'm fooling around with Android and trying to make a simple application to split a bill between friends.
I'm wondering if there is a way to create buttons and textfields based on user input. For example, if the user says he needs to split the bill 5 ways, how can I generate 5 buttons and 5 textfields? Similarly, splitting 10 ways will create 10 buttons and 10 text fields.
Thanks,
Ben
Edit: I should mention I have implemented the input already. I simply need to use the input to generate the buttons and textfields.
What you need is to create a Linear layout that have only a button and textfield then you can dynamically add that layout into your activity main's layout programatically. You can follow this thread on how to add view dynamically to another view
I've had a similar problem. You need to make a custom layout and then use LayoutInflater to "put it into another layout".You can use the return of inflater.inflate() to get the parent layout. then you just use parent.getChildAt(i) in a for loop for all "fields" and getChildAt(whatever the index of the button or text field you want is) to get the buttons and access them programatically. You can also give every Button a unique ID with setId(generateViewId(yourButton)) to access them from other functions.

Java-Android / How to dynamically add plots to an AchartEngine chart

I have 2 Fragments, one with my chart and below one with a TableLayout with my TimeChart values. I would like to add to my chart, a function when i click (or hover) one value of my table, a plot draw himself on the chart at specific date and value but i didn't find yet a way to do this. Could some have already done something like that?
Thanks in advance
You can definitely do this. One option would be that you add an extra series that would contain only the extra point you want to render. When you want to render it at another location, just remove the previous one and add another one. Don't forget to call chartView.repaint(); after every such change.

Click button and that button waits for second button on activity to be pushed. Game board. How to accomplish?

I wasn't exactly sure how to briefly paraphrase my question in the title, so please forgive me as I never post questions until now. I am new to Android/Java for starters, as the main language I have used so far is C++. My question is that I have a game board layout (similar to checkers/chess). When it is the user’s turn, they are to click the piece they want to move on the board and then the blank location they would like to move it to. How can this be accomplished? Up until this point I have implemented onClickListeners that never rely on another button in the activity to be clicked afterwards and wait for the user to do so.
Brief information on my project currently (unsure if needed):
I am currently using an ImageButtons array (of size 36) and a two dimensional integer array to hold the information of each of the buttons, as they are displayed in a GridLayout in a 6 X 6 fashion. In my MainActivity class I have implemented the OnClickListener and created a switch statement in onClick() for each of the button ids.
I am not sure how much more information is needed on my code for help or if it is completly irrelevant. I tried looking on the internet before choosing to ask finding nothing. It is always possible though I was not correctly phrasing my issue. Thank you to everyone in advance!!! :)
Without any code it is hard to say what you are doing wrong/right. But if you have the images stored in an Array then when they click on one image that can be put into a variable which is used to place on the View that they click on next. If you have a more precise question then please post relevant code and error messages from what you have tried, if any. Hope this helps
after a user click's a piece store that info in a member variable like a current piece and then on second click check if the previous is not null, the current clicked space is empty and start your animation by posting to a runnable implementation. this is the simple logic part. where are you having problem exactly
To optimize your code to change images what you can do is custom animations on the imageview. You can change the position of the image with animation without having to create or store an image. (If you use images bitmap a lot you have a very good chance of running into OutOfMemory exceptions and this will happen on top end phones very easily
How to Move a Button and Change the Button Image
I would put a single Button on every GridView position.
Then, I would use button.setBackground() and button.setVisibility() to display and hide the buttons with various images. In this way, the buttons will appear to move, but you are really just displaying a different button.
I recommend this because it is easier to change the visibility and image properties of a button than it is to actually move the button, although both methods are possible.
You will maintain a 1 dimensional array of images[n] and a 2 dimensional array of buttons[6,6].
As an example, suppose you want to move image[5] from grid position (1,2) to position (3,4):
// Hide the button at (1,2);
button[1,2].setVisibility(Button.INVISIBLE);
// Display the button at (3,4) with image #5.
button[3,4].setBackground(image[5]);
button[3,4].setVisibility(Button.VISIBLE);
Additionally, if your button images are stored in your resources, you could efficiently use button.setBackgroundResource(R.drawable.image-5-id);
The instructions above discuss how to move a Button, but now how to trigger one Button to be moved and then trigger the location into which to move the Button.
To accomplish this, you will have to define two states, such as:
private final static int STATE_PICK_BUTTON = 0;
private final static int STATE_PICK_LOCATION = 1;
private int state;
Initialize state = STATE_PICK_BUTTON;
When the system is in the first state STATE_PICK_BUTTON, all button
presses identified in your onClick() function memorize a grid
position to move from, and in some cases transition the system into
the second state: STATE_PICK_LOCATION.
When the system is in the second state STATE_PICK_LOCATION, all
button presses actually move the button from the memorized grid
position to the grid position of this button press.
Of course, you will have to do all sorts of error checking to make
sure you are allowed to move a button before triggering the state
transition.
Finally, the above suggestion may not work, because it may be impossible to click an invisible button. If this is the case, instead of changing the visibility of Buttons in empty grid locations, leave all the buttons Button.VISIBLE and use a fully transparent Button image for the buttons representing empty grid spaces.

Categories

Resources