Search from String? - android

I have an application that I am trying to get to got to a specific part of a website. When the user enter the text inside of the EditText and hits the Button I want the application to take that string and enter it into the specific part of the url that I want it to go into.
First Activity
private EditText text;
private Button search;
text = (EditText) findViewById (R.id.text);
search = (Button) findViewById (R.id.Search);
search.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (FirstActivity.this,SecondActivity.class);
intent.putExtra("string", text.getText().toString());
startActivity(intent);
}
});
Second Activity
WebView mWebView;
private EditText string;
private String string1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
string = (EditText) findViewById (R.id.string);
string.(getIntent().getExtras().getString("string"));
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com/"+string);
}
}
Any help will be appreciated thanks.

You're almost there.
string1 = getIntent().getExtras().getString("string", "");
Later edit: the name of the String variable is string1, not string. You have to change your last line, too:
mWebView.loadUrl("http://www.example.com/"+string1);
Consider using less confusing, more descriptive variable names. It will help a lot on the long run.

Related

Making a button to show a toast and sending a data to another activity

I am trying to send data from one activity to another and also to show toast when the button is pressed I am trying to accomplish both at same time.The id of the button I have assigned is add_to_cart and I tried this code below and i didnt work
Button customToastButton = (Button) this.findViewById(R.id.add_to_cart);
customToastButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.cart_toast, (ViewGroup)
findViewById(R.id.toast_layout_root));
//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("Item as been added to cart");
//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_SHORT);
t.setView(layout);
t.show();
Bundle intent = getIntent().getExtras();
if (intent != null) {
firstname= intent.getString("Gladwin");
secondname= intent.getString("james");
}
}
});
The toast part works fine but not the data sending part and I tried Implementing separately this code to send the data but was not responsive and below is the code I am using to receive the data sent
private String firstname;
private String secondname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);
and this is the XML part of the receiving end
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
<TextView
android:layout_below="#+id/text1"
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
I know there a are lot of newbie mistakes sorry for that tho
So, let´s assume you have opened the Activity that should receive the intent before. The problem here is, that if you leaving this activity and does not finish it, it´s still alive as long as the system needs not much resources. So, then you are going to the next activity where you send the intent and start th receiving activity again. But here is the trick: the activity just comes back to screen, but had never died. So the intent is not received. Do it this way
Sending the intent:
public class SendingActivity extends Activity {
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
Intent testIntent = new Intent(SendingActivity.this, ReceivingActivity.class);
testIntent.putExtra("firstname","Eggsy");
testIntent.putExtra("lastname","Iggsy");
startActivity(testIntent);
}
});
}
Receiving intent in the activity and finish after leaving:
public class ReceivingActivity extends Activity {
//make TextView objects
private TextView txtFirst;
private TextView txtSecond;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activty_xml);
//initialize textViews <-- this has to be done with all views
txtFirst = (TextView)findViewById(R.id.yourFirstTextViewId);
txtSecond = (TextView)findViewById(R.id.yourSecondTextViewId);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
#Override
onBackPressed(){
finish();
super.onBackPressed();
}
}
From updated question, I think you have a general problem with understanding the order of activities lifecycle. OnCreate() will be called on start of activity and the views are set. The String variables that you set to the textView will not changed automatically, you´ll have to do it in your button click method. So, above example should be added to your onClick if you don´t want to do it at the start directly:
#Override
public void onClick(View v){
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
To get data from other activity:
Intent intent = new Intent(currentactivity.this, targetactivity.class);
intent.putExtra("XXX", value);
You need a String value, in this case: text and a string name in this case: uid
Then to get the uid in another activity:
String uid = getIntent().getExtras().getString("XXX");
In data receiving side Do like this....
private String firstname;
private String secondname;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
firstname= bundle.getString("firstname");
secondname= bundle.getString("Secondname");
}
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);

Retrieving multiple information for new activity

I'm new to Android developing programming. I'm trying to pass through multiple information from my starting activity to a new activity and i can't figure out how to retrieve all of the values.
My MainActivity buttonSend event code is this:
public void sendInformation (View view) {
Intent intent = new Intent(this, DisplayInformation.class);
EditText toText = (EditText) findViewById (R.id.toText); //Finds the control 'toText' in the form and gets it by ID
EditText fromText = (EditText) findViewById (R.id.fromText); //Finds the control 'fromText' in the form and gets it by ID
EditText messageText = (EditText) findViewById (R.id.messageText); //Finds the control 'messageText' in the form and gets it by ID
EditText subjectText = (EditText) findViewById (R.id.subjectText); //Finds the control 'subjectText' in the form and gets it by ID
String toMessage = toText.getText().toString(); //Gets the text that you enter for the 'To'Field
String fromMessage = fromText.getText().toString(); //Gets the text that you enter for the 'From' field
String messageMessage = messageText.getText().toString(); //Gets the text that you enter for the 'Message' field
String subjectMessage = subjectText.getText().toString(); //Gets the text that you enter for the 'Subject' field
intent.putExtra(EXTRA_MESSAGE, toMessage);
intent.putExtra(EXTRA_MESSAGE, fromMessage);
intent.putExtra(EXTRA_MESSAGE, messageMessage);
intent.putExtra(EXTRA_MESSAGE, subjectMessage);
startActivity(intent);
}
EXTRA_MESSAGE is defined as public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE"; I don't really know what this does to be honest the tutorial just told me to include it.
In my second activity page i have this code for the onCreate instance:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_information);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String toMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String fromMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String subjectMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String messageMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView toMessagetextView = new TextView(this);
toMessagetextView.setTextSize(40);
toMessagetextView.setText(toMessage);
TextView fromMessagetextView = new TextView(this);
fromMessagetextView.setTextSize(40);
fromMessagetextView.setText(fromMessage);
TextView subjectMessagetextView = new TextView(this);
subjectMessagetextView.setTextSize(40);
subjectMessagetextView.setText(subjectMessage);
TextView messageMessagetextView = new TextView(this);
messageMessagetextView.setTextSize(40);
messageMessagetextView.setText(messageMessage);
setContentView(toMessagetextView);
setContentView(fromMessagetextView);
setContentView(subjectMessagetextView);
setContentView(messageMessagetextView);
}
When I go to run my app it errors out and shuts down.
*Also if anybody could tell me how to put the TextView in the fragment.xml file for the second activity it would be greatly appreciated. I know how to declare the id and what not but i don't know to use it with retrieving data from another activity yet.
Thank you in advance for any help.
You are using same key EXTRA_MESSAGE for all extra values. Intent stores values like Map.
So here you are loosing all the value other than subjectMessage, because it was added at last.
You have to use different keys for each values. like
public final static String EXTRA_MESSAGE_TO = "com.example.Fun.MESSAGE_TO";
public final static String EXTRA_MESSAGE_FROM = "com.example.Fun.MESSAGE_FROM";
public final static String EXTRA_MESSAGE_SUBJECT = "com.example.Fun.MESSAGE_SUBJECT";
public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE";

Getting android the method findViewbyId(int) is undefined for the type QuoteFormActivity?

I don't see why i get the error, findViewById() is owned by the Activity class, right? So extending it would be all it takes.
For quoteText and btnSendQuote i get the error messages, how can fix this and what is the cause?
public class QuoteFormActivity extends Activity {
//VARIABLES from FORM
EditText quoteText;
Button btnSendQuote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_form);
quoteText = (EditText) findByViewId(R.id.et_quote);
btnSendQuote = (Button) findByViewId(R.id.btn_send_quote);
// Show the Up button in the action bar.
//setupActionBar();
}
ps: already made a clean buitl
Its findViewById not findByViewId
change:
quoteText = (EditText) findByViewId(R.id.et_quote);
btnSendQuote = (Button) findByViewId(R.id.btn_send_quote);
to:
quoteText = (EditText) findViewById(R.id.et_quote);
btnSendQuote = (Button) findViewById(R.id.btn_send_quote);

Pass value obtained from EditText box to a TextView in the next screen

I have two java classes, HelloAndroidActivity and GetTasks. I want to try to get the text from the Edit Text box from the first activity on clicking the button and get that value in the next activity GetTasks and display it in the text view. My code is as shown:
HelloAndroidActivity
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
Intent i = new Intent(HelloAndroidActivity.this, GetTasks.class);
//i.setClass(HelloAndroidActivity.this, GetTasks.class);
EditText taskname = (EditText) findViewById(R.id.task_name);
String task_name = taskname.getEditableText().toString();
Log.d("Task Name", task_name + "");
i.putExtra("taskname", task_name);
startActivity(i);
}
});
GetTasks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page_layout);
CharSequence task_name = (CharSequence) findViewById(R.id.task_name);
Log.d("Here", task_name + "");
Intent i2 = getIntent();
taskname = i2.getStringExtra("taskname");
TextView text = (TextView) findViewById(R.id.gettaskname);
text.setText(taskname);
}
Can you tell me what am I doing wrong? My application force closes itself. Instead of passing a variable, if I pass a string variable, I am able to see that in the text view? Does it have to do with the manifest file? I have an intent for both the activities. Any help regarding this would appreciated.
I believe that the line
CharSequence task_name = (CharSequence) findViewById(R.id.task_name);
is the most likely culprit. CharSequence should replaced with whatever type of a view the task_name element is...
Instead of (CharSequence), use (EditText), that is the type of layout object your are retrieving the data from, as defined your layout XML file.
In your GetTasks.onCreate method, you need to bring in the values that you passed from the intent in the HelloAndroidActivity.
You do that like this:
Bundle extras = getIntent().getExtras();
if (extras ==null) { return;}
String taskname = extras.getString("taskname");
See the following link for a good tutorial on using intents

android tab pass intent extras to new tabs activity

I have read through the similar questions but do not see one like this. I have a simple calculator application there are two tabs. Each has their own activity class. I initially wrote this with a button on the first screen which onClick would take the inputs and pass them to the results screen which would do some calculation and then display the results. Now I want to do it with a TabHost. I have the two screens all set, but no idea how to take the inputs and pass them to the results activity to do the calculations and display the resulting values.
Thanks in advance
Dean-O
The most natural way to do this would be to provide your own subclass of android.app.Application and use it to store the shared data. Then the first tab would set the values in the data structure, and the second tab would read them and use them to perform whatever calculation you wanted to do. See here: How to declare global variables in Android?
Assuming you don't want to take this approach and really want to use Intent extras to pass the data between Activities within a TabHost, you could do something like the following hack where you use the TabHosts Intent (accessed via getParent().getIntent()) to pass data back and forth.
public class Tab1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_one);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
EditText x = (EditText) findViewById(R.id.x);
EditText y = (EditText) findViewById(R.id.y);
int a = Integer.parseInt(x.getText().toString());
int b = Integer.parseInt(y.getText().toString());
Intent i = getParent().getIntent();
i.putExtra("a", a);
i.putExtra("b", b);
i.putExtra("tab", 1);
TabActivity ta = (TabActivity) Tab1.this.getParent();
ta.getTabHost().setCurrentTab(1);
}
});
}
}
public class Tab2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView result = new TextView(this);
Intent i = getParent().getIntent();
int a = i.getIntExtra("a", 0);
int b = i.getIntExtra("b", 0);
int sum = a + b;
result.setText(Integer.toString(sum));
setContentView(result);
}
}

Categories

Resources