Multiple checkbox information is displayed in another activity when checked - android

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();

Related

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>

How to convert textview as link in android?

I have created a login page which consists of a username password login button and a signup button which should be a link. I want to convert this signup(text view) to a link that can be navigated to the next page. How would I do that? For example if I click on don't have an account? sigup,it should be navigated to welcome.java..
Please help....
activity_main.xml
<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:background="#drawable/bg_gradient"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="45dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_form_rounded"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#null"
android:hint="#string/email"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:hint="#string/password"
android:inputType="textPassword"
android:padding="5dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#drawable/bg_button_rounded"
android:text="#string/login"
android:textColor="#color/white" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:text="#string/signup"
android:autoLink="web"
android:textColor="#color/white" />
</RelativeLayout>
mainactivity.java
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getActionBar().hide();
}
}
Try this :
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
TextView txtSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSignUp=(TextView)findViewById(R.id.txt_sign_up);
txtSignUp.setText(Html.fromHtml("<u>Dont have account?SignUp</u>")
txtSignUp.setOnClickListener(signUpListener);
//getActionBar().hide();
}
public OnClickListener signUpListener=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Welcome.class));
}
};
}
You have to setOnTouchListener to the TextView. Here I give you an example:
public class MainActivity extends Activity implements View.OnTouchListener {
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// HERE YOU HAVE TO CHECK WHICH ID IS SELECTED, IF YOU HAVE MORE THAN ONE,
// AND WHICH EVENT HAS THE USER DONE.
// IN YOUR CASE.. OPEN WEBSITE LIKE THIS:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
}
you can try Linkify here
textview.setText("www.androiddeveloper.com");
Linkify.addLinks(textview, Linkify.WEB_URLS);

Setting a ProgressBar to VISIBLE on click in android

I am trying to make my ProgressBar turn VISIBLE when the LogIn button is clicked. Here's my XML for the Activity:
<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:gravity="center_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtView_UserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="#string/login_username_hint">
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtView_Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtView_UserName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="#string/login_password_hint"
android:inputType="textPassword" />
<Button
android:id="#+id/button_LogIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtView_UserName"
android:layout_below="#+id/txtView_Password"
android:layout_marginTop="10dp"
android:onClick="logIn"
android:text="#string/login_button" />
<CheckBox
android:id="#+id/checkbox_RememberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtView_Password"
android:layout_below="#+id/button_Login"
android:layout_marginTop="15dp"
android:text="#string/remember_my_password" />
<CheckBox
android:id="#+id/checkbox_LogMeInAutomatically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkbox_RememberPassword"
android:layout_below="#+id/checkbox_RememberPassword"
android:layout_marginTop="-5dp"
android:text="#string/log_me_in_automatically" />
<ProgressBar
android:id="#+id/progressBar_LogIn"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_LogMeInAutomatically"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:visibility="invisible" />
<TextView
android:id="#+id/txtView_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="#string/version_number" />
</RelativeLayout>
And here is where I am trying to change it to VISIBLE in the code.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Get Views and Buttons
EditText txt_Username = (EditText) findViewById(R.id.txtView_UserName);
EditText txt_Password = (EditText) findViewById(R.id.txtView_Password);
Button btn_LogIn = (Button) findViewById(R.id.button_LogIn);
CheckBox chkbox_RememberMyPassword = (CheckBox) findViewById(R.id.checkbox_RememberPassword);
CheckBox chkbox_LogMeInAutomatically = (CheckBox) findViewById(R.id.checkbox_LogMeInAutomatically);
ProgressBar progressbar_LogIn = (ProgressBar) findViewById(R.id.progressBar_LogIn);
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
}
The program is crashing without rendering my Activity's Views. Any ideas?
I have a feeling its because you have an onClick for login, but login doesn't take a view parameter. Try setting the onclick for the button programmatically, instead of using the xml.
Edit: Try changing
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
to
public void logIn(View v) {
progressbar_LogIn.setVisibility(0);
}
or to set it programatically
btn_LogIn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//CODE GOES HERE
}
});
Further Edit:
Also noticed that theres no sign of an onCreateView so make sure that all that view code is inside of, or atleast inside of onCreate:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
}

how to assign a string value associated with an image to a textwidget when image is clicked

package com.me.trial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
OnClickListener l;
}
private void hasBeenClicked(<method invoked when the user has clicked>){
}
}
<?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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="64dp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitEnd"
android:tag="Employee name"
android:src="#drawable/img18" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I need the application to pass some string value associated to it to be passed into the text as soon as the user clicks on the image.How do i use the click Listeners in android to achieve the following task.
It is not necessary to fill in the code snippet that i have given.Any new ideas regarding the design are most welcome.
U can set text as Tag to the Image so that u can get that text onClick event. Tag can be set either in XML or dynamically.
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setTag("YOur text");
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String urText= arg0.getTag().toString();
EditText edt = (EditText)findViewById(R.id.editText1);
edt.setText(urText);
}
});
}
}
This code demonstrates how to achieve your requirement dynamically.

onActivityResult in zxing show results in a different activity

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.

Categories

Resources