Equivalent control in Android to WPF Grid - android

I feel so dumb asking this, but I couldn't find the answer nowhere. I'm used to C# and XAML, but I'm getting started with Android Developement, so I'm new with Java and XML and I feel so lost.
What is the control that's closest to the WPF Grid?
How can I define it via code and XML?

Well, another possible solution is to use LinearLayout and then set layout_weight of child elements as required, but make sure to set layout_height of child elements to 0dp.
For Example:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="SomeText"
android:layout_weight=".5"/>
</LinearLayout>

You are probably looking for GridLayout, which is explained in full with implementation examples in this blog post.
Note that GridLayout was first introduced in API 14, Ice Cream Sandwich. If you need to support older devices, you can use the Support Library equivalent, android.support.v7.widget.GridLayout which as its package name implies, supports back to API 7. You'll need to add the support GridLayout to your project by following these instructions.

Related

Android ConstraintLayout generates absolute values

I have recently started learning the new ConstraintLayout in Android Studio 2.2 and noticed that when I add simplest of the views, the layout editor automatically generates some absolute coordinates. Here is a sample XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_portfolio"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.Activity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TextView
android:text="#string/creator_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="246dp"
tools:layout_editor_absoluteY="479dp"
android:id="#+id/first_textview"
app:layout_constraintRight_toRightOf="#+id/activity"
android:layout_marginEnd="16dp"
tools:layout_constraintRight_creator="0"
app:layout_constraintBottom_toBottomOf="#+id/activity"
android:layout_marginBottom="16dp"
tools:layout_constraintBottom_creator="0" />
</android.support.constraint.ConstraintLayout>
Notice the absolutes like 81dp, 246dp, 479dp... I tried to manually delete these, but when I go back to the "Design" tab and come back to the "Text" tab, these regenerate. Now, I have three questions:
Is there a way to tell Android Studio to not generate these?
Should I manually place them in dimens.xml?
Would these absolutes cause some layout problems in other devices?
You'll note that all of the absolute values are in the tools namespace - this means they are not compiled into your app, nor used in anything but in the tools (and in this case, the visual editor). They are simply to ensure that switching from the Design to Text tab is always consistent, with the underlying files remaining stable.
Is there a way to tell Android Studio to not generate these?
No.
Should I manually place them in dimens.xml?
These are only useful for the tools and therefore should not be added to a separate dimens.xml file that would be included in your final APK.
Would these absolutes cause some layout problems in other devices?
No, they are only used by the tools.
I'm not sure your original question contains your entire layout, as it references a widget with an id of #+id/activity, so the issue might lie elsewhere in your layout.
Ensure that no widget that exists within a ConstraintLayout has a layout_width or layout_height of match_parent.
MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
Source
If you use match_parent, Android Studio will generate these absolute values, as well as replacing match_parent with an absolute dimension.
Based on the layout you posted, your TextView probably had a layout_width or layout_height of match_parent before Android Studio replaced it.
You should replace android:layout_width="match_parent" with
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"
And android:layout_height="match_parent" with
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"
In your specific layout, you probably want something like this:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_portfolio"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.Activity">
<TextView
android:text="#string/creator_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/first_textview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/activity"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>
As a side notes to given answers, you can use the Magic Wand Icon in the toolbar menu above the design preview. Click on Infer Constraints button, this will automatically add some lines in the text field and the tools one will be converted to constrained.
Please see below picture :

Android: Why subtitle in ListView not grey?

I use latest Android Studio and SDK. In preview & real device i see this:
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myappname.view.AboutActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewAbout" />
</RelativeLayout>
How i make subtitle text color is gray? Like this:
I'm going out on a limb and assume that you're using the row layout simple_list_item_2.xml (based on the screenshot) which gives you two rows. The problem, if you may call it that, is that depending on the SDK version, the styling for this layout has changed.
On SDK 23, it looks like this:
However, on say SDK 19, it looks like this:
Why?
To understand this we first need to take a look at the xml that generates the rows from simple_list_item_2.xml, you'll see it's a pretty simple layout that uses the now deprecated view TwoLineListItem but that's just a plus on why to use your custom layout.
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
The reason is because of the way the style textAppearanceListItemSecondary is resolved in each SDK version. The style is what gives the text the size, the color, etc. The evolution of the interface in Android has given birth to a huge ecosystem of themes and relying on the default styling will result in inconsistencies like the one you stumbled upon.
What to do about it?
You should use your own layout for this to allow for uniform styling across versions. To do so, please refer to any of the multiple questions covering this matter. But in short it just means creating a layout file, call it for example custom_row.xml and having the layout look exactly as you please. This also gives you total control over placement of the items, extra Views that you may need, and overhead in terms of coding is minimal compared to the SimpleAdapter or ArrayAdapter that perhaps you were using.
Note
You should consider moving your code towards RecyclerView instead of ListView if you haven't already.
You can set Textview property
android:textColor="#color/grey"
in you Adapter layout to change colour of your sub item
Hope this will help

How to split screen like ipad in android?

i want to make application in android Tablets like in ipad which have spliting screen,
How i can do that ? if any body have idea please send me!
Thank you in advance.
This is achievable using the Fragments API. Fragments are available since Android 3.0, but there's also a Support Library that lets you use these API's from Android 1.6. Hope this helps.
Using fragments: check http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
it's a tutorial with sample application that uses the compatibility package (allows you to support from API 4), it may help
this is not a 'feature' limited to iOS
you can easily do this in Android by using two vertical LinearLayouts and assigning them equal weight - to split the screen into half or Different weights to achieve something like the image you provided.
And of course, there are a lot of other ways to do that.
This will be the fragment xml file. then you have to write two xml files for CustomerList(Left Part) and CustomerInfo(Right Part). your activity should extends FragmentActivity. try with this:::
<fragment class="ui.misc.Customers$CustomerList"
android:id="#+id/customerlistfragmant" android:layout_weight="1"
android:layout_width="550px"
android:layout_height="wrap_content"
/>
<fragment class="ui.misc.Customers$CustomerInfo"
android:id="#+id/customerinfofragmant" android:layout_weight="1"
android:layout_width="350px" android:layout_height="wrap_content" />
<FrameLayout android:id="#+id/regscreentwo" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="fill_parent"
/>
</LinearLayout>
you can use the APIs described here: Fragments API

Alternative to nested weights with LinearLayouts

I want to achieve the following:
It works with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:name="com.bobjohn.DetailsMenuFragment"
android:id="#+id/detailsMenuFragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>
<fragment
android:name="com.bobjohn.SummaryFragment"
android:id="#+id/summaryFragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="4"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="fill_parent"
android:text="Test Text"/>
</LinearLayout>
However, I get the warning about nested weights being bad for performance. I understand the error but I don't know how to express this layout in another way. What is the alternative?
There are NEW updates in SUPPORT Libs, Please check Accepted Answer too.
Updated Answer :-
Whenever you create any view, It calls it's measure events to know the height width of view on the screen, If you are not using WRAP_CONTENT or FILL_PARENT or FIXEDSIZE and using Weights then it's becoming more complex to render your layout on the screen.
Means,
First your main layout is rendered and calls it's measure..then based on weight all child views calls it's measure events recursively so it consumes more time to load.
So, One should avoid nesting of weights.
Alternative to Nested weights :-
You should consider using different layout and drawable folder specific to different sizes. Write your views in your XML with specific height-width OR make it wrap_content and use specific background images OR make it fill_parent.
I believe that as developer we may be wrong several time, but as creator Android (Lint) they may be wrong only in rare case, should listen to those warnings to make your code better.
BELOW ANSWER WAS WRITTEN WITH LACK OF KNOWLEDGE ABOUT ANDROID LAYOUT
AFAIK, I think you have done right, this is the best written XML for the same.
You have used the weight attribute perfectly as it should have been used. You just ignore the Warnings.
What is the alternative?
I have coded all my XML in the same way in my projects so This has been the best alternative to me,So I dont think there is any other alternative to CODE the XML to get Such layout until and unless you use RelativeLayout as parent layout with some fixed sizes height and width of the child views. Still I advice you keep it as it is.
I would have deleted this answer as I still don't completely know Android Layouts but keeping it to receive new comments and answer based on this
Yes we have the alternative for nested LinearLayout weight by android's percent support library
Code and concept HERE !
GitHub Project HERE !
Consider this simple layout where I have totally avoided weight property of LinearLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/fifty_huntv"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff7acfff"
android:text="20% - 50%"
android:textColor="#android:color/white"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="#id/fifty_huntv"
android:background="#ffff5566"
android:text="80%-50%"
app:layout_heightPercent="80%"
app:layout_widthPercent="50%"
/>
</android.support.percent.PercentRelativeLayout>
Really awesome !!!
I think (and I will probably be flamed for this), but again I think my phone has a quad core processor to rival (if not utterly destroy) most peoples home PC's.
I also think this kind of hardware capability is the future of phones.
So I come to a conclusion, that as long as you are not getting carried away with nesting (in MHO a layout should never be more then 4 levels deep, and if it is you are probably doing it wrong), your phone could care less about having weights.
There are many things you can do that will have a much more far reaching effect on performance, then worrying about your processor doing some extra math.
(please note that I am being slightly humorous, and so not to take anything too seriously from this post, other then the idea that there are other things you should optimize first, and that worrying about a 2-3 level deep weight is not helping your health)

iOS to Android: How to display a TextView

I have a background in iPhone development, which may be a cause of some of my confusion with Android, which I am very new at developing.
My question is this: How to I create two TextViews, and specify their exact location on screen? For example, on the iPhone, I would create a UILabel, generate a rectangular frame that specified the label's size and position, and then set this rectangle to the frame property of the UILabel.
If you can help me understand the similarities with Objective C and iOS' UILabel, that would be most helpful.
On Android, we don't use absolute screen positions. This is highly discouraged. It's pretty understandable that you think this way if you are coming from iOS. But you need to revise your habits.
Instead of absolute positions, we use layouts, such as LinearLayout, RelativeLayout or FrameLayout. All of these allow you to arrange your views dynamically. And in many cases, it will automagically adapt to the screen size, which vary a lot from device to device.
Actually, there's nothing exotic about dynamic layouts. Many major UI toolkits, such as GTK, or Qt, work similarly. Pixel position are a bad idea in my opinion, except maybe in the Apple world, where the OS and the hardware are tightly coupled, but this is an exception actually.
So, in your case, all that you need is to put your text views into the appropriate layout. Please read the documentation and tutorials about the different types of layouts mentioned above to decide which one is best. The question is how you want your views to be placed relatively to each other.
Create a basic Android project in eclipse. You will be having a main.xml layout file in your project. You can open it in Eclipse using Ctrl+Shift+r and keying in main.xml
copy paste this in your xml after clearing its content.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:text="TextView One"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:text="TextView Two"
android:layout_height="fill_parent"
android:layout_weight="1"></TextView>
</LinearLayout>

Categories

Resources