How to deal with different API levels in code - android

I started with development with material design.
My layouts and styles are separated in those for pre-lollipop devices and those for lollipop and higher.
E.g i use
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/Toolbar" />
In my pre-lollipop layout file and
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
style="#style/Toolbar" />
in the other one.
Therefore I am forced to use two different objects in the related activity class. That confuses me a lot and leads to confusing code.
I know how to distinguish between API levels via
Build.VERSION.SDK_INT
Nevertheless what is the best approach to solve this problem?
Thank you for answers

i usually use android.support.v7.widget.Toolbar, because it's compatible with other API. The regular one will give you a lot of problems if you use it a lot in your code.

Related

How to program graphical interface being blind?

This is a translation from the same question in Spanish, and I'm posting it here since I think you could provide more information than our small community.
I am blind from birth and I am studying the career of systems engineering at the university.
I would like to know how I can program the graphical interface of an application in Android Studio. I have searched for tutorials and found several that, for example, to add a button, drag it to the place where you want it to be and it is automatically added to it.
In my case, by not seeing, this is impossible since my screen reader is not qualified to do that kind of work. So my question is how could I do to program android graphical interfaces without seeing?
You can code your layouts in XML.
Writing in XML gives you the full control on how you want to code your layouts.
I'm also Visually Impaired and I'm coding layouts in XML since many years.
There's a good tutorial at tutorialpoint which can help you in this. Also, I'm pasting one of the xml code sample of a layout from my app for your reference. You can find the code below the link:
Tutorial:
https://www.tutorialspoint.com/android/android_user_interface_layouts
XML Layout Sample:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_create_theme" />
</android.support.design.widget.CoordinatorLayout>

ViewPager inside CoordinatorLayout covers other views on API >= 21

I am using the new CoordinatorLayout from Google's new design support library.
I have the following layout:
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<android.support.design.widget.TabLayout />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager" />
</LinearLayout>
<com.getbase.floatingactionbutton.FloatingActionsMenu
app:layout_anchor="#id/viewPager"
app:layout_anchorGravity="bottom|right|end"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
>
<com.getbase.floatingactionbutton.FloatingActionButton />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</android.support.design.widget.CoordinatorLayout>
The problem is that the ViewPager always overlays on top of the FloatingActionMenu on API >= 21, is there a fix for this? I tried using view.bringToFront() to fix this, but no luck. I also moved the Views around as well, still no luck. Things I tried:
Moving the FABMenu inside AppBarLayout
Moving the FABMenu inside LinearLayout
No luck.
Has anyone experienced anything similar?
This turned out to be a simple elevation issue, I was setting the elevation as android:elevation=".." and this was a lower elevation than another view, thus covering the FloatingActionButtonMenu. This only occurs on API >= 21.
although you seemed to have found the answer yourself, there is an absolutely amazing resource here: https://github.com/chrisbanes/cheesesquare
This is an implementation of viewpager inside a coordinator layout that uses the official google design support library, along with all the other goodies supported by this library (including an extremely easy to use Navigation side panel).
Why am I sharing this? Because the code is clean, uses an official latest library and is backward compatible. I found it very easy to use this as a base to my application and I am already getting good feedback on the overall implementation of the design which closely follows the material guidelines.
The example is written by a person from the official android team so it is also reliable that way!
Hope you find this useful (although not totally a direct answer to your question - but related nonetheless).
I am a new user who can't even post comments due to low rep so awarding the rep will be really helpful!
Thanks :)

Total beginner; need help properly setting up my Android 5.0 Toolbar

I do have a toolbar right now, but I don't really understand a lot of the things that I needed to add and copypaste from tutorials and other questions to get it to work. Also, my toolbar doesn't display an elevation even though I set android:elevation to 9dp.
Similar to the Google I/O app, I've set up a resource in /layout for my toolbar. I copied the name and named it "toolbar_actionbar_with_headerbar". Please excuse that it doesn't make any sense.
This is the content:
toolbar_actionbar_with_headerbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="128dp">
<Toolbar
android:id="#+id/toolbar"
android:elevation="9dp"
android:layout_height="128dp"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom" />
</LinearLayout>
Now, as it's apparently supposed to be done, I am applying my toolbar to activities by including them in their /layout XML-file. Works fine.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
[...]
<include layout="#layout/toolbar_actionbar_with_headerbar" />
[...]
</RelativeLayout>
However, this is my first question. When I did that initially, my toolbar was cut off at the sides like all other content of the activity, according to the margins set at the beginning of the xml. Right now, I simply deleted those, and add margins for every text field and other things on the activity. There probably is a better way?
The second question is: Why doesn't my elevation work? I've even tried setting it through code onCreate, but that doesn't work either. And lastly, what would I have to do to properly support devices pre-Level21?
Thanks a lot.
You should read this post: appcompat-v21
Take care when you copy some code text, there are some ” that are wrong, and you should change to ".
About elevation (you can read it in the post too), it only works with lollipop.

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

Android: Control with own parameters

I want to ask you, if is possible in Android to make a control, which will take my own parameters.
I want, that my control will looks like:
<my.own.control
android:style
attributes
>
<include name=”” value=””>
<include name=”” value=””>
</my.own.control>
or
<my.own.control
Android:style
attributes
>
<include>
<name></name>
<value></value>
</include>
<include>
<name></name>
<value></value>
</include>
</my.own.control>
The reason why I want to do it this way, is to dynamically edit includes.
The most important question is, how to read includes on initialize.
Or do you have another advice?
Unfortunately this is not possible.
There is an open bug on the android project for it that was reviewed and accepted by Romain-gui, one of the lead android developers at google.
http://code.google.com/p/android/issues/detail?id=14532
There is however a workaround, it is explained very well at http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/

Categories

Resources