Set up TextView with method result - android

I'm a beginner with Java and am stuck in one place. I have a method like this in class 1
public String saySomething()
{
return "blabla";
}
Then in Main:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tw = (TextView) findViewById(R.id.text1);
tw.setText(saySomething());
}
}
I try set return from this method to TextView. Have no idea how but wanted to show what i want to do. How to do it properly?

In short, put saySomething() method in your Activity class or generate an instance of the class hosting the method and call it:
ClassName1 className = new ClassName1();
TextView tw = (TextView) findViewById(R.id.text1);
tw.setText(className.saySomething());

Related

How to send array of objects to activity

I read some example on this website and other but still have error.
It's not a compilation error but my application crash when I click on the button.
There is code of my MainActivity.java (only interesting part) :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//blablabla
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myLayout= (LinearLayout) findViewById(R.id.layoutProp);
Button button = (Button) findViewById(R.id.buttonValider);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text); // this one does not work
intent.putExtra(EXTRA_NUMBER, nbTextView);
startActivity(intent);
}
}
And code in Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
int nbTextView = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0);
MyTextView[] text = (MyTextView[]) intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.TVtest);
textView1.setText(""+ nbTextView);
} }
if I comment : MyTextView[] text =(MyTextView[])intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
the application does not crash.
Thank you all very much for your help
You cannot pass views through intents. Views are part of the layout and cannot be shared between activities.
If you want to pass array with custom objects you must implement a Parcelable or Serializable class and use:
getIntent().getParcelableExtra("array");
or
getIntent().getSerializableExtra("array");
Parcelable class will be faster but it is harder to implement.
Another way is to use a library like Gson to parse your data to Json and pass it as a string.

Android Intents to pass data

I don't know why, but this isn't working. Can anyone see anything wrong with my code.
I have two activities and I'm passing data from two
public class InputActivity extends AppCompatActivity {
EditText number1EditText;
EditText number2EditText;
Button addButton;
InputActivity code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
number1EditText = (EditText)findViewById(R.id.editText);
number2EditText = (EditText)findViewById(R.id.editText2);
addButton = (Button)findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(), AddActivity.class);
mIntent.putExtra("number1", number1EditText.getText().toString());
mIntent.putExtra("number2", number2EditText.getText().toString());
startActivity(mIntent);
}
});
}
AddActivity code looks like this
public class AddActivity extends AppCompatActivity {
TextView answer;
double y=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
answer =(TextView)findViewById(R.id.textView);
String value1 = getIntent().getExtras().getString("number1");
String value2 = getIntent().getExtras().getString("number2");
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
}
Just looking quickly:
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
You're passing a int to the method setText() which happens to have an overload that receives a int argument for the cases when you pass the reference from a String in some xml. You may be getting a ResourceNotFoundException.
If you want to show a text with the sum between your values:
answer.setText(String.valueOf((int) (Double.parseDouble(value1)+Double.parseDouble(value2))));
Just keep in mind that there are a lot of checks you have to do first, you may get another exceptions doing this kind of parse, like NumberFormatException for instance.

Why does my code not work?

I have the following code to get information from a rating bar and put it into a TextView but it doesn't seem to be working:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ratingText = (TextView) findViewById(R.id.ratingText);
RatingBar rating=(RatingBar)findViewById(R.id.rating);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
public void onRatingChanged(RatingBar ratingBar,float rating, boolean fromUser){
ratingText.setText(""+this.rating.getRating());
}
At the second last line of code it is giving me the following error: "rating cannot be resolved or is not a field" and "ratingText cannot be resolved". I am wondering why I am getting these errors and how to fix them. Thanks.
a-) I think your problem is that you need that your class MainActivity implements OnRatingBarChangeListener:
public class MainActivity extends Activity implements OnRatingBarChangeListener {
}
b-) Another possible error is that you need to make sure that you do NOT have this in your imports:
import android.R;
but:
import your.application.packagename.R;
c-) Declare the TextView ratingText and the RatingBar rating outside of the method, as an attribute of the class.
Hope it helps!
Take a look at this for a solution.
r.string error "cannot be resolved or is not a field"
Main idea is to make sure your R reference is correct, and try by deleting the gen files and rebuild the project.
EDIT::
This was incorrect the problem was stated in another answer. by #Marv
It is because you create your ratingText and rating in the onCreate method. This means they are only available inside this method.
Try to do something like this:
private TextView ratingText = null;
private RatingBar rating = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingText = (TextView) findViewById(R.id.ratingText);
rating=(RatingBar)findViewById(R.id.rating);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
This way they are available in the entire class and you can use them in onRatingChanged.
try this.
public class Test extends Activity implements OnRatingBarChangeListener{
TextView ratingText;
RatingBar rating;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.info);
ratingText = (TextView) findViewById(R.id.textView_aaa);
rating=(RatingBar)findViewById(R.id.ratingBar1);
rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
ratingText.setText(""+this.rating.getRating());
}
}

Making ImageView invisible from method

I have created HideImages() function as shown below. The problem is, that running this code causes NullPointerExcpection. When I comment out the setVisibility lines, it works fine. What am I doing wrong?
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
ImageView img_w0 = (ImageView)findViewById(R.id.img0);
ImageView img_w1 = (ImageView)findViewById(R.id.img1);
ImageView img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
Make all the references of ImageView as Global as
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
I think this may case the problem because you had already initialize ImageView above the onCreate() method the why you declare here,
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
The problems is here. You're already declared the ImageView objects as globally. And, again you're declaring internally in onCreate()
So, just remove the declaration inside of onCreate() and run. Like below -
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
}

Assigning Values to a TextView from a different class

TestaActivity.java
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
}
}
Print.java
public class Print {
public Print(Context tempContext) {
//I want to assign the value to the tvText from here
}
}
In the above example, as you can see I have set the text in tvText to "Sample". In the same way, I need to assign the textView1 ID with some value inside Print class, once it is created.
Please help me to figure out the way to do it.
If your class Print is instantiated when TestaActivity is on the screen, then you can get tvText reference, passing to Print in some way a TestaActivity reference.
Maybe you could pass it via constructor:
From TestaActivity you do:
Print print = new Print(this);
where this represents the instance of TestaActivity.
And then in your Print code you can do:
TextView tvText = (TextView)((TestaActivity)context.findViewById(R.id.textView1));
tvText.setText("Sample");
Another solution is provide an interface from TestaActivity, transparent for the outside, which manage your changes on the textview (or whatever).
Something like that:
private TextView tvText;
public void setTvText(String str){
tvText.setText( str );
}
And then in your Print class:
((TestaActivity)context).setTvText( "Sample" );
try as:
public class Print {
protected TestaActivity context;
public Print(Context tempContext) {
context = tempContext;
}
public void changetextViewtext(final String msg){
context.runOnUiThread(new Runnable() {
#Override
public void run() {
//assign the value to the tvText from here
context.tvText.setText("Hello Test");
}
});
}
}
and call changetextViewtext from Activity for Changeing TextView Text from Print Class
public class TestaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.textView1);
tvText.setText("Sample");
Print myPrint = new Print(this);
myPrint.changetextViewtext("Hello World !!!");
}
}
as your need!!!!:)
#imran - the solution is correct except that you would want to pass the TextView as an argument in the constructor or the method.
Harcoding TextView in a method is bad because you cannot be reuse it.

Categories

Resources