I'm trying to have a profile page kinda of concept for one of my activities. I'm curious if this is the correct form to save the entered info and i would like it to stat whatever text they entered until next time they want to edit it ..
public class MainPage extends Activity implements OnClickListener,
OnItemSelectedListener {
TabHost th;
TextView Uotw, Aotw, Ds, Hmos, Eds, Cl;
Button go, Submit;
EditText Name, Email, Phone, Info;
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
Name = (EditText) findViewById(R.id.etNameOfBusiness);
Email = (EditText) findViewById(R.id.etEmail);
Phone = (EditText) findViewById(R.id.etPhone);
Info = (EditText) findViewById(R.id.etAdditionalInfo);
Submit.setOnClickListener(new OnClickListener(){
@override
public void onClick(View v) {
// TODO Auto-generated method stub
{
Log.v("EditText", Name.getText().toString());
Log.v("EditText", Email.getText().toString());
Log.v("EditText", Phone.getText().toString());
Log.v("EditText", Info.getText().toString());
}
i didn't post the whole activity because to much code thats irrelevant to the question
WaitTobi said:
I'm trying to have a profile page kinda of concept for one of my activities. I'm curious if this is the correct form to save the entered info and i would like it to stat whatever text they entered until next time they want to edit it ..
public class MainPage extends Activity implements OnClickListener,
OnItemSelectedListener {
TabHost th;
TextView Uotw, Aotw, Ds, Hmos, Eds, Cl;
Button go, Submit;
EditText Name, Email, Phone, Info;
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
Name = (EditText) findViewById(R.id.etNameOfBusiness);
Email = (EditText) findViewById(R.id.etEmail);
Phone = (EditText) findViewById(R.id.etPhone);
Info = (EditText) findViewById(R.id.etAdditionalInfo);
Submit.setOnClickListener(new OnClickListener(){
@override
public void onClick(View v) {
// TODO Auto-generated method stub
{
Log.v("EditText", Name.getText().toString());
Log.v("EditText", Email.getText().toString());
Log.v("EditText", Phone.getText().toString());
Log.v("EditText", Info.getText().toString());
}
i didn't post the whole activity because to much code thats irrelevant to the question
Click to expand...
Click to collapse
Omarkoopa said:
Click to expand...
Click to collapse
sorry maybe i was just rambling and not getting my real question across what I'm trying to do is have my edittexts to b able to save the info thats entered and stays there until a user would like to change it ..kind of like a profile pages you'd enter info and have it permanently save and can be viewed by other users .... i hope this make more sense
Related
I like to start a listener; e.g. accelerometer sensor and have it stop after like 20 seconds or so. How can I force it to stop after criteria is reached (e.g 20 seconds is passed)
Here is the base code of what I have:
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...
//...
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
}
}
private void getAccelerometer(SensorEvent event) {
float[] values = event.values.clone();
float x = values[0];
float y = values[1];
float z = values[2];
//...
}
@override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@override
protected void onResume() {
super.onResume();
// register this class as a listener for the accelerometer sensor
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@override
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
All help is appreciated.
Thanks
a fragment has two views a FrameLayout contains some children views ,below the FrameLayout there has a viewPager . I want to replace the fragment when there has an action like ACTION_MOVE on FrameLayout ,so I add a onTouchListener on FrameLayout but it never work,viewPager works well,and also the FrameLayout's children views has onClick event
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.hall_gift, container,
false);
frameLayout = (FrameLayout)view.findViewById(R.id.fans_body_title);
frameLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
Log.i(TAG,"ACTION DOWN");
}
return false;
}
});
return view;
}
Click to expand...
Click to collapse
Hi folks,
well ... first of all: I'm completely new to programming related to Android. I do have basic knowledge in programming with Basic and C. But im all new to Java and programming with the Android ecosystem (I am familiar with the Android ecosystem, though ... I've just never written code for Android).
Now I wanted a simple app to read out and display some sensor data, such as acceleration along y-axis and rotation angle around y-axis.
But I found the whole way you get stuff out of the sensors very confusing ans somehow unnecessarily complicated. However....
I've successfully managed to code an app, that checks whether certain sensors are available or not - and show an according text in a TextView for the user to read.
The next step was, I wanted to code an app that reads the rotation angle and shows the current angle in a TextView for the user. But I haven't managed that.
I found the whole System of reading the rotation data very complex..... why isn't there just a simple function like "get the y-axis rotation angle" and thats it .... I've searched a lot in the Interwebz, tried to understand how that stuff works, and used some code of some examples i thought i understood ... well, I obviously didn't ....
This is what I've got so far:
Code:
package test.sensor4;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//float A[] = new float[9];
//float I[] = new float[9];
float[] mGravity;
float[] mGeomagnetic;
float azimut;
float pitch;
float roll;
Button btn1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
//SensorManager.registerListener(this)
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float A[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(A, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(A, orientation);
azimut = orientation[0]; // orientation contains: azimut, pitch and roll
pitch = orientation[1];
roll = orientation[2];
TextView tv1 = (TextView)findViewById(R.id.text1);
tv1.setText(String.valueOf(+roll));
}
}
}
@Override
public void onClick(View e) {
TextView tv2 = (TextView)findViewById(R.id.text2);
tv2.setText(String.valueOf(+pitch));
}
}
what this does is: it doesn't change the text from the first textview at all ... in the second textview it prints "0.0"
I don't understand why .... Maybe you guys can help me.
Thanks in advance.
Best wishes.
public class MainActivity extends Activity {
MyView myView;
Button mButtonAdd;
@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = new MyView(this);
myView.setBackgroundColor(Color.WHITE);
setContentView(R.layout.activity_main);
mButtonAdd = findViewById(R.id.buttonAdd);
mButtonAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do something here
setContentView(myView);
}
});
}
public class MyView extends View {
...
}
}
I have a button, when clicked it should show a view. However, the button seems to disappear 'under' the view shown on click of the button. How to maintain the button fixed, also able to add a view multiple times?
Im following the steps laid out here for using the Settings Activity Template and despite some changes (e.g. now a root_preferences.xml file gets created instead of multiple xml files for different preference screens when selecting the Settings Activity template) I pretty much have nailed down the steps very similarly.
It says towards the end:
The first time you run the app, you should see "-1" displayed in the Toast because you haven't changed the setting yet.
Click to expand...
Click to collapse
However, instead of -1, Im seeing US as the toast message. Im thinking thats because its the default value, but they also set the default value as the US, yet the expectation according to the instructions is -1 the first time they run the app...how come?
Here is my code and the result Im getting first time running the app
SettingsActivity.java:
package com.example.droidcafe;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =
new Intent(MainActivity.this, OrderActivity.class);
intent.putExtra(EXTRA_MESSAGE, mOrderMessage);
startActivity(intent);
}
});
PreferenceManager.setDefaultValues(this,
R.xml.root_preferences, false);
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
String marketPref = sharedPref
.getString("market", "-1");
displayToast(marketPref);
}
root_preferences.xml
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/pref_header_account">
<ListPreference
app:defaultValue="US"
app:entries="@array/pref_market_titles"
app:entryValues="@array/pref_market_values"
app:key="market"
app:negativeButtonText="@null"
app:positiveButtonText="@null"
app:title="@string/pref_title_account" />
</PreferenceCategory>
</PreferenceScreen>
strings.xml:
<string name="pref_header_account">Account</string>
<!-- Sync Preferences -->
<string name="pref_title_account">Market</string>
<string-array name="pref_market_titles">
<item>United States</item>
<item>Canada</item>
<item>United Kingdom</item>
<item>India</item>
<item>Japan</item>
<item>Other</item>
</string-array>
<string-array name="pref_market_values">
<item>US</item>
<item>CA</item>
<item>UK</item>
<item>IN</item>
<item>JA0</item>
<item>-1</item>
</string-array>
first time running app:
Expectation: -1
I realize the default value is US, but their codelab default is also US, yet the codelab says it should display first time -1.