<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
tools:context="com.karanvir.stepcounter.MainActivity">
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#android:color/holo_orange_light"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.433"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="65dp"
android:background="#color/colorPrimary"
app:srcCompat="#drawable/i"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="66dp"
app:layout_constraintTop_toBottomOf="#+id/value"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="65dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:background="#android:color/holo_orange_light"
android:text="How it works"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.karanvir.stepcounter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private TextView value;
private SensorManager sensorManager;
public static DecimalFormat DECIMAL_FORMATTER;
private SensorManager mSensorManager;
Button button22;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button22=(Button) findViewById(R.id.button);
button22.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity3();
}
});
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
//
Toast.makeText(this, "Success! There's a magnetometer.", Toast.LENGTH_SHORT).show();
}
else {
// Failure! No magnetometer.
openActivity2();
}
setContentView(R.layout.activity_main);
value = (TextView) findViewById(R.id.value);
// define decimal formatter
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
symbols.setDecimalSeparator('.');
DECIMAL_FORMATTER = new DecimalFormat("#.000", symbols);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// get values for each axes X,Y,Z
float magX = event.values[0];
float magY = event.values[1];
float magZ = event.values[2];
double magnitude = Math.sqrt((magX * magX) + (magY * magY) + (magZ * magZ));
// set value on the screen
value.setText(DECIMAL_FORMATTER.format(magnitude) + " \u00B5");
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void openActivity2(){
Intent intent=new Intent(this, Main2Activity.class);
startActivity(intent);
}
public void openActivity3(){
Intent intent=new Intent(this, Main3Activity.class);
startActivity(intent);
}
}
I have posted all the code for my project, this includes the xml and java code. im learning and just playing around with a project for fun. I have never ran into this error, I think it may have something to do with "public class MainActivity extends AppCompatActivity implements SensorEventListener" as this is my first time using something like this.
You are setting contentView 2 times.
setContentView(R.layout.activity_main);
Remove the later one.
You use setContentView(R.layout.activity_main) two times, Remove the second one.
Related
I am trying to use the pedometer sensor in a fragment to count the number of steps that has been taken since the device last rebooted.
I can get the pedometer working in an Activity but cannot seem to get it working in the fragment. I am new to using fragments so it's probably very simple what I am doing wrong.
My class is below:
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.application.placepicker.R.id.count;
public class Tab3 extends Fragment implements SensorEventListener {
private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab3, container, false);
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
count = (TextView) getActivity().findViewById(count); **error
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); **error
}
#Override
public void onResume() {
super.onResume();
activityRunning = true;
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Toast.makeText(this, "Count sensor not available!", Toast.LENGTH_LONG).show(); **error
}
}
#Override
public void onPause() {
super.onPause();
activityRunning = false;
}
#Override
public void onSensorChanged(SensorEvent event) {
if (activityRunning) {
count.setText(String.valueOf(event.values[0])); **error
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
My XML file to accompany this is:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="114dp"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:id="#+id/textLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Step counter"
android:textSize="40sp"
android:textColor="#color/colorAccent"
android:fontFamily="sans-serif"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:layout_gravity="center_horizontal"
android:textSize="26dp"
android:text="Steps walked so far today:"
android:textColor="#color/colorAccent" />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:text="---"
android:textColor="#color/colorAccent"
android:textSize="36dp" />
</LinearLayout>
You have no member variable for count
private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;
are examples of member variables in your fragment class
count = (TextView) getActivity().findViewById(count);
is setting a TextView to an unknown reference, please initialise count properly i.e.
TextView count = (TextView) getActivity().findViewById(count);
as a function member or:
private TextView count;
with the other class member variables
you will then need to call
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE)
as a fragment has no reference to the getSystemService method by itself, so it needs to go through it's holding activity
as well i know
you should change
count = (TextView) getActivity().findViewById(count);
to
count = (TextView)getView().findViewById(R.id.count);
cause you have to define view , don't call activity.
I tried to use:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1sp"
android:background="#000000"></FrameLayout>
but it just ended up in failure, as the constraints just went wonky. I've searched all over, but couldn't find a solution that works, and I'm on the verge of tearing my hair out. My code looks like this:
button_msg = (Button)findViewById(R.id.button_red);
button_msg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
greeting = "赤色です" ;
btn_send_msg.callOnClick();
}});
button_msg = (Button)findViewById(R.id.button_blue);
button_msg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
greeting = "青色です" ;
btn_send_msg.callOnClick();
}});
So if I press red, a border (black, etc) appears only around that button, and vice versa for other buttons. It's just to visually represent which state the program is in now.
Edit: the complete code is below
package app.real_time_chat;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private Button add_room;
private EditText room_name;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> list_of_rooms = new ArrayList<>();
private String name;
private DatabaseReference root =
FirebaseDatabase.getInstance().getReference().getRoot();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_room = (Button) findViewById(R.id.btn_add_room);
room_name = (EditText) findViewById(R.id.room_name_edittext);
listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,list_of_rooms);
listView.setAdapter(arrayAdapter);
request_user_name();
add_room.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
map.put(room_name.getText().toString(),"");
root.updateChildren(map);
}
});
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int
i, long l) {
Intent intent = new
Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("room_name",(
(TextView)view).getText().toString() );
intent.putExtra("user_name",name);
startActivity(intent);
}
});
}
private void request_user_name() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("名前を入れてください:");
final EditText input_field = new EditText(this);
builder.setView(input_field);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
name = input_field.getText().toString();
}
});
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
request_user_name();
}
});
builder.show();
}
}
XML File:
<?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:layout_width="match_parent"
android:background="#color/pale_blue"
android:layout_height="match_parent"
android:id="#+id/linearlayout">
<ImageView
android:layout_width="382dp"
android:layout_height="62dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/alumis_logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="-4dp"
app:layout_constraintHorizontal_bias="0.454" />
<Button
android:id="#+id/button_blue"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:background="#0000ff"
android:text="青色"
app:layout_constraintBottom_toTopOf="#+id/button_green"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_red"
app:layout_constraintVertical_bias="0.284"
app:layout_constraintVertical_chainStyle="packed"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
<Button
android:id="#+id/button_green"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:background="#00ff00"
android:text="緑色"
app:layout_constraintBottom_toTopOf="#+id/button_yellow"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_blue"
app:layout_constraintVertical_bias="0.407"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
<Button
android:id="#+id/button_yellow"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="40dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:text="黄色"
android:background="#ffff00"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.04"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
/>
<Button
android:layout_width="129dp"
android:layout_height="48dp"
android:text="Send"
android:id="#+id/btn_send"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toTopOf="#+id/msg_input"
app:layout_constraintLeft_toRightOf="#+id/msg_input"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.466"
android:layout_marginTop="-14dp" />
<EditText
android:layout_width="275dp"
android:layout_height="50dp"
android:id="#+id/msg_input"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="1dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="460dp" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="269dp"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="68dp"
app:layout_constraintBottom_toTopOf="#+id/msg_input"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<TextView
android:id="#+id/textView"
android:layout_width="394dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="49dp" />
</ScrollView>
<Button
android:id="#+id/button_red"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="60dp"
android:background="#ff0000"
android:text="赤色"
app:layout_constraintBottom_toTopOf="#+id/button_blue"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>
create button_border.xml file inside res/drawable folder
button_border.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#android:color/transparent" >
</solid>
<stroke
android:width="2dp"
android:color="#F06292" >
</stroke>
<corners
android:radius="2dp" >
</corners>
</shape>
MainActivity.java
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setBackground(getResources().getDrawable(R.drawable.button_border));
}
});
Here is the code I'm using to read the heart rate from a 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. The text boxes are not being displayed on screen, instead I just receive a blank screen. How to add mTextViewHeart to the screen programmatically ? Or is required that I add it 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 with Pavneet Singh answer but blank screen displayed.
Maybe should configure all UI via XML ?
Update2 :
Updating to use linearlayout does not have an 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 an edittext field also does 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 the issue be something 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 to another 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>
i am having problem in text to speech. have this code in java class
package com.techblogon.loginexample;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class listenActivity extends Activity implements OnClickListener, OnInitListener {
private TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
tts = new TextToSpeech(this, this);
findViewById(R.id.button1).setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (tts!=null) {
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();
if (text!=null) {
if (!tts.isSpeaking()) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
#Override
public void onInit(int code) {
if (code==TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.getDefault());
} else {
tts = null;
Toast.makeText(this, "Failed to initialize TTS engine.", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onDestroy() {
if (tts!=null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
and xml file has this code
<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=".listenActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TTS Demonstration" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="Say It" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:ems="10"
android:text="Hello world!" />
</RelativeLayout>
but issue is when i click button no sound heard on my mobile. does i need any permission for that
any help
Many thanks
I have just started learning android. I have develop a program named Android Light Sensor that measure the Intensity of Light. Here is my code:
package com.AndroidLightSensor;
import com.example.andriodlightsensor.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class AndriodLightSensorActivity extends Activity {
ProgressBar lightMeter;
TextView textMax, textReading;
float counter;
Button read;
TextView display;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
counter = 0;
read = (Button) findViewById(R.id.bStart);
display = (TextView) findViewById(R.id.tvDisplay);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lightMeter = (ProgressBar)findViewById(R.id.lightmeter);
textMax = (TextView)findViewById(R.id.max);
textReading = (TextView)findViewById(R.id.reading);
SensorManager sensorManager
= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor lightSensor
= sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor == null){
Toast.makeText(AndriodLightSensorActivity.this,
"No Light Sensor! quit-",
Toast.LENGTH_LONG).show();
}else{
float max = lightSensor.getMaximumRange();
lightMeter.setMax((int)max);
textMax.setText("Max Reading(Lux): " + String.valueOf(max));
sensorManager.registerListener(lightSensorEventListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
SensorEventListener lightSensorEventListener
= new SensorEventListener(){
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_LIGHT){
final float currentReading = event.values[0];
lightMeter.setProgress((int)currentReading);
textReading.setText("Current Reading(Lux): " + String.valueOf(currentReading));
read.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
display.setText("" + String.valueOf(currentReading));
}
});
}
}
};
}
Also The xml is:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDisplay"
/>
<ProgressBar
android:id="#+id/lightmeter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="80dp"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0"
/>
<TextView
android:id="#+id/max"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/reading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Start"
android:layout_y="249dp"
android:textSize="30dp"
android:onClick="onButtonDown"
android:id="#+id/bStart"
></Button>
</LinearLayout>
I want to get the current value whenever I press the button; i:e the value should not keep changing (Like in Stop Watch, but the updated should replace the previous one). In Eclipse, It shows no error, but when I run on my device is says" Unfortunately, Android Light Sensor has stopped."
Please help!!
There are an error that is obvious.
If findViewById is used before setContentView(R.layout.main); the values returned are null.
When you try to use them you get an error.
Put this two lines after setContentView(R.layout.main);
read = (Button) findViewById(R.id.bStart);
display = (TextView) findViewById(R.id.tvDisplay);