I am developing a Task manager( a simple one) to learn android and this is my short description.
I am making two activities one to view task and other to add ( and I am using an Application class through which i add the task)..
when i add the task on the addtask activity my viewtask activity still remains blank. Could you help me out.
This is my viewTask activity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
setupviews();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
System.out.println(requestCode);
if (requestCode == 1) {
showtask(); // your "refresh" code
}
}
public void OnResume()
{
super.onResume();
System.out.println("Its working...");
showtask();
}
/*public void onPause(){
super.onPause();
}*/
public void showtask()
{
ArrayList<Task> task=gettma().gettask();
StringBuffer sb=new StringBuffer();
for(Task t:task){
sb.append(String.format("* %s\n", t.toString()));
System.out.println("Its working...again");
}
taskText.setText(sb.toString());
}
private TaskManagerApplication gettma() {
TaskManagerApplication tma1=(TaskManagerApplication)getApplication();
return tma1;
}
private void setupviews() {
AddButton=(Button)findViewById(R.id.AddButton);
taskText=(TextView)findViewById(R.id.TextList);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(ViewTask.this,AddTask.class);
startActivity(intent);
}
});
}
}
And this is my Addtask activity.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask);
setupviews();
}
protected void addtask() {
String name=EditText.getText().toString();
Task t= new Task(name);
//System.out.println("its working here");
gettma().addTask(t);
finish();
}
private TaskManagerApplication gettma() {
TaskManagerApplication tma=(TaskManagerApplication)getApplication();
return tma;
}
private void setupviews() {
EditText=(EditText)findViewById(R.id.EditText);
AddButton=(Button)findViewById(R.id.AddButton1);
CancelButton=(Button)findViewById(R.id.CancelButton);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtask();
}
});
CancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Please Help
EditText=(EditText)findViewById(R.id.EditText);
This is the line in your setupviews(). You cannot do this because EditText is a class and you need a variable of that type to which you can assign a value. It must be EditText followed by some variable name.
if (requestCode == 1) {
showtask(); // your "refresh" code
}
This is in your onActivityResult(). You aren't checking the resultCode to check if the called Activity failed to do the operation that would produce a result.
Also, in numerous places you arent following the Java naming convention. Please, use upper and lower cases properly when naming variables and classes.
Related
I'm having an Issue that I have two Activities A and B when I clicked on Button to change an Activity from A to B . It Restart my Application I don't know what going wrong Please help me
public class Login_Choice_Activity extends AppCompatActivity{
private Button d_btn,p_btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login__choice);
FindAllView();
p_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// startActivity(new
Intent(Login_Choice_Activity.this,Patient_SignIn_Activity.class));
Toast.makeText(getApplicationContext(),"CLICKED",Toast.LENGTH_LONG).show();
}
});
}
private void FindAllView(){
d_btn = findViewById(R.id.choice_doctor_btn);
p_btn = findViewById(R.id.choice_patient_btn);
}
}
https://i.stack.imgur.com/KcgAh.gif
It could be better if you create a method for opening the new Activity and call it from onClick(View v) method. Example
private void openActivity() {
startActivity(new Intent(this,Patient_SignIn_Activity.class));
}
In your onClick call:
#Override
public void onClick(View v) {
openActivity()
}
I'm using two activities, MainActivity & SubActivity.
MainActivity.class
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivity(sub);
}
}
}
}
SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
In this case, if I change the text in EditText in SubActivity, I want to send changed text data to MainActivity when back button pressed in SubActivity(It means SubActivity onDestroy()). I don't want to make the second MainActivity like this in SubActivity.
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
startActivity(intent);
}
Is there any way to send text data when SubActivity removed from the activity stack?
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivityForResult(sub , 2);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("name");
textView1.setText(message);
}
}
}
And
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
setResult(2,intent);
}
You need to use startActivityForResult() if you expect to have a return from the started activity.
After that, you'll also need to implement onActivityResult() method on the start activity to actually read the returned data.
Use startActivityForResult instead of startActivity
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
String name= "a";
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", name);
startActivityForResult(sub,100);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 100) {
String name = data.getStringExtra("name);
text.setText(name);
}
}
}
}
SubActivity:
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
Intent resultIntent = new Intent();
resultIntent.putExtra("name", "your_editext_value");
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can use SharedPreference for storing value and update it or use it anywhere instead of using Intent
like In MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Editor edit=sp.edit();
edit.putString("name","a");
edit.commit();
Intent sub = new Intent(this, SubActivity.class);
startActivity(sub);
}
}
} }
and In SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(sp.getString("name",""));
text.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editor edit=sp.edit();
edit.putString("name",s.toString);
edit.commit();
} });
}
#Override
public void onBackPressed() {
super.onBackPressed();
}}
To use value from SharedPreference
sp.getString("name","");
For using SharedPreference look at this https://developer.android.com/training/data-storage/shared-preferences#java
In my app I create two activities and I want to get input from the second activity and use it in the first activity, I use startActivityForResult but there is a problem in it!
That is the code of first activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent("net.naif.action.GETDATA");
startActivityForResult(i,77);
}
});
}
protected void onActivityForResult (int requestCode,int resultCode,Intent data){
if (requestCode ==77 && resultCode ==RESULT_OK){
String msg =data.getStringExtra("text");
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
}
}
And this for the second:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Button b=(Button)findViewById(R.id.button);
final EditText t=(EditText)findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s=t.getText().toString();
Intent i=new Intent();
i.putExtra("text",s);
setResult(Activity.RESULT_OK,i);
finish();
}
});
}
Android studio notify me that is the method ( protected void onActivityForResult (int requestCode, int resultCode, Intent data) is never used.
That's because the method is named onActivityResult, not onActivityForResult.
I am using the ZXING QR Code Reader. It is working fine. Detecting codes etc but it does not shows any detection progress(Like red line) while it is scanning the QR Code. Here is Screenshot
And my ScanActivity is,
ScanCode.Java
public class ScanCode extends Activity implements OnClickListener {
Button btnScan;
TextView scanResult;
String text;
Bitmap bmp;
ImageView ivPic;
#Override
public void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.scan_code);
initViews();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnScanCode) {
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
}
}
private void initViews() {
scanResult = (TextView) findViewById(R.id.scanResult);
ivPic = (ImageView) findViewById(R.id.capturedImg);
btnScan = (Button) findViewById(R.id.btnScanCode);
btnScan.setOnClickListener(this);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
Toast.makeText(getApplicationContext(), contents,
Toast.LENGTH_SHORT).show();
scanResult.setText(contents);
}// if result_ok
}// onActivityResult
}
I know this answer is bit late but I've just used Zxing barcode/QR-Code scanner recently in my app, if anyone has issue implementing it just follow the steps below (Using Android Studio Official IDE for android).
First add dependencies in app --> build.gradle file
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
}
Activity Usage:
public class MainActivity extends AppCompatActivity {
Button btnScan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scanBarcode();
}
});
}
private void scanBarcode()
{
new IntentIntegrator(this).initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
}
}
}
If you want to use custom callback and BarCodeView. Zxing provide this functionality simply make layout file
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.journeyapps.barcodescanner.CompoundBarcodeView>
<TextView
android:id="#+id/tvScanResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Results will be shown here"
android:textColor="#android:color/white"
android:textStyle="bold"/>
In Activity use the following code
public class MainActivity extends AppCompatActivity
{
CompoundBarcodeView barcodeView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcodeView = (CompoundBarcodeView) findViewById(R.id.barcode_scanner);
barcodeView.decodeContinuous(callback);
barcodeView.setStatusText("");
}
#Override
protected void onResume()
{
super.onResume();
barcodeView.resume();
isScanned = false;
}
#Override
protected void onPause()
{
super.onPause();
barcodeView.pause();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
private BarcodeCallback callback = new BarcodeCallback()
{
#Override
public void barcodeResult(BarcodeResult result)
{
if (result.getText() != null)
{
barcodeView.setStatusText(result.getText());
tvscanResult.setText("Data found: " + result.getText());
}
//you can also Add preview of scanned barcode
//ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
//imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}
#Override
public void possibleResultPoints(List<ResultPoint> resultPoints)
{
System.out.println("Possible Result points = " + resultPoints);
}
};
}
Source zxing-android-embedded. Hope someone get help from this solution
I've this code:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, ConnectDialog.class);
update();
}
private void update(){
if(a)
startActivity(intent);
else{
//code
}
}
}
And this:
public class ConnectDialog extends Activity{
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_dialog);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
The problem is: when I click on the button of the Intent, is it possible to execute the method update of the main activity again? Thank you very much
Just put the call to update into onResume() of the MainActivity. This way, it will be called at first startup and when the MainActivity is shown again later on:
#Override
protected void onResume(){
super.onResume;
update();
}
I would use the onActivityResult method.
Change your update method to
int YOUR_RESULT = 100;
private void update(){
if(a)
startActivityForResult(intent, YOUR_RESULT);
else{
//code
}
}
then in that same activity, use this method:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_RESULT) {
update();
}
}
If you use this method, the update() method will only get called when coming back from that activity.
if u check the Android Life Cycle u will get the answer why the Update is not executing..
How ever to call the update method on click..
private void update(){
if(a)
startActivityForResult(intent, 0);
else{
//code
}
}
and use onActivityResult to call the update method again
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
update();
}