I have huge problem. Have searching answer from many places but I do not find answer to question.
I have 2 classes in java. One is "main" and other is "menu"
On main, there is editText where person can type name and button ok.
When you press ok, I want to specific thing happen. That is where I need help. I am newcomer in Android.
I want that in other class, where will be main application and stuff, the entered name would be displayed. For example "Welcome " + name
I have tried many ways but I do not get it to work. So I want it to get one of 2 possible ways.
Set string in class 1 and then when it goes class 2, then it imports the string from class 1 to class 2 so I can use it.
I set string in class 2 and then in Class 1 I change the string in class 2, so the main 'data' string is actually in class 2, where I will continue using it if needed!
I have searched it from many places, used google and this database, but haven't found answer.
My codes are really nothing much, so no point pasting them here :).
Thanks for the help!
edit:// Ok, here are some codes then :)
package viimane.voimalus.incule;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ViimaneVoimalusActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button oknupp = (Button) findViewById(R.id.nimiOK);
oknupp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("viimane.voimalus.in" +cule.REALMENU"));
}
});
}
}
package viimane.voimalus.incule;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class RealMenu extends Activity {
EditText nimesisestus;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.peamenu);
}
}
You didn't show any code at all, so the answer won't be too detailed.
You can pass a String in the intent used to start the second activity, using putExtra method. In the second activity you may get the string by calling getStringExtra(). If you don't know much about starting activity and intents, there are many resources on the web, for example - this one.
To receive text input use EditText and to put text on the screen (that the user can't cange) use TextView.
Those are starting points. Next time I hope the question will be more focused.
Pass the string as parameter to the constructor of the menu class like this:
menu m = new menu(mystring);
then in the constructor save the atribute to your class
private String mystring;
menu(String mystring)
{
this.mystring = mystring;
}
Related
I'm trying to create a game log, to show what happened on each round of the game. The log is in another activity but I want to be updated constantly with whatever the player does. A player just presses buttons that do certain things. It's too much code to post, but I have made a new bundle, and an intent.
In MainActivity.
Bundle bundle = new Bundle();
Intent GameLogSwitch = new Intent(getApplicationContext(),GameLog.class);
I am trying to put this to send to the other activity but I don't know if you can put variables in it. otherwise it works with simple words such as ("key","It works")
GameLogSwitch.putExtra("Break","---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")");
and then of course I have this when the gamelog button is pressed
startActivity(GameLogSwitch);
Now in Gamelog.class i have this.
package com.example.adam.snookerproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GameLog extends AppCompatActivity {
private TextView GameLogText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
GameLogText = (TextView) findViewById(R.id.GameLogText);
Intent GameLogSwitch = getIntent();
String myString = GameLogSwitch.getStringExtra("Break");
GameLogText.append(myString);
}
}
I have a couple of questions. First, why does append only work once with my string when I start the activity i.e when I go back and press the same button again it won't write the same thing again underneath?
Secondly, it doesn't seem to work for my "Break" key, does this have to do with the fact that there are variables in the text I'm sending? It only works for simple text like GameLogSwitch.putExtra("key","It works");
There must be an easier way to do this! Thank you.
UPDATE 1: The answer from Drv does seem to work, but when I try to do GameLogText.append(AppConstants.log) it just replaces everything in the textview no matter how many times I press the button. I think the activity is just resetting each time I start it again. Any way around this?
Make a global string in Constants class and use it wherever you want:
public class AppConstants{
public static String log="";
}
Edit the string in the class where you are sending it in intent:
AppConstants.log="---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")";
And use it in your class as:
package com.example.adam.snookerproject;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class GameLog extends AppCompatActivity {
private TextView GameLogText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
GameLogText = (TextView) findViewById(R.id.GameLogText);
//get your log here using AppConstants.log
}
}
Try to use getContext() instead of getApplicationContext, then check your string in debug mode
Use Callback or Interface. Fire the Interface from the Main Activity and Implement the call Interface in the Activity where you want the Textview to be Updated. Inside that method Update your Text View.
Read here More about Communication between Activity Activity and Fragment Activity
(Posted solution on behalf of the OP).
I managed to append using "\n"+AppConstants.log at the end of each output.
i m programming an android program of a simple hello world in netbeans but it returns me this error...what should i do in order to rid of this error ...i m new and exited to jump into this .
here is my code...
enter code here
package com.google.haha;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
/**
*
* #author abc
*/
public class NewActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
TextView text=new TextView(this);
text.setText("hello");
setContentView(this);
}
}
When you do
setContentView(this);
this refers to the current class instance, not the TextView you just created.
Try:
setContentView(text);
Like "metter" said, the setContentView(VIEW) method expects an object of the View class (TextView, ListView, etc). So you have to pass a View element in there...
I am writing a code for going to the next page after clicking a button so I have written the code that I have mentioned below i just want to confirn that it is correct as i cant check it now, I know this is silly but I need help
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class DetailsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextScreen = new Intent(getApplicationContext(), JewelInfo.class);
// TODO Auto-generated method stub
startActivity(nextScreen);
}
});
}
}
Assuming that your JewelInfo extends from an Activty it looks fine.
I think you should use Intent nextScreen = new Intent(DetailsActivity.this, JewelInfo.class); instead of getApplicationContext()
The correctness of code is never checked just by looking at a bit of code. Syntactically, it seems to not contain an error. Semantically, how should we know? You need to define what you want to do, how you want it to look, etc. Basically, what are the requirements?
We can then validate the code against the requirements but not assess the correctness as that is only discerned by executing the code and evaluating the result of the execution on the device it is executed against the requirements.
use DetailsActivity.this instead of getApplicationContext(), and declare your JewelInfo activity on the manifest file : <activity android:name=".JewelInfo" />.Check this tutorial about how to switch between activities and pass data between them
Edit: My original question is below the line. I decided to go with a much simpler approach to setting up a button and assigning a click function. I found it at the following link. He does a good job of explaining the difference between the 2 approaches...
Android User Interface Design: Basic Buttons
I realize this is a popular question, but in all of the examples I've looked at the problem seems to be a simple detail that's been overlooked, and the detail is never the same. I'm sure this is basic. I'm just starting out with programming for Android and this is a modification of existing code.
The app has one button on a blank page, and I want the button click to send an int to my Arduino via the Amarino API. Here is my MainActivity code
package com.example.buttontest1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import at.abraxas.amarino.Amarino;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity{
private Button button;
private static final String DEVICE_ADDRESS = "00:06:66:4B:E4:23";
public Context foo1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Amarino.connect(this, DEVICE_ADDRESS);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
//Select a specific button to bundle it with the action you want
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
}
});
}
protected void onStop() {
super.onStop();
// stop Amarino's background service, we don't need it any more
Amarino.disconnect(this, DEVICE_ADDRESS);
}
}
The error I see is this, referring to line 38:
The method sendDataToArduino(Context, String, char, int) in the type
Amarino is not applicable for the arguments (new
View.OnClickListener(){}, String, char, int)
So there's a problem with the context and the method?
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this here refers toView.OnClickListener's current instance. The compilation error basically says, sendDataToArduino() expects the first argument as Context but you are passing a OnClickListener
sendDataToArduino expects its first argument to be of type Context. You are passing it a View.onClickListener. Instead of passing this as the first argument, try setting up a context as mentioned here and pass that as the first argument.
Try adding the following in your onCreate method after the super call:
MainActivity.context = getApplicationContext();
Also add the following method after onCreate:
public static Context getAppContext() {
return MainActivity.context;
}
Now call the method with:
Amarino.sendDataToArduino(getApppContext(), DEVICE_ADDRESS, 'j', 1);
Teh api expects the object of Context but you pass this to:
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this is not an object of Context instead it is OnClickListener object.
What I want is that when Activity starts first time, it first checks if nimiolemas is true. Since it just starts, then it can't be true. So, it will automatically starts new activity and also asks to get me info. In activity 2 person can type their name and when they press Ok, info will be sent back to activity one. Now, I don't know really how to change there Boolean to true and send that as well, so for now I told to change nimiolemas true before launching activity 2.
After pressing ok, it sends back to activity one and does the check again. Since it should be now true and also able to retrieve information about persons name, then it will go to true condition and print that name on screen in first activity. Now whenever program is launched, it will skip asking name and will straight show the person name :).
But it doesn't work exactly as I want. Before i put boolean, it actually went to second activity, but I couldn't get data so well. I have been working on solution for too long and I really appreciate help. If I find mistakes I can study from that more then searching on solution all over internet for next 10 hours :(.
I might have made some things very wrong, so please let me know and teach me! I really want to get better in this! So far I have done:
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainStuff extends Activity {
String tyybinimi;
TextView tere;
Boolean nimiolemas;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.TERE);
Intent i = new Intent(this, nimekysija.class);
tyybinimi = i.getStringExtra("nimi");
if (nimiolemas = true) {
System.out.print(tyybinimi);
} else {
startActivity(i);
nimiolemas = true;
finish();
}
}
}
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
Intent resultIntent;
EditText nimi;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
Button kysOk = (Button) findViewById(R.id.bNimekysija);
nimi = (EditText) findViewById(R.id.etNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
String nimiS = nimi.getText().toString();
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.putExtra("nimi", nimiS);
startActivity(new Intent("viimane.voimalus.MAIN"));
finish();
}
});
}
}
Instead of using a boolean, you should be using Shared Preferences.