onActivityResult in zxing show results in a different activity - android

Hey guys im making an app using zxing integrater I have the scanner working propelry and show the results fine but would like the reslts to showon a separate class activity any ideas?
JavaActivity
public class QRGOLFActivity extends Activity {
TextView contents = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contents = (TextView) findViewById(R.id.contents);
}
public void doScan(View v) {
IntentIntegrator.initiateScan(this);
}
public void onActivityResult(int request, int result, Intent i) {
IntentResult scan = IntentIntegrator.parseActivityResult(request,
result, i);
if (scan != null) {
contents.setText(scan.getContents());
}
}
#Override
public void onSaveInstanceState(Bundle state) {
state.putString("contents", contents.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle state) {
contents.setText(state.getString("contents"));
}
}
and the Xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="doScan"
android:text="Scan!" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contents"
android:layout_width="fill_parent"
android:layout_height="116dp"
android:textSize="20dp" />
</ScrollView>
<Button
android:id="#+id/score"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:text="Scorecard" />
<Button
android:id="#+id/about"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="About Us" />
<Button
android:id="#+id/contact"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Contact Us" />

Call startActivity() on "a separate class activity", passing whatever data you want (such as the ZXing results) via extras on the Intent you supply to startActivity(). Your "separate class activity" can then use getIntent() to retrieve the Intent and the various get...Extra() methods to retrieve those values.

Related

Android Studio - Save TextView value for different users

My application has an authentication service made in .net and SSMS(SQL Server)
In one of my activities, I want to store a TextView value (that is incrementing due to the user using the application) so that each user has his own value. For example, user 1 uses the application 1h and closes the application. If user 2 logins in the app, the value should be different. When user 1 logins again, the value should be the last one before he closes the app.
Which method should I use in this case? Should I use SharedPreferences?
EDIT 08/09/2020
UserPoints.java
public class UserPoints extends AppCompatActivity{
private TextView userMoneyTV;
private BroadcastReceiver minuteUpdateReceiver;
private int userMoney = 0;
private Button saveMoney, loadMoney;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_points);
userMoneyTV = (TextView) findViewById(R.id.userMoney);
final String UserMoney = userMoneyTV.getText().toString();
saveMoney = (Button) findViewById(R.id.saveMoney);
saveMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("MYPREFS",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("money", UserMoney);
editor.putString("email", String.valueOf(R.id.email));
editor.commit();
}
});
loadMoney = (Button) findViewById(R.id.loadMoney);
loadMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences preferences = getSharedPreferences("MYPREFS",MODE_PRIVATE);
String money = preferences.getString("money",UserMoney);
String email = preferences.getString("email",String.valueOf(R.id.email));
userMoneyTV.setText(money);
}
});
}
public void startMinuteUpdateReceiver(){
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_TICK);
minuteUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
userMoney++;
userMoneyTV.setText(""+ userMoney);
}
};
registerReceiver(minuteUpdateReceiver,intentFilter);
}
#Override
protected void onResume() {
super.onResume();
startMinuteUpdateReceiver();
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(minuteUpdateReceiver);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
activity_user_points.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/rewardLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rewardInfoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<ImageView
android:id="#+id/chestReward"
android:layout_width="200dp"
android:layout_height="150dp"
android:layout_marginTop="120dp"
android:src="#drawable/chestreward"
android:layout_alignParentLeft="true">
</ImageView>
<TextView
android:id="#+id/rewardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginBottom="68dp"
android:layout_toRightOf="#+id/chestReward"
android:text="Earn money every minute you use the app"
android:layout_marginTop="175dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/moneyInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/rewardInfoLayout">
<TextView
android:id="#+id/userMoneyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="casual"
android:text="Your Money: "></TextView>
<TextView
android:id="#+id/userMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_toRightOf="#+id/userMoneyText"
android:layout_marginTop="20dp">
</TextView>
<TextView
android:id="#+id/euro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="€"
android:layout_toRightOf="#+id/userMoney"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/moneyInfo">
<Button
android:id="#+id/saveMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Money"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:onClick="startVideoAd">
</Button>
<Button
android:id="#+id/loadMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Money"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/saveMoney">
</Button>
</RelativeLayout>
</RelativeLayout>
EDIT 9/9/2020
My problem was solved with EDIT 8/9/2020
Now my question is, how can i make the value increasing when i'm in another activity? AsyncTask?
Yes, you can use the SharedPreferences but if the user uninstalls the application user will lose its data and the value you've stored in SharedPreferences.
And if you want to save data even after uninstalling the app you can use any lightweight database i.e. Realm,Room,SQLite,Firebase and store your local database in users sd card. Else use Google backup service or store it in another place like on your server.

Changing Activities through a button and display xml

I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}

Passing value of textview from one activity to another by pressing a button [duplicate]

This question already has answers here:
How to pass a value from one Activity to another in Android?
(7 answers)
Closed 7 years ago.
This is my 1st time working in AndroidStudio. What I want to do is I want to pass the value generated in the txresult(activity_main.xml), the one generated after it scans. I want to pass that value after I push Yes, to my SecondActivity.java, and to be able to display the value.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ddd" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scan"
android:onClick="callZXing"
android:id="#+id/Button" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#ddd" />
<TextView
android:id="#+id/txResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rezultat:"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Is this the correct code?"
android:id="#+id/textView"
android:layout_marginTop="70dp"
android:layout_gravity="center_horizontal" />
<Button
style="#style/CaptureTheme"
android:layout_width="112dp"
android:layout_height="68dp"
android:text="Yes"
android:layout_marginTop="30dp"
android:id="#+id/button2"
android:layout_weight="0.10"
android:layout_gravity="center_horizontal"
android:onClick="sendSecond" />
<Button
style="#style/CaptureTheme"
android:layout_width="112dp"
android:layout_height="68dp"
android:text="No"
android:layout_marginTop="10dp"
android:id="#+id/button1"
android:layout_weight="0.10"
android:layout_gravity="center_horizontal"
android:onClick="callZXing" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
public static final int REQUEST_CODE = 0;
private TextView txResult;
public class FirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button2).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView)findViewById(R.id.txResult);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("textViewText", textView.getText().toString());
startActivity(intent);
}
});
}}
SecondActivity.java
package br.exemplozxingintegration;
import android.app.Activity; import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(extras.getString("textViewText"));
}
}}
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53" /> </LinearLayout>
Using Intent.putExtra() you can do what you want to do.
Intent intent = new Intent( this, MyActivity.class );
intent.putExtra( "paramName", textview.getText().toString() );
startActivity( intent );
You can get the values by using
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String myParam=extras.getString("paramName");
}
You would use this part on the second activity, specifically the onCreate methode.
Try this:
Intent i=new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("result",txResult.getText());
startActivity(i);
and in your SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String strResult=extras.getString("result");
}
strResult gives your string passed from your previous Activity
FirstActivity.java
public class FirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.text_view);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("textViewText", textView.getText().toString());
startActivity(intent);
}
});
}}
first_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_view"
android:text="123"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Text to Second Activity"/>
</LinearLayout>
SecondActivity.java
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(extras.getString("textViewText"));
}
}}
SecondActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Parse information into listView

i am asking you for help. As you see from the picture it is a result that i should have, but, at the moment i have information orinted on my left corner. what i am doing wrog?
MainActivity.java
public class MainActivity extends ListActivity {
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
TextView mTvSDate;
TextView mTvSName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivityForResult(intent, 1);
}
};
ImageButton addBtn = (ImageButton) findViewById(R.id.addBtn);
addBtn.setOnClickListener(listener);
Log.d("Suceess1","Sucess1");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Defining the ArrayAdapter to set items to ListView
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
// Getting reference to TextView tv_sage of the layout file activity_student
// mTvSDate = (TextView) findViewById(R.id.editDate);
// Getting reference to TextView tv_sname of the layout file activity_student
mTvSName = (TextView)findViewById(R.id.editName);
Log.d("Suceess5","Sucess5");
// Fetching data from a parcelable object passed from MainActivity
NoteElement drug = getIntent().getParcelableExtra("drug");
// MyAdapter adapter = new MyAdapter(this, generateData());
Log.d("Suceess6","Sucess6");
list.add(mTvSName.getText().toString());
mTvSName.setText(drug.mSName);
adapter.notifyDataSetChanged();
}
#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;
}
}
SecondElementActivity.java
public class SecondElementActivity extends Activity{
EditText mEtSDate;
EditText mEtSName;
Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_element);
// Getting a reference to EditText et_sname of the layout activity_main
mEtSName = (EditText)findViewById(R.id.editName);
// Getting a reference to EditText et_sage of the layout activity_main
mEtSDate = (EditText)findViewById(R.id.editDate);
// Getting a reference to Button btn_ok of the layout activity_main
btnSave = (Button)findViewById(R.id.btnSave);
// Setting onClick event listener for the "OK" button
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Creating an instance of NoteElement class with user input data
NoteElement drug = new NoteElement(
mEtSDate.getText().toString(),
mEtSName.getText().toString());
// Creating an intent to open the activity MainActivity
Intent intent = new Intent(getBaseContext(), MainActivity.class);
// Passing data as a parecelable object to MainActivity
intent.putExtra("drug",drug);
// Opening the activity
startActivity(intent);
}
});
}
}
Parcelable class NoteElement.java
public class NoteElement implements Parcelable{
String mSDate;
String mSName;
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/**
* Storing the NoteElement data to Parcel object
**/
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mSDate);
dest.writeString(mSName);
}
/**
* A constructor that initializes the NoteElement object
**/
public NoteElement(String sDate, String sName){
this.mSDate = sDate;
this.mSName = sName;
}
/**
* Retrieving NoteElement data from Parcel object
* This constructor is invoked by the method createFromParcel(Parcel source) of
* the object CREATOR
**/
private NoteElement(Parcel in){
this.mSDate = in.readString();
this.mSName = in.readString();
}
public static final Parcelable.Creator<NoteElement> CREATOR = new Parcelable.Creator<NoteElement>() {
#Override
public NoteElement createFromParcel(Parcel source) {
return new NoteElement(source);
}
#Override
public NoteElement[] newArray(int size) {
return new NoteElement[size];
}
};
}
And my activity_main have such a .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Student version -->
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<TextView
android:id="#+id/tv_sdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
<!-- List -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/addBtn" />
</RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_new"
android:contentDescription="#string/desc"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_alignParentLeft="true"
android:layout_marginLeft="53dp"
android:text="#string/mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- Date -->
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/textView1"
android:layout_marginRight="36dp"
android:layout_marginTop="14dp"
android:layout_toLeftOf="#+id/editDate"
android:text="#string/date" />
<EditText
android:id="#+id/editDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editName"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="date"
android:hint="#string/str_hnt_date" />
<!-- Name -->
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="#+id/editName"
android:layout_alignLeft="#+id/date"
android:text="#string/name" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/date"
android:ems="10"
android:hint="#string/str_hnt_name" />
<!-- Dosage -->
<TextView
android:id="#+id/dosage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText3"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/notes"
android:text="#string/dosage" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editName"
android:layout_below="#+id/editName"
android:ems="10"
android:hint="#string/str_hnt_dosage" />
<!-- Notes -->
<TextView
android:id="#+id/notes"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_alignRight="#+id/name"
android:text="#string/notes" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:ems="10"
android:hint="#string/str_hnt_notes"/>
<!-- buttons: Save and Selete-->
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/notes"
android:layout_marginTop="20dp"
android:text="#string/btnSave" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnSave"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="#+id/addBtn"
android:layout_toRightOf="#+id/btnSave"
android:text="#string/btnDelete" />
</RelativeLayout>
Okay so you have two activities total right?
Here's the deal: Look at your image. Activity 1 starts activity 2 right? And you're expecting to get information from your activity 2 back to activity 1, correct? Alright.
First thing to learn:
startActivity(intent);
This method states that you will just initiate an activity but expect nothing back from it. So even if you want to send information back to activity 1 THROUGH activitiy 2 it will not work. Instead you must do this:
startActivityForResult(intent, 1);
The second parameter is an integer that can help you differentiate between different activity calls, it is not important for you right now.
Now, because you say "ForResult" in your method above, in your MainActivity now you must implement this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// here you will work with the code.
// data is your intent data sent from activity 2
// where you say this in your own code:
// Passing data as a parecelable object to MainActivity
// intent.putExtra("drug",drug);
}
Now the last thing to note:
In SecondElementActivity.java where you have this:
// Opening the activity
startActivity(intent);
It is wrong. You know why? because you're saying that you want to start a new activity. But in Android you already have a parent for this activity, which is activity 1. So your activity 1 called 2, when you end activity 2 it will go back to 1. So, replace that line for this:
setResult(RESULT_OK, intent);
finish();
EDIT:
Also I don't know if this is correct, I don't do it this way, so here is my fix:
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.SecondElementActivity");
startActivity(intent);
}
};
When you say you want to make a new intent you should pass two parameters:
Intent intent = new Intent(this, SecondElementActivity.class);
The second parameter is the name of the class you want to call.
You can try like this :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/hello_world"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addBtn"
android:layout_centerHorizontal="true"
android:text="mainTxt"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="32sp" />
<!-- List -->
<ListView
android:id="#+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp" />
</RelativeLayout>
Follow This tutorial for populating ListView just change adapter and row.xml from that tutorial
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_sdate "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="5dp"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/tv_sname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tv_sdate "
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
http://hmkcode.com/android-custom-listview-items-row/

Multiple checkbox information is displayed in another activity when checked

Our problem is we need to make some kind of a menu where on the first activity there is a list of food with checkboxes.
What we wanted to do is when the onClickmeal_ok button is clicked, the information in the checkboxes that were checked will be displayed in another activity.
This is our .xml file:
<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"
android:background="#ffff"
tools:context=".MainMenu" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/dessertsmenu"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="27dp"
android:text="#string/meal_ok"
android:onClick="onClickmeal_ok" />
<CheckBox
android:id="#+id/chkAutosave3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chkAutosave1"
android:layout_below="#+id/chkAutosave1"
android:defaultValue="false"
android:text="#string/peachcrumbbars"
android:onClick="onClickpeachcrumbbars_ok" />
<CheckBox
android:id="#+id/chkAutosave4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chkAutosave3"
android:layout_below="#+id/chkAutosave3"
android:defaultValue="false"
android:text="#string/snickerysquares"
android:onClick="onClicksnicjerysquares_ok" />
<CheckBox
android:id="#+id/chkAutosave1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="22dp"
android:layout_marginTop="18dp"
android:defaultValue="false"
android:text="#string/blackberrypiebars"
android:onClick="onClickblackberrypiebars_ok" />
and this is our .java file:
package net.xadtv.yoursmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class DessertsMenu extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.desserts);
}
public void onClickblackberrypiebars_ok(View view) {
}
public void onClickpeachcrumbbars_ok(View view) {
}
public void onClicksnicjerysquares_ok(View view) {
}
public void onClickmeal_ok(View view) {
}
}
To pass data between activies use Intent extra,
public void onClickmeal_ok(View view)
{
Intent i = new Intent("com.example.activityname");
i.putExtra("extra1", "this is an extra");
i.putExtra("extra2", 420);
}
You can also use Bundle to put multiple items at once
public void onClickmeal_ok(View view)
{
Intent i = new Intent("com.example.activityname");
Bundle extras = new Bundle();
extras.putInt("extra4", 420);
i.putExtras(extras);
}
Then you can retrieve data by using
String stringExtra1 = getIntent().getString("extra1");
or retrieve the whole bundle with
Bundle bundle = getIntent().getExtras();

Categories

Resources