android - Add text box to wearable screen - beginner -
here code i'm using read heart rate wearable :
import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.bundle; import android.support.wearable.activity.wearableactivity; import android.util.log; import android.widget.textview; import android.hardware.*; import android.support.wearable.view.boxinsetlayout.*; public class mainactivity extends wearableactivity implements sensoreventlistener { private static final string tag = "mainactivity"; private textview mtextviewheart; sensormanager msensormanager; sensor mheartratesensor; sensoreventlistener sensoreventlistener; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_activity); mtextviewheart = new textview(this); mtextviewheart.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); mtextviewheart.settext("heart rate "); msensormanager = ((sensormanager) getsystemservice(sensor_service)); mheartratesensor = msensormanager.getdefaultsensor(sensor.type_heart_rate); // msensormanager.registerlistener(this, mheartratesensor, sensormanager.sensor_delay_normal); log.i(tag, "listener registered."); mtextviewheart.settext("something here"); msensormanager.registerlistener(this, mheartratesensor, msensormanager.sensor_delay_fastest); // msensormanager.registerlistener(sensoreventlistener, mheartratesensor, msensormanager.sensor_delay_fastest); } public void onresume(){ super.onresume(); } public void onaccuracychanged(sensor sensor, int accuracy) { log.d(tag, "onaccuracychanged - accuracy: " + accuracy); } public void onsensorchanged(sensorevent event) { if (event.sensor.gettype() == sensor.type_heart_rate) { string msg = "" + (int)event.values[0]; mtextviewheart.settext(msg); log.d(tag, msg); } else log.d(tag, "unknown sensor type"); } }
main_activity.xml :
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <android.support.wearable.view.gridviewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.wearable.view.dotspageindicator android:id="@+id/page_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="10dp" android:padding="5dp" android:background="@drawable/rounded_background"> </android.support.wearable.view.dotspageindicator> </relativelayout>
the sensor functionality seems fine. text boxes not being displayed on screen, instead receive blank screen. how add mtextviewheart
screen programmatically ? or required add via main_activity.xml
?
update :
import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.bundle; import android.support.wearable.activity.wearableactivity; import android.util.log; import android.widget.relativelayout; import android.widget.textview; import android.hardware.*; import android.support.wearable.view.boxinsetlayout.*; public class mainactivity extends wearableactivity implements sensoreventlistener { private static final string tag = "mainactivity"; private textview mtextviewheart; sensormanager msensormanager; sensor mheartratesensor; sensoreventlistener sensoreventlistener; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_activity); relativelayout relativelayout = (relativelayout )findviewbyid(r.id.parent); mtextviewheart = new textview(this); mtextviewheart.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); mtextviewheart.settext("heart rate "); msensormanager = ((sensormanager) getsystemservice(sensor_service)); mheartratesensor = msensormanager.getdefaultsensor(sensor.type_heart_rate); log.i(tag, "listener registered."); mtextviewheart.settext("something here"); msensormanager.registerlistener(this, mheartratesensor, msensormanager.sensor_delay_fastest); relativelayout.addview(mtextviewheart); } public void onresume(){ super.onresume(); } public void onaccuracychanged(sensor sensor, int accuracy) { log.d(tag, "onaccuracychanged - accuracy: " + accuracy); } public void onsensorchanged(sensorevent event) { if (event.sensor.gettype() == sensor.type_heart_rate) { string msg = "" + (int)event.values[0]; mtextviewheart.settext(msg); log.d(tag, msg); } else log.d(tag, "unknown sensor type"); } }
main_activity.xml :
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <android.support.wearable.view.gridviewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.wearable.view.dotspageindicator android:id="@+id/page_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="10dp" android:padding="5dp" android:background="@drawable/rounded_background"> </android.support.wearable.view.dotspageindicator> </relativelayout>
i've updated pavneet singh answer blank screen displayed.
maybe should configure ui via xml ?
update2 :
updating use linearlayout not have impact :
<?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:paddingleft="16dp" android:paddingright="16dp" android:orientation="vertical" > <android.support.wearable.view.gridviewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.wearable.view.dotspageindicator android:id="@+id/page_indicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="10dp" android:padding="5dp" android:background="@drawable/rounded_background"> </android.support.wearable.view.dotspageindicator> </linearlayout> package com.example.android.wearable.datalayer; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.bundle; import android.support.wearable.activity.wearableactivity; import android.util.log; import android.widget.linearlayout; import android.widget.relativelayout; import android.widget.textview; import android.hardware.*; import android.support.wearable.view.boxinsetlayout.*; public class mainactivity extends wearableactivity implements sensoreventlistener { private static final string tag = "mainactivity"; private textview mtextviewheart; sensormanager msensormanager; sensor mheartratesensor; sensoreventlistener sensoreventlistener; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_activity); linearlayout relativelayout = (linearlayout )findviewbyid(r.id.parent); mtextviewheart = new textview(this); mtextviewheart.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); mtextviewheart.settext("heart rate "); msensormanager = ((sensormanager) getsystemservice(sensor_service)); mheartratesensor = msensormanager.getdefaultsensor(sensor.type_heart_rate); log.i(tag, "listener registered."); mtextviewheart.settext("something here"); msensormanager.registerlistener(this, mheartratesensor, msensormanager.sensor_delay_fastest); relativelayout.addview(mtextviewheart); } public void onresume(){ super.onresume(); } public void onaccuracychanged(sensor sensor, int accuracy) { log.d(tag, "onaccuracychanged - accuracy: " + accuracy); } public void onsensorchanged(sensorevent event) { if (event.sensor.gettype() == sensor.type_heart_rate) { string msg = "" + (int)event.values[0]; mtextviewheart.settext(msg); log.d(tag, msg); } else log.d(tag, "unknown sensor type"); } }
adding edittext field not display :
<edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="121dp" android:ems="10" android:text="test" />
could issue fundamental ?
this screen layout displays :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="click me screen" /> <textview android:id="@+id/textview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="i'm screen 1 (main.xml)" android:textappearance="?android:attr/textappearancelarge" /> </linearlayout>
Comments
Post a Comment