[Help] Do my stuff after activity loads - Android Software Development

Hello,
I'm trying to do something with my app but i dont know how =) maybe you can help me.
My main activity needs to initialize some parts of the app. It takes 2-5 seconds and I'd like to show the user a progress dialog.
the code im using is like
Code:
ProgressDialog tempdialog= ProgressDialog.show(this, "Loading", "Please Wait");
Config.init();
temdialog.cancel();
But i dont know where to put that code. I tried in onCreate(), onStart() and onResume(). But all them show a blackscreen while initializing and dialog is never shown.
Thanks in advance

removed

Thanks for the answer. I post my solution for helping others with the same problem
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd=ProgressDialog.show(this, "Loading", "Please wait");
Thread t = new Thread() {
public void run() {
try {
Config.init();
myHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
public Handler myHandler=new Handler() {
public void handleMessage(Message msg) {
pd.hide();
}
};

Related

Android ICS SSL Authentication Help

Im trying to build an RSS feed reader that needs to do some client side SSL authentication.
Ive got, or at least think i have, the certificate and now cannot figure out how to setup a ssl tunnel to send the certificate to the server to authenticate.
here is what i have so far:
public class Authenticator extends Activity {
PrivateKey privateKey = null;
String SavedAlias = "";
private static final String TAG = "AUTHENTICATOR.CLASS";
final HttpParams httpParams = new BasicHttpParams();
private KeyStore mKeyStore = KeyStore.getInstance();
public Handler mHandler = new Handler(Looper.getMainLooper());
public void run()
{
mHandler.post(new Runnable() {
public void run() {
new AliasLoader().execute();
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getCertificates("TEST");
}
public class AliasLoader extends AsyncTask<Void, Void, X509Certificate[]>
{
X509Certificate[] chain = null;
@Override protected X509Certificate[] doInBackground(Void... params) {
android.os.Debug.waitForDebugger();
if(!SavedAlias.isEmpty())
{
try {
chain = KeyChain.getCertificateChain(getApplicationContext(),SavedAlias);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
else
{
this.cancel(true);
}
return chain;
}
@Override
protected void onPostExecute(X509Certificate[] chain)
{
if(chain != null)
{
Toast.makeText(getApplicationContext(), "YAY, Certificate is not empty", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Certificate is Empty", Toast.LENGTH_LONG).show();
}
/*
if (privateKey != null) {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1withRSA");
} catch (NoSuchAlgorithmException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
try {
signature.initSign(privateKey);
} catch (InvalidKeyException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
*/
}
}
public void getCertificates(String Host)
{
KeyChainAliasCallback callBack = new KeyChainAliasCallback() {
@Override
public void alias(String alias) {
if (alias != null)
{
Looper.prepare();
saveAlias(alias);
run();
Looper.loop();
}
}
};
KeyChain.choosePrivateKeyAlias(this, callBack,
new String[] {"RSA", "DSA"}, // List of acceptable key types. null for any
null, // issuer, null for any
null, // host name of server requesting the cert, null if unavailable
443, // port of server requesting the cert, -1 if unavailable
null); // alias to preselect, null if unavailable
}
public void saveAlias(String alias)
{
SavedAlias = alias;
}
}
Any help on how to do this would be greatly appreciated as i have never done any authentication before and i have found it difficult to find anything on this topic for android 4.0 as 4.0 seems to be different in implementation then the older versions.

Problem of flashlight app code (Eclipse)

Hello everyone ..
I have problem on developing a flashlight app
I tried the solutions that provided by Eclipse but none solved the problem
or even make any sense to me :angel:
every "Void" statement on the code appeared as an error
and i don't know why .. also i'm sure that everything on the code is correct
Examples of the errors:
private void playSound() {
private void toggleButtonImage() {
private void turnOffFlash() {
and .. etc , i got an error on every "Void" !
so.. i'm waiting for your opinions
Thanks in advance )​
Hi,
It is impossible for us to tell you what you have done wrong if you do not post your source code.
It could be just one character missing somewhere or one to much.
We need your code to help you.
(If you do not want to release it completely, copy your project and remove those parts you do not want us to see. This could also help you with finding the error yourself. )
nikwen said:
Hi,
It is impossible for us to tell you what you have done wrong if you do not post your source code.
It could be just one character missing somewhere or one to much.
We need your code to help you.
(If you do not want to release it completely, copy your project and remove those parts you do not want us to see. This could also help you with finding the error yourself. )
Click to expand...
Click to collapse
Thanks 4 answering me nikwen
i tried to post a screenshot of the problem but i'm a new member in the forum so i couldn't post an external link
anyway .. here's the Code
package com.mohamedsherif.flashlighttorch;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ImageButton switchButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.switchButton);
//Check if the Device has Flash
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error! ");
alert.setMessage("Sorry! .. Your device doesn't have a flash!");
alert.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
finish();
}
});
alert.show();
return;
}
//Getting camera parameters
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to open. Error", e.getMessage());
}
}
}
private void turnOnFlash() {
if (!hasFlash) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
toggleButtonImage();
}
}
//Turning off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
toggleButtonImage();
}
};
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});
// Toggle buttons images
private void toggleButtonImage() {
if (isFlashOn) {
switchButton.setImageResource(R.drawable.switch_on);
} else {
switchButton.setImageResource(R.drawable.switch_off);
}
}
// Playing Sound
private void playSound() {
if (isFlashOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Click to expand...
Click to collapse
the errors are in RED ​
little changes
private static Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
ToggleButton switchButton;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
getCamera();
switchButton = (ToggleButton) findViewById(R.id.tglOnOffFlashlight);
switchButton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});
//Check if the Device has Flash
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(FlashlightActivity.this).create();
alert.setTitle("Error! ");
alert.setMessage("Sorry! .. Your device doesn't have a flash!");
alert.setButton("ok", new DialogInterface.OnClickListener() {
@override
public void onClick(final DialogInterface dialog, final int which) {
finish();
}
});
alert.show();
return;
}
}
//Getting camera parameters
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to open. Error", e.getMessage());
}
}
}
private void turnOnFlash() {
if (hasFlash) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
toggleButtonImage();
}
}
//Turning off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
toggleButtonImage();
}
}
// Toggle buttons images
private void toggleButtonImage() {
if (isFlashOn) {
switchButton.setBackgroundResource(R.drawable.on_yellow );
} else {
switchButton.setBackgroundResource(R.drawable.off_white);
}
}
// Playing Sound
private void playSound() {
if (isFlashOn) {
mp = MediaPlayer.create(FlashlightActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(FlashlightActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
these work
Does it work on other projects?
It is working for him, so I guess that the errors are not in the code but maybe your project setup or your installation of Eclipse and the Android SDK is the reason for that.
So to quote myself " Does it work on other projects?".
---------- Post added at 09:59 PM ---------- Previous post was at 09:55 PM ----------
Oh no, there are mistakes with the brackets.
For example you set a listener somewhere inside a methods. However, you forgot to close the bracket (I mean these ones {}). So it ends with a semicolon. It wants more lines of code.
So check your brackets.
Void methods should not return anything. So you can remove all 'return' words in the void methods.
Sent from my NexusHD2 using xda app-developers app
Eztys said:
Void methods should not return anything. So you can remove all 'return' words in the void methods.
Sent from my NexusHD2 using xda app-developers app
Click to expand...
Click to collapse
These returns ARE allowed. They are not necessary but allowed. You can stop the method by this.
It is really a problem with these brackets {}.
krsln said:
private static Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
ToggleButton switchButton;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
getCamera();
switchButton = (ToggleButton) findViewById(R.id.tglOnOffFlashlight);
switchButton.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
these work
Click to expand...
Click to collapse
the same problem with the Void errors !! -.-
nikwen said:
Does it work on other projects?
It is working for him, so I guess that the errors are not in the code but maybe your project setup or your installation of Eclipse and the Android SDK is the reason for that.
So to quote myself " Does it work on other projects?".
---------- Post added at 09:59 PM ---------- Previous post was at 09:55 PM ----------
Oh no, there are mistakes with the brackets.
For example you set a listener somewhere inside a methods. However, you forgot to close the bracket (I mean these ones {}). So it ends with a semicolon. It wants more lines of code.
So check your brackets.
Click to expand...
Click to collapse
i tried the code on a new project but i got the same errors
also i don't think there's any problem with the brackets ?
Eztys said:
Void methods should not return anything. So you can remove all 'return' words in the void methods.
Sent from my NexusHD2 using xda app-developers app
Click to expand...
Click to collapse
the return is not the problem ​
i just solved the void errors but the app is not running on the virtual device !
"the app has stopped unexpectedly"
can anyone try the code ? or even tell me what's wrong ?
BTW the code doesn't contain any errors now .. not even the void errors
package com.mohamedsherif.flashlighttorch;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton switchButton;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.switchButton);
//Check if the Device has Flash
hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(!hasFlash) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Error! ");
alert.setMessage("Sorry! .. Your device doesn't have a flash!");
alert.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
finish();
}
});
alert.show();
return;
}
getCamera();
toggleButtonImage();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});
}
//Getting camera parameters
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to open. Error: ", e.getMessage());
}
}
}
private void turnOnFlash() {
if (!hasFlash) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
toggleButtonImage();
}
}
//Turning off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
toggleButtonImage();
}
};
// Toggle buttons images
private void toggleButtonImage() {
if (isFlashOn) {
switchButton.setImageResource(R.drawable.switch_on);
} else {
switchButton.setImageResource(R.drawable.switch_off);
}
}
// Playing Sound
private void playSound() {
if (isFlashOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
turnOffFlash();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (hasFlash)
turnOnFlash();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
getCamera();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (camera != null) {
camera.release();
camera = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Click to expand...
Click to collapse
M. Sherif said:
i just solved the void errors but the app is not running on the virtual device !
"the app has stopped unexpectedly"
can anyone try the code ? or even tell me what's wrong ?
BTW the code doesn't contain any errors now .. not even the void errors
​
Click to expand...
Click to collapse
There will be a logcat. Post it here.

why doInBackground run twice

im using AsyncTask in my project
but i do not understand why doInBackground run twice when execute? any expert can advice me?
new AsyncTask<Void, Void, Void>() {
@override
protected void onPreExecute() {
}
@override
protected Void doInBackground(Void... params) {
call(); //my function
return null;
}
}
lye85 said:
im using AsyncTask in my project
but i do not understand why doInBackground run twice when execute? any expert can advice me?
new AsyncTask<Void, Void, Void>() {
@override
protected void onPreExecute() {
}
@override
protected Void doInBackground(Void... params) {
call(); //my function
return null;
}
}
Click to expand...
Click to collapse
The only way for it to run twice is if you call it twice, and as far as I can remember AsyncTask gets invalidated after first execution finishes, so you must be pretty explicityly newing and running this AsyncTaks twice.

[Q]Problem using RootTools library

i am trying to list files of root folders using roottools libary .here is the code
Code:
public ArrayList<File> listFiles(final Context c,final String path) {
final ArrayList<File> a=new ArrayList<File>();
Command command = new Command(0, "ls "+path)
{
@Override
public void commandOutput(int i, String s) {
File f=new File(path+"/"+s);
a.add(f);}
@Override
public void commandTerminated(int i, String s) {
}
@Override
public void commandCompleted(int i, int i2) {
//want to arraylist return from here
}
};
try {
RootTools.getShell(true).add(command);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
now problem is that if i return arraylist<file> from the end of method,its size is zero,to solve it i must return the arraylist from commandCompleted
method.but i am not able to accomplish that.So please solve my problem
Maybee arraylist as static outside your function? So on top of source file? Not sure but maybee

[Q] How do I add an image picker to my app, show it in image view, then save it to pa

This is what I have:
Code:
package JEB.ssf;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View.*;
import com.parse.*;
import android.content.*;
import android.support.v4.app.*;
import java.util.*;
import android.widget.AdapterView.*;
import android.util.*;
import com.google.android.gms.ads.*;
import com.google.ads.mediation.*;
import com.google.ads.*;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdListener;
import android.provider.*;
import android.database.*;
import android.graphics.*;
public class ProfilePage extends Activity
{
private static int LOAD_IMAGE_RESULTS = 1;
// GUI components
private Button button; // The button
private ImageView image;// ImageView
Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2, adp3;
List<String> l1,l2, l3;
int pos;
EditText fname, lname, hf, hi, weight,
waist, wrist, hip, forearm, age, goal;
TextView userName, gender, category, unit;
String hftxt, hitxt, id, firstNameTxt, userNameTxt, posttxt, postnum;
Button share;
float obid;
int in;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_page);
getActionBar().setDisplayHomeAsUpEnabled(true);
Parse.initialize(this, "abmPl8JnnhyaqjA0Pp8ZWmptKTCeV0p8eoMEHPaL", "icKQxW5AdPXHJtsGEqSX5LwJ2Dd8zt0USKd1PNeP");
fname = (EditText) findViewById(R.id.first_name);
lname = (EditText) findViewById(R.id.last_name);
hf = (EditText) findViewById(R.id.height_feet);
hi = (EditText) findViewById(R.id.height_inch);
weight = (EditText) findViewById(R.id.weight_lbs);
waist = (EditText) findViewById(R.id.waist);
wrist = (EditText) findViewById(R.id.wrist);
hip = (EditText) findViewById(R.id.hip);
forearm = (EditText) findViewById(R.id.forearm);
age = (EditText) findViewById(R.id.age);
userName = (TextView) findViewById(R.id.user_name);
gender = (TextView) findViewById(R.id.gender);
category = (TextView) findViewById(R.id.category);
unit =(TextView) findViewById(R.id.unit);
goal =(EditText) findViewById(R.id.goal);
share = (Button) findViewById(R.id.share);
Intent i = getIntent();
userNameTxt = i.getStringExtra("user_name");
userName.setText(userNameTxt);
button = (Button)findViewById(R.id.image_pick);
image = (ImageView)findViewById(R.id.image);
gender_button();
category_button();
unit_button();
image_button();
share_button();
small_banner_ad();
final ParseQuery<ParseObject> query = ParseQuery.getQuery("profile_info");
// Restrict to cases where the author is the current user.
// Note that you should pass in a ParseUser and not the
// String reperesentation of that user
query.whereEqualTo("user_data", ParseUser.getCurrentUser());
// Run the query
query.findInBackground(new FindCallback<ParseObject>() {
public String TAG;
[user=439709]@override[/user]
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
// If there are results, update the list of posts
// and notify the adapter
for (ParseObject profile_info : postList) {
userName.setText(profile_info.getString("user_name"));
fname.setText(profile_info.getString("first_name"));
lname.setText(profile_info.getString("last_name"));
hf.setText(profile_info.getString("height_in_feet"));
hi.setText(profile_info.getString("height_in_inches"));
weight.setText(profile_info.getString("weight"));
waist.setText(profile_info.getString("waist"));
wrist.setText(profile_info.getString("wrist"));
hip.setText(profile_info.getString("hip"));
forearm.setText(profile_info.getString("forearm"));
age.setText(profile_info.getString("age"));
gender.setText(profile_info.getString("gender"));
category.setText(profile_info.getString("category"));
unit.setText(profile_info.getString("unit"));
goal.setText(profile_info.getString("goal"));
}
} else {
Log.d("Post retrieval", "Error: " + e.getMessage());
}
}});
}
private void share_button()
{
share.setOnClickListener(new OnClickListener() {
public void onClick(View arg1) {
final ParseObject posts = new ParseObject("posts");
posts.put("user_name", userName.getText().toString());
posts.put("category", "My goal is to" + " "+category.getText()+" "+goal.getText()+" "+unit.getText().toString());
// Create an user data relationship with the current user
posts.put("user_posts", ParseUser.getCurrentUser());
// Save the post and return
posts.saveInBackground(new SaveCallback () {
[user=439709]@override[/user]
public void done(ParseException e) {
if (e == null) {
setResult(RESULT_OK);
Toast.makeText(getApplicationContext(),"post successfull",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error sharing: " + e.getMessage(),
Toast.LENGTH_SHORT)
.show();
}
}});
}
});
}
private void unit_button()
{
unit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(ProfilePage.this, unit);
//Inflating the Popup using xml file
if (category.getText().toString().equals("Loose Weight")){
popup.getMenuInflater()
.inflate(R.menu.weight_menu, popup.getMenu());
}if(category.getText().toString().equals("Target Weight")){
popup.getMenuInflater()
.inflate(R.menu.weight_menu, popup.getMenu());
}if(category.getText().toString().equals("Gain Weight")){
popup.getMenuInflater()
.inflate(R.menu.weight_menu, popup.getMenu());
}if(category.getText().toString().equals("Build Muscle")){
popup.getMenuInflater()
.inflate(R.menu.weight_menu, popup.getMenu());
}if(category.getText().toString().equals("Run")){
popup.getMenuInflater()
.inflate(R.menu.distance_menu, popup.getMenu());
}if(category.getText().toString().equals("Walk")){
popup.getMenuInflater()
.inflate(R.menu.distance_menu, popup.getMenu());
}
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.lbs:
//act for menu_item1
unit.setText("lbs");
return true;
case R.id.mile:
//act for sub_menu_item1
unit.setText("Mile");
return true;
case R.id.k:
//act for sub_menu_item1
unit.setText("K");
return true;
default:
//pass the event to parent, for options menu, use below
}
return false;
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
}
private void category_button()
{
category.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(ProfilePage.this, category);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.category_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.target_weight:
category.setText("Target Weight");
return true;
case R.id.loose_weight:
//act for menu_item1
category.setText("Loose Weight");
return true;
case R.id. gain_weight:
//act for sub_menu_item1
category.setText("Gain Weight");
return true;
case R.id. build_muscle:
//act for sub_menu_item1
category.setText("Build Muscle");
return true;
case R.id. run:
//act for sub_menu_item1
category.setText("Run");
return true;
case R.id. walk:
//act for sub_menu_item1
category.setText("Walk");
return true;
default:
//pass the event to parent, for options menu, use below
}
return false;
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
}
private void gender_button()
{
gender.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(ProfilePage.this, gender);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.gender_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id. male:
//act for menu_item1
gender.setText("Male");
return true;
case R.id. female:
//act for sub_menu_item1
gender.setText("Female");
return true;
default:
//pass the event to parent, for options menu, use below
}
return false;
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
}
private void small_banner_ad()
{
AdView adView = (AdView) this.findViewById(R.id.adView);
//request TEST ads to avoid being disabled for clicking your own ads
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)// This is for emulators
//test mode on DEVICE (this example code must be replaced with your device uniquq ID)
.addTestDevice("15174709D5FCBF710958952E2589F20D") // Nexus 6
.build();
adView.loadAd(adRequest);
}
private void save_update_button()
{
ParseUser.getCurrentUser();
ParseQuery<ParseObject> pQuery = new ParseQuery<ParseObject>("profile_info");
pQuery.whereEqualTo("user_name", userName.getText().toString());
pQuery.getFirstInBackground(new GetCallback<ParseObject>()
{ [user=439709]@override[/user]
public void done(ParseObject update, ParseException e) {
if (e == null){
update.put("user_name", userName.getText().toString());
update.put("first_name", fname.getText().toString());
update.put("last_name", lname.getText().toString());
update.put("height_in_feet", hf.getText().toString());
update.put("height_in_inches", hi.getText().toString());
update.put("weight", weight.getText().toString());
update.put("waist", waist.getText().toString());
update.put("wrist", wrist.getText().toString());
update.put("hip", hip.getText().toString());
update.put("forearm", forearm.getText().toString());
update.put("age", age.getText().toString());
update.put("gender", gender.getText().toString());
update.put("category", category.getText().toString());
update.put("unit", unit.getText().toString());
update.put("goal", goal.getText().toString());
update.saveInBackground();
Toast.makeText(getApplicationContext(),
"Your profile has been updated",
Toast.LENGTH_SHORT)
.show();
} else {
final ParseObject profile_info = new ParseObject("profile_info");
profile_info.put("user_name", userNameTxt);
profile_info.put("first_name", fname.getText().toString());
profile_info.put("last_name", lname.getText().toString());
profile_info.put("height_in_feet", hf.getText().toString());
profile_info.put("height_in_inches", hi.getText().toString());
profile_info.put("weight", weight.getText().toString());
profile_info.put("waist", waist.getText().toString());
profile_info.put("wrist", wrist.getText().toString());
profile_info.put("hip", hip.getText().toString());
profile_info.put("forearm", forearm.getText().toString());
profile_info.put("age", age.getText().toString());
profile_info.put("gender", gender.getText().toString());
profile_info.put("category", category.getText().toString());
profile_info.put("unit", unit.getText().toString());
profile_info.put("goal", goal.getText().toString());
// Create an user data relationship with the current user
profile_info.put("user_data", ParseUser.getCurrentUser());
// Save the post and return
profile_info.saveInBackground(new SaveCallback () {
[user=439709]@override[/user]
public void done(ParseException e) {
if (e == null) {
setResult(RESULT_OK);
Toast.makeText(getApplicationContext(),"Profile saved",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error saving: " + e.getMessage(),
Toast.LENGTH_SHORT)
.show();
}
}});
}
}
});
}
//image button
public void image_button(){
image.setOnClickListener(new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
// Create the Intent for Image Gallery.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
startActivityForResult(i, LOAD_IMAGE_RESULTS);
}});
}
//menu [user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.profile_menu, menu);
return super.onCreateOptionsMenu(menu);
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
// Handle action buttons
switch(item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.logout:
ParseUser.logOut();
finish();
return true;
case R.id.Wall:
Intent intent = new Intent(ProfilePage.this,
Wall.class);
startActivity(intent);
return true;
case R.id.Save:
save_update_button();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
[user=439709]@override[/user]
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
// Now we need to set the GUI ImageView data with data read from the picked file.
image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
// At the end remember to close the cursor or you will end with the RuntimeException!
cursor.close();
}}}
[\CODE]
When I import "uri" for my on activity results at the end of my code I get a bunch of errors for everything I'm doing with parse. I been searching and trying different image picker tutorials and always end up with same results. I've checked out parse.com but only see info on saving images from the drawable folder. If I can get the image picker to work I'm pretty sure I can figure out how to save it to parse.com.
Sent from my Nexus 6 using XDA Free mobile app

Categories

Resources