How to add a Layout - android

Hello I want to add a LAyout to my Activity.
I have a FrameLayout where I add a SurfaceView so far.
Now I want to add another Layout as a menu over it so when I click a Button on my Surface View I set it to be Visible.
This layout should contain one Textview and should be scrollable
1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Strings dynamically
2: How can I achieve this in Code?
It should be:
Scrollable.
Should be a certain Size: Half of the screen width and half of the screenHeight
So it also should be at the position screenWidth/4 and screenHeight/4
To achieve this I can set these parameters inside my mainThread that is attached to the surfaceview so I have the parameters needed allready in Pixel format that's no Problem.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
So it should go like this:
User taps the button to display the menu
Now the User can scroll through the Menu
When he presses the Backbutton or outside of the View It should close itself
When there is a new Text that should be displayed it should be attached to the layout to be ready to be displayed at the next time the user clicks the button again
Thank you

1: What is the best LAyoutout thing for this approach? I thought of a Listview where I add Textviews dynamically
That sounds good to me, you can go with either ListView or RecyclerView. You are basically not adding TextViews dynamically, you create an adapter that will do handle creating and recycling of views for you.
2: How can I achieve this in Code?
Create a DialogFragment, it's the best candidate for such dialogs with logic. Its lifecycle is handled via FragmentManager, so you won't have issues on screen rotations and so on. It allows you setting whatever layout you need just like any other Fragment. It will be placed in the center for you, so you don't have to handle that manually. Just set the size of the dialog you want and it will look perfect.
It should loose the Focus when the User taps outside of the view so my surfaceView gets the Focusagain and set the LAyout to Gone.
I don't really get this one. When user touches outside, do you need to lose the focus but the dialog should not be closed? If yes, then this is already answered here:
Allow outside touch for DialogFragment
If the dialog should be closed, you don't have to implement anything, it works like that by default.

Related

Android: Detect if user touches a view inside parent view

I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App

Change layout when soft keyboard is shown

I would like to change the layout when an EditText is clicked and the softkeyboard is shown, so all EditText views are still visible.
I know that you can use the following two, but this is not what i'm looking for.
android:windowSoftInputMode="stateUnchanged|adjustPan">
android:windowSoftInputMode="stateUnchanged|adjustResize">
I've got 8 EditText views spread over the whole screen. When one is clicked to change a value I would like to still see all the ET views but nicely arrange and not pushed in a weird view.
Is there an easy way to do this?
I think you are going to have to do this yourself, but one technique might be ...
Create a layout with the views arranged for the keyboard shown (for instance a new RelativeLayout) that overlays your standard layout. Set it's visibility to GONE. Then when you detect the event that shows the keyboard hide the current view and show the alternate one
I suppose you could also use a ViewSwitcher
If you are using both at the same time then it doesnt work.Set the attribute android:windowSoftInputMode="adjustPan" only.That should work.

Any better approach for layout design?

just wondering if anyone else has a better suggestion that what I'm coming up with.
The issue is to do with a TabLayout, 5 Tabs all using a single Activity. Each Tab layout contains quite a few fields so the main parent on each tab is a ScrollView.
What I currently have is a 'Save' & 'Cancel' button sitting outside the scrollviews so theyre always visible and there for the user regardless which tab they're on. The problem is that since I'm using Tabs and always have these buttons visible when they are editing and the IME is displayed, they'res barely any of the form visible.
So I think the best thing for me to do is to probably show the buttons at the end of each scrollview. What I don't like about this is as I have 5 tabs, it will mean I have to declare 5 sets of the buttons, and of course define them and bind them in the Activity.. which seems rather messy and inefficient.
So if anyone has any better ideas I sure would love to hear them :-)
Thanks
Rgds,
Dave
Some of the options you got is
Try to put a title bar, and move the save and cancel buttons there (Small image buttons).
Save can be moved to the menu too (not recommended).
Auto Save functionality can also help, depends on how useful is it to you.
Try to use custom images for tabs, and make it take less space.
else, remove the save and cancel buttons, after the user makes any modifications, and presses back, prompt him to save the unsaved changes.
You could make it that when you're in tab X, the tab button for tab X now becomes X(save), so if they click again on the tab button in the same tab, it saves. That saves you five buttons, potentially...
Define programmatically the buttons inside a LinearLayout and add them to each ScrollView with addView. The code of the buttons should check in what tab are we at that moment, and act accordingly.
Then you'll have the same two buttons arranged in the same way in all your ScrollViews.
Or, if you dislike doing it programmatically, do a layout for the buttons and use View.inflate of that layout, and add them via addView to the ScrollView.

Sliding in a layout for user input

I have a ListView and each item contains a TextView displaying a number. I'd like to give my users the ability to change this number while staying on the ListView (as opposed to drilling down into a detailed view by clicking on the list item).
What I'm looking to do is to slide in a layout from the bottom of the screen that covers about half of the screen. I'd like this layout to be OVER the Activity behind it (as opposed to being part of that Activity's layout and simply showing it). I'd also like it to be model (or seem modal). Meaning the Activity behind it can not be focused and manipulated. In this layout I will essentially create a calculator.
What I need help with right now is:
1) How to display a layout over the current Activity
2) How make the background (the Activity) modal
Could someone point me to some tutorials/resources and/or give me a few tips?
use an Animation. here is a small tutorial on them: http://developerlife.com/tutorials/?p=343
initially, the view you want to be modal must be placed where you want it to show up(and visibility set to gone).
use a translate animation to visually move the view from below the screen to halfway up the screen. once the animation starts, set visibility to visible
try disabling all views that the user should not be able to interact with after you have started the animation holding the calculator view

How do you do your dynamic layouts?

I want to dynamically hide/show controls in an activity based on a button touch event: when the user presses the "details" button, I want to show more controls in the same activity.
What is the best way to handle this? Is there a way to do TextView.Hide()? Can it animate the transition?
setVisibility() will allow you to make a widget visible, invisible, or gone. The difference between the latter two is that an invisible widget still takes up space, while a widget that is gone does not.
Android has an entire animation framework for slides, fades, spins, and the like. Here are two sample projects showing some of this.

Categories

Resources