Optimize code for Android with multiple layouts - java

I am creating an Android Application having two layouts. All layouts have some common features like the top part is same and the only difference is at the bottom part.
Currently I have created two separate java files for two layouts. And I have simply copied the code which implements similar functionality like buttons etc
The part seen is the pic is the top part of the layout which is same in both the layouts;
Is there any way with which I can optimize the functionality by having reusable code

In my opinion this is helpful using <include> in your main layout.
STEP:
you have to create one XML file for your common layout.
topbar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="#color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
like this way now you have to include this XML file to where you have to require.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<include layout="#layout/topbar"/>
...
</LinearLayout>

I think this tab bar can also be used for this..
below is the link for complete tutorial...
http://mindstick.com/Articles/7e659092-3046-4461-97ba-b2b28616241e/?tab%20layout%20android%20application

Related

warp a view in android

i want to warp views in android ,
for example need to create wave on top of screen and any time can convert it to straight line
or
use a button in bottom navigation bar which create a wave on bottom navigation
please visit screenshots:
This is new Design as per material IO. It is introduced in material design 2. You need to add anchor for using that type of view.
There are some changes which need to be noted.
This is AppBar which is placed at bottom not BottomNavigatioView.
It can be used for multi-purpose, so chose your use wisely.
You can modify design as your need.
Following code can be used to achieve that functionality.
<android.support.design.widget.CoordinatorLayout
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">
<!-- Other components and views -->
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="#drawable/ic_menu_24"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bar"/>
</android.support.design.widget.CoordinatorLayout>
The FloatingActionButton can be anchored to the BottomAppBar by setting app:layout_anchor or by calling CoordinatorLayout.LayoutParams#setAnchorId(int).
Note: This things are introduced in Android P versions, you need to
have Android studio 3.2 or above.
Checkout this link for detailed explanation :
Implementing BottomAppBar: Material Components for Android

Google Play Store Cardview

I have been trying for a long time to make Cards similar to appearance that of Google Play Store, ie cards of Different sizes , with images and all inside the card. Any tips on how to proceed.
Basically you just create a layout (RelativeLayout, LinearLayout, etc) as a child of your cardview element and make it look however you want.
For example...
<android.support.v7.widget.CardView
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardElevation="2dp"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="2dp">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent">
//Your desired layout here
</RelativeLayout>
</android.support.v7.widget.CardView>
You can take a look at the MaterialList implementations for some good examples.

using the <include/> in android programming

I am making an app, and I have the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="325dp" />
<Button
android:id="#+id/add_claim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Claim" />
</LinearLayout>
I made a button called View_list that wants to see only the expandable list view on the entire page. what I did is create a new XML file, and added the follwing include statement:
<include android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
However, I realized that this will now work for two reasons
1 - There is no layout ID
2- I only want the list, not the button and everything else I am going to put in the original layout. What is going to happen is if I add something to the list, I need to be able to view it without the option of adding antthing to it.
My question is therefore this:
How do I find the layout ID and how can I prevent the entire layout from showing, and only show the list. I would really appreciate some advice.
The include should say:
<include layout="#layout/View_list"
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
This includes the View_list.xml layout in another layout.
But I dont really understand why you would use an include here if you don't want to re-use the whole layout, but only the list part of it. includes exist to easily use layouts in other layouts, if you need just a ListView, use just a ListView.
EDIT: I'm not sure anymore if I got your question right, but I'll not delete the answer because you commented on it.
If you want to hide the button or other Views on the click of a button, you can set their visibility to gone programmatically.

TwoLineListItem Source Code

Since TwoLineListItem has been deprecated as of API 17, I have taken steps to replace it with custom XML and ViewHolder. However, I would really like for my app to look exactly as it had while using TwoLineListItem (since it is a big part of the application). Thus, I would like to know if it's possible to find the XML source code for the most recent TwoLineListItem that Android was using (all the ones I've found have been outdated), or do I have to basically go with trial and error?
I'm not sure what you're searching for. The TwoLineListItem is a simple widget which uses a layout that you could easily look at if you search the layout folder in the SDK/platforms/android-x/data/res/layout. Here is the one for Jelly Bean:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#android:id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#android:id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The widget's code is very simple to replicate.

Is it possible to include a MapView as an element of another activity?

Just as the title says...I want to include a MapView inside of another activity. For instance, I want the MapView to take up half of the screen, and below that, include some other widgets - text fields, buttons, whatever. I have been tinkering with it, but so far I have been unsuccessful - the map does take up only half the screen, but no widgets show up.
Any clues? Is this possible, or is the Google Map integration strictly using the map as a full screen?
Please post your xml layout!
You are just doing it wrong. :-D
A mapview will work like any other widget. I strongly believe there is a mistake in your layout.
This one will work for instance!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/h1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:text="Satellite"
android:id="#+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<ImageView
android:id="#+id/image"
android:layout_width="75dip"
android:layout_height="75dip"
android:layout_marginRight="6dip"
android:src="#drawable/uoicon" />
</LinearLayout>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0zDdYFYf6Ir2W-NuiHPLAoFjsq0nmqRhPfzjY3A"/>
</LinearLayout>
Just a guess but you're using a LinearLayout to contain the MapView and other widgets but you forgot to set the orientation to vertical? The default for LinearLayout is horizontal.
It came to mind because it's something I've done 2 or 3 times myself. Totally confusing as effectively the other widgets 'disappear' off the right-hand side of the screen. It eventually got engrained in my mind and I always make sure to explicitly specify orientation now when I use LinearLayout.

Categories

Resources