android studio issue related to emulator and real time mobile phone - java

I created new login page in android studio. that activity xml looks good on Emulator but in real time mobile its looks like irregular alignments what can I do for that? I used Linear layout for that.

The most efficient solution is to use ConstraintLayout.
ConstraintLayout has a flat view hierarchy, unlike other layouts, so it performs better than various other layouts. Only a single layout can handle your UI.
Or you may use a RelativeLayout, and then use LinearLayout as a child inside RelativeLayout, i.e., you will need to create a hierarchy with some nested views, which will adversely affect performance.

Most probably you hard-coded pixels, try converting to ConstrainsLayout. 1 LinerLayout can not provide enough flexibility if the layout is not simple.

Related

Android: What is the best way to avoid code duplication when several screens of the app have similar layout?

My application has several screens, and each of them has almost the same layout.
I know I can use the "include" tag in xml to avoid rewriting the same layout again, but I also do not want to duplicate the java code of setting the behavior/properties of views and layouts.
What is the best practice to be more organized in this case?
Edit: For example in several screens, the first half of the layout is a gallery that scrolls horizontally with text below it. This is the same throughout my app
If the screens are the same for example. Activity/Fragment
You can create a "generic" parent that handles the logic and use include for the layout.
Or
Create a custom view that uses a layout as content and you can write your logic there.
I create a library that uses this principle.
here is a tutorial on how to create a view with a layout file

Designing for different screen sizes using ConstraintsLayouts

I have started to dive into the Constraints Layout that were announced on the IO this year. This looks very promising and makes your layouts very efficient. I have seen the codelab excersize provided by Google. However I have the following questions that I would like to resolve.
How can ConstraintLayouts be used to design the layout for different screen sizes?
Currently I design the layout and set the views' margins and paddings etc for a specific device/screen group. Save those values in the dimens.xml file. This is a REALLY painful method to design the layouts. Does the Android ConstraintsLayout mimic the AutoConstraintsLayout of iOS in designing for multiple screens as well?
Does designing the Layout for different screen orientation still need different Layout.xml files i-e; layout_landscape.xml and layout_portraint.xml?
Yes, the entire point of ConstraintLayout (and for that matters, any Android layout since Android 1.0) is to be able to handle different screens (dimension, density...). You have nothing special to do (other than specifying constraints between widgets).
If you want to design for different screen sizes, think about margins as "minimum" spacing; and create constraint between widgets so that you position widget relative to other widgets.
You also have guidelines and bias centering, which give you a lot of flexibility.
Regarding the screen orientation, it is entirely up to you -- if you do nothing, the layout you define will be applied on either orientation. So, it would "work". But often, you want a different UI between portrait and landscape. In that case, you can indeed create two layout files, and the usual system will pick the right one for you.
Finally, you can easily test how the layout you are working on would behave in a different screen/device by changing the current screen definition from the popup in Android Studio:
You can also use the resize handle to see the layout to an arbitrary screen dimension.
How can ConstraintLayouts be used to design the layout for different
screen sizes?
If by different screens you mean phone and tablets then you have to create different layout for them. one way to do it is to define layout-sw360dp,layout-sw600dp and layout-sw720dp.
Currently I design the layout and set the views' margins and paddings
etc for a specific device/screen group. Save those values in the
dimens.xml file.
You might wanna look into chains and guidelines.
Does designing the Layout for different screen orientation still need
different Layout.xml files i-e; layout_landscape.xml and
layout_portraint.xml?
Yes.

Android resize entirely layout with all his views dynamically

I have added in a Relative Layout some ImageViews with some Buttons and gave them some width and high values.I have tested the application on a nexus 7 and everything looks great but when I tested on other devices some functions like a popUpWindow or even some Views look wrong.Is there a way how to resise a layout with all his views dynamically to fit every device I test on ?
The values you're using are probably not what you're looking for. Instead, you may mean to use wrap_content or match_parent.
Please have a look at this tutorial on layouts and absorb and practice some of that before immediately asking for help. You got this!

LinearLayout to animate when shown

I have a vertical LinearLayout which has two another vertical layouts inside. The first one is always shown, and the second one depends on some event. I do want to animate that second layout when shown. How can I do it smoothly and nice? Thank you so much,
You should check out this library on Github. It may help. It is an animation library for listviews.
ListViewAnimations
A very simple, yet elegant way to get your layouts animated is by using the android:animateLayoutChanges="true" on its parent. This will likely take a lot of work in getting a nice animation out of your hands, unless you want to customize it.
With KitKat, Google introduced the android.transtion API, which may also be worth looking at.
You can create your own animation and you apply that animation to your layout when you need(In your case at the time of shown your layout)
yourLayout.startAnimation(yourAnimation);

Android layout in java code

i'm making an app that requires indefinite textviews and images. i'm trying to implement a Pulse News app UI but having a hard time implementing one. so i thought of an idea to make a UI like that with the use of textviews, imageview and horizontal scroll view.
textview string values are from parsed xml online and images or the imageviews will be images from a specific directory in the sdcard that my app is using.
can anyone give me ideas how can i do it without using an xml layout or is there any or other options or ways for doing this? thanks...
You can create a viewgroup with one textview and an image. Then it can be added dynamically to your layout many times. This can be done by creating objects in a loop. You can change the content in each viewgroup at the time of inflation.
though i dont know what exactly how pulse new app looks like, but by going through your question (horizontal scroll view in particular) I guess you want to implement a "Gallery" type implementation where in you can swipe left/right on page basis.
If my assumption is correct then you will like to see to ViewPager of android backward compatiblity pkg.

Categories