Layout will not match_parent - Android Q&A, Help & Troubleshooting

I don't understand why these forums force me to post software development related topics in other forums because I don't have 10 posts yet....no logic in it.
Anyways...
I am trying to add a layout to my main layout at runtime.
The child layout being added is specified in an xml file as follows. My problem is that the layout is not matching the parent in height or width, it is just wrapping content. I don't understand why the match_parent values are being ignored.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="FORUM WILL NOT LET ME POST URL"
android:id="@+id/midLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1"
android:background="@color/white" >
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
Java
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root=(RelativeLayout)findViewById(R.id.main_layout);
root.addView((View) LayoutInflater.from(this).inflate(R.layout.mid_layout,null));
Can anyone provide any clarification as to what is going wrong here?

Anyone?

Related

[Q][HELP] Eclipse issue in main.xml

I am getting an annoying error when creating the GUI for my app. It is an extremely simple GUI with only two text boxes and one button. I created it using Android's SDK for Eclipse in the "Graphical layout", which converts it to the main.xml. However, I keep getting the following error:
"Failed to convert 'dberm22 app' into a drawable" <--dberm22 app is the (fake) name of my app.
The detailed log gives this:
Code:
!ENTRY com.android.ide.eclipse.adt 4 0 2011-09-27 21:54:46.255
!MESSAGE main.xml: Failed to convert 'dberm22 app' into a drawable
!STACK 0
java.lang.NumberFormatException: Color value 'dberm22 app' must start with #
at com.android.layoutlib.bridge.impl.ResourceHelper.getColor(ResourceHelper.java:71)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:248)
at com.android.layoutlib.bridge.android.BridgeTypedArray.getDrawable(BridgeTypedArray.java:731)
at android.view.View.<init>(View.java:1951)
at android.widget.TextView.<init>(TextView.java:350)
at android.widget.TextView.<init>(TextView.java:343)
at sun.reflect.GeneratedConstructorAccessor15.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
at com.android.layoutlib.bridge.android.BridgeInflater.onCreateView(BridgeInflater.java:84)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:129)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:279)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:318)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:380)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1310)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1075)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:901)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.pageChange(LayoutEditor.java:403)
at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:290)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2745)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1432)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
My strings.xml and main.xml should be fine:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView android:text="@string/Welcome" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@string/app_name" android:gravity="center" android:layout_weight="0.46"></TextView>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:text="@string/Authorize" android:layout_height="wrap_content" android:layout_weight="0.20" android:onClick="callOAuth" android:clickable="true"></Button>
<TextView android:text="@string/Note" android:layout_height="wrap_content" android:id="@+id/textView3" android:gravity="center" android:layout_width="wrap_content" android:layout_weight="0.25"></TextView>
</LinearLayout>
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">dberm22 app</string>
<string name="Note">Note: This app uses OAuth to authenticate, so all your information is completely anonymous to me.</string>
<string name="Welcome">Welcome to yadda yadda yadda.</string>
<string name="Authorize">Authorize App</string>
<string name="callOAuth">callOAuth</string>
</resources>
What can be the issue? Its driving me nuts!
Thanks in advance,
dberm22
dberm22 said:
I am getting an annoying error when creating the GUI for my app. It is an extremely simple GUI with only two text boxes and one button. I created it using Android's SDK for Eclipse in the "Graphical layout", which converts it to the main.xml. However, I keep getting the following error:
"Failed to convert 'dberm22 app' into a drawable" <--dberm22 app is the (fake) name of my app.
The detailed log gives this:
<SNIP>
My strings.xml and main.xml should be fine:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:text="@string/Welcome"
android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="match_parent"
[COLOR="Red"]android:background="@string/app_name"[/COLOR]
android:gravity="center"
android:layout_weight="0.46">
</TextView>
<Button android:id="@+id/button1" android:layout_width="match_parent" android:text="@string/Authorize" android:layout_height="wrap_content" android:layout_weight="0.20" android:onClick="callOAuth" android:clickable="true"></Button>
<TextView android:text="@string/Note" android:layout_height="wrap_content" android:id="@+id/textView3" android:gravity="center" android:layout_width="wrap_content" android:layout_weight="0.25"></TextView>
</LinearLayout>
What can be the issue? Its driving me nuts!
Thanks in advance,
dberm22
Click to expand...
Click to collapse
I think it's expectig a color ref hex like #ff0000 (red) for the background color, not a string.
android:background="@string/app_name"
You're trying to use a string as a background.
Gene Poole said:
I think it's expectig a color ref hex like #ff0000 (red) for the background color, not a string.
Click to expand...
Click to collapse
Wow...I looked through my code 1000 times, and completely missed that. Thanks so much!
[SOLVED]
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A

[Q] Help needed for my Android App from my uni football team.

Hey Good People,
Basically I have a Radio Group with two radio buttons, one of them is labelled RUN and the other is labelled PASS.
Just underneath this I also have a check-box labelled "Pass Complete"
Question: How do I disable the check-box when RUN radio button is selected (so it can't be selected) and enable it while the PASS radio button is selected?
Any help constructing some type of IF statement
XML
<RadioGroup
android:id="@+id/runandpass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidrientation="horizontal">
<RadioButton
android:id="@+id/radiobuttonrun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RUN" android:layout_weight="50"/>
<RadioButton
android:id="@+id/radiobuttonpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PASS"
android:layout_weight="50"/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidrientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:text="" />
<CheckBox
android:id="@+id/checkBoxcmpltpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pass Complete"
android:layout_weight="6"/>
</LinearLayout>
JAVA
package com.aces.acesfootballuk;
import android.app.Activity;
import android.os.Bundle;
public class CoachesPage extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.coachespage);
}
};

Question regarding ImageView cropping / sizing and alignment in app widgets

Hi developers,
I have a special problem and didn't find a solution for it yet. So I hope that anybody here had a similar issue and can point me to a solution.
The problem:
Basically I use an ImageView inside my widgets. When the image is smaller than the ImageViews space then there is no problem.
The problem raises when the image gets bigger than the ImageView.
In some cases I want that the image doesn't get scaled but cropped and then aligned due to the users option.
What I want to achive is that the ImageView gets as big as the image and then is aligned (top, right, bottom, left). The rest of the image should get clipped by the container.
All I could achive until now is that the image gets cropped and centerd (all done by the ImageView).
Here is the xml I use for the widgets:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widgetbackground"
android:focusable="true" >
<!-- [...] -->
<LinearLayout
android:id="@+id/llTextBlock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/llText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/llContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- [...] -->
<ImageView
android:id="@+id/imgContentDontScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/widgetbackground"
android:focusable="true"
android:scaleType="center"
android:visibility="gone" >
</ImageView>
</LinearLayout>
<!-- [...] -->
</LinearLayout>
</LinearLayout>
<!-- [...] -->
</RelativeLayout>
The ImageView in question is the "imgContentDontScale" one.
The solution has to work on widgets (so almost only xml defined excluding the "RemotableViewMethod" marked methods) and ist has to be Froyo compatible.
Thanks
Michael

[SOLVED][Debug Help] Empty/White Activity (invisible UI controls)

Hello! I generated a very simple 2 activities app using Google's Android Studio, and the target is an Android 8.1 phone. The problem is that the main activity doesn't show the edit box and buttons (that are obviously visible in the Design page). If I start my login activity (in the onCreate() function of the main activity), the login activity UI is visible and the main activity UI is still not visible. If I single step debug the app, the main activity UI seems present (as no error is encountered). They are just invisible. I compared the XML of both activities, and couldn't find a different setting that would fix this problem.
PS: I could't find a solution in Google search either.
PS: I added a space to break the URL links below.
AndroidManifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http: //schemas.android.com/apk/res/android"
package="com.example.test2">
<application
android:allowBackup="true"
android:allowTaskReparenting="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="false"
android:theme="@style/AppTheme">
<activity
android:alwaysRetainTaskState="false"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:alwaysRetainTaskState="false"
android:label="@string/app_name"
android:name=".LoginActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.test2.MainActivity" />
</activity>
</application>
</manifest>
activity_main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<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/containerMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:visibility="visible"
tools:context=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="46dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/textName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/TextNameHint"
android:visibility="visible" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:enabled="true"
android:focusable="true"
android:longClickable="false"
android:onClick="buttonClick"
android:text="@string/Button"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
Code:
package com.example.test2;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Thanks in advance!
[SOLVED] [Debug Help] Empty/White Activity (invisible UI controls)
Even though the design view seemed fine to me, including constraints and all, something was off to make the UI controls invisible. To try to isolate the problen, I decided to delete the controls one by one, and then I started to see something.
So, I made a different UI design. :cyclops:

Scan + Map facilitating in Shipping and Logistics Services

You are probably a shipping and logistics company and your services require you to manage the flow of product delivery. You are trying your best to make sure that the products you handle, are transported from the purpose of origin to destination safely. You are trying to find ways to reinforce your services to cater to your customers better. QR Codes are the need of time as QR Codes store information which is easily scan-able with a smartphone and don't need you to put in any cost-intensive set-QR Codes store information. In fact, a QR Code can store up to 7,089 numeric or 2,953 alphanumeric characters.
Being a Shipping & Logistics Service, you need to keep an eye on goods at various stages during transit for which you do not need to regularly call the fellow operators or track the goods information and delivery status. QR Codes help both shipping and logistics do it easily. Once you place them on the packaging, operators can scan them at various stages of the transit from the warehouse to the delivery destination.
With Scan & Map Kit in your Business App, you can easily keep a track of the delivery packages. From scanning to get information, delivery status, delivery destination, and the directions to guide the Deliveryman to the destination.
Huawei Scan Kit:
HUAWEI Scan Kit scans and parses all major 1D and 2D barcodes and generates QR codes, helping you quickly build barcode scanning functions into your apps. Scan Kit automatically detects, magnifies, and recognizes barcodes from a distance, and is also able to scan a very small barcode in the same way.
Huawei Map Kit:
Huawei Map kit allows can easily integrate map-based functions into your apps and make location-based services work better for you.
Pre-Requisites
1. Integrate HMS Core in project
2. Enable Scan and Map Kit from AGC Console
3. Add agconnet-service.json file in the app level directory
1. Add Dependencies & Permission:
1.1: Add the following dependencies in the app level build.gradle file:
Code:
dependencies {
//Map
implementation 'com.huawei.hms:maps:5.0.3.302'
//Scan
implementation 'com.huawei.hms:scan:1.2.2.300'
}
1.2: Add the following permissions in the AndroidManifest.xml:
Code:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--Camera permission-->
<uses-permission android:name="android.permission.CAMERA" />
<!--File reading permission-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
2. Add Layout Files:
2.1: Add the activity_main.xml layout file in the layout folder of the res. This is the layout view of the Main Activity of the in the application, contains a Recyclerview to display the arraylist.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_parcels"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/_5sdp"
android:stackFromBottom="true" />
</RelativeLayout>
2.2: Add the activity_details.xml layout file in the layout folder of the res. This is the layout view of the Details Activity of the in the application, which contains the Textviews and a Mapview.
Code:
<?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:layout_gravity="center"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/parcel_details"
android:textStyle="bold"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:gravity="center"
android:textColor="@color/teal_200"
android:textSize="@dimen/_14sdp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/parcelid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="@dimen/_14sdp"/>
<TextView
android:id="@+id/parcelname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="@dimen/_14sdp"/>
<TextView
android:id="@+id/parcelcontact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="@dimen/_14sdp"/>
<TextView
android:id="@+id/parcellocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="@dimen/_14sdp"/>
<TextView
android:id="@+id/parcelstatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/_14sdp"
android:layout_marginBottom="@dimen/_20sdp"/>
<com.huawei.hms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="51"
map:cameraTargetLng="10"
map:cameraZoom="8.5"
map:mapType="normal"
map:uiCompass="true"
map:uiZoomControls="true" />
</LinearLayout>
</LinearLayout>
2.3: Add the activity_scan.xml layout file in the layout folder of the res. This is the layout view of the Details Activity of the in the application, which contains the Surfaceview for the camera preview and an ImageView for a customize scanning rectangular area.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- customize view for camera preview -->
<FrameLayout
android:id="@+id/rim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<!-- customize scanning rectangle -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="#FF000000"
android:alpha="0.1" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:background="@drawable/scanningframe" />
</RelativeLayout>
</RelativeLayout>
2.4: Add the cardview_parcel.xml layout file in the layout folder of the res. This is the layout view for the items of Recyclerview in the Main Activity of the application, which contains the Cardview to display Textviews & Imageviews.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/tools"
android:padding="@dimen/_3sdp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:background="@color/white"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearclick_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:id="@+id/serial_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1.5"
android:padding="@dimen/_5sdp"
android:background="@color/purple_200"
android:textColor="@color/black"
android:textSize="@dimen/_11sdp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7.5"
android:padding="@dimen/_5sdp"
android:gravity="center"
android:id="@+id/linearmid"
android:orientation="vertical"
android:weightSum="10">
<TextView
android:id="@+id/parcelid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="@color/black" />
<TextView
android:id="@+id/parcelname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<TextView
android:id="@+id/parcelcontact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<TextView
android:id="@+id/parcellocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black" />
<TextView
android:id="@+id/parcelstatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<ImageView
android:layout_marginEnd="@dimen/_10sdp"
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:scaleType="centerCrop"
android:id="@+id/cameraBtn"
android:visibility="invisible"
android:background="@drawable/ic_camera"/>
<ImageView
android:layout_width="@dimen/_20sdp"
android:layout_height="@dimen/_20sdp"
android:layout_toEndOf="@id/cameraBtn"
android:scaleType="centerCrop"
android:id="@+id/locationBtn"
android:background="@drawable/ic_location"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
3. Add Classes
3.1: Add the MainActivity.java file in the App. This class extends AppCompatActivity and implements the Callback Interface of Parcel Adapter to handle views inside the adapter.
Code:
public class MainActivity extends AppCompatActivity implements ParcelsAdapter.CallbackInterface {
List<ParcelsModal> parcelsModalList;
RecyclerView mRecyclerView;
ParcelsAdapter parcelsAdapter;
public static final int SCAN_START_CODE = 001;
private static final int SCAN_CODE = 002;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parcelsModalList = new ArrayList<ParcelsModal>();
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_parcels);
mRecyclerView.setLayoutManager(layoutManager);
// Add Data to list
parcelsModalList.add(new ParcelsModal("00001", "John Atkinson", "123456789", "40.716124, -74.001884","Delivered"));
parcelsModalList.add(new ParcelsModal("00002", "Mark Bennett", "123456789", "40.712188, -73.997378","Delivered"));
parcelsModalList.add(new ParcelsModal("00003", "Jack Brooks", "123456789", "40.717588, -74.010403","Not Delivered"));
parcelsModalList.add(new ParcelsModal("00004", "Alice John", "123456789", "40.715188, -73.995078","Not Delivered"));
parcelsModalList.add(new ParcelsModal("00005", "Julie Berk", "123456789", "40.713588, -74.013303","Not Delivered"));
parcelsAdapter = new ParcelsAdapter(MainActivity.this, (ArrayList<ParcelsModal>) parcelsModalList);
mRecyclerView.setAdapter(parcelsAdapter);
parcelsAdapter.notifyDataSetChanged();
}
//To Handle Callback
@Override
public void onHandleSelection(String text) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
SCAN_START_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (permissions == null || grantResults == null || grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
return;
}
if (requestCode == SCAN_START_CODE) {
//start your activity for scanning barcode
Intent newIntent = new Intent(this, ScanActivity.class);
this.startActivityForResult(newIntent, SCAN_CODE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//receive result after your activity finished scanning
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK || data == null) {
return;
}
//Get Code from QR
if (requestCode == SCAN_CODE) {
HmsScan hmsScan = data.getParcelableExtra(ScanActivity.SCAN_RESULT);
if (hmsScan != null && !TextUtils.isEmpty(hmsScan.getOriginalValue())) {
processData(hmsScan.getOriginalValue());
}
}
}
//To Handle Adapter callback data in Activity
private void processData(String value){
for (ParcelsModal modal : parcelsAdapter.getDataAdapterList()) {
//Update date at your Server Here
if(modal.getParcel_id().equals(value)){
modal.setParcel_dilvery_status("Delivered");
Toast.makeText(this, "Parcel Delivered", Toast.LENGTH_SHORT).show();
break; // No need to run the remaining loop
}
}
parcelsAdapter.notifyDataSetChanged();
}
}
How many types of barcode can Huawei Scan kit generates ?

Categories

Resources