Use setBackgroundResource in a other method? - android

I have the following code in my onCreate-method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView field1 = (ImageView) findViewById(R.id.field1);
field1.setBackgroundResource(R.drawable.field0);
How can I set the BackgroundResource in any other method like this:
public void setBackground() {
field1.setBackgroundResource(R.drawable.field2);
}
It says, that field1 cannot be resolved to a variable...

set field1 to global of the class, as follows:
ImageView field1=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
field1 = (ImageView) findViewById(R.id.field1);
field1.setBackgroundResource(R.drawable.field0);
}
public void setBackground() {
field1.setBackgroundResource(R.drawable.field2);
}

You need to put the view in parameter of your function :
public void setBackground(ImageView view) {
view.setBackgroundResource(R.drawable.field2);
}
and now you can call it like that :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView field1 = (ImageView) findViewById(R.id.field1);
setBackground(field1);
Anyway you should probably read some code tutorial in order to learn the basis of coding.

ImageView field1;
Set ImageView on top of the onCreate Method, because it can be used by whole of your class

Related

Set up TextView with method result

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

How to call a method in Android from onCreate

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();}
public void chustilla (View v){ //Do anything }
When I compile it gives me a problem in the parameter of chustilla(). What can I do to call this method from onCreate?
PD: If I put "this" or "null" inside the brackets it doesn't work aswell
chustilla(View) requires a View reference to be passed as a parameter so it won't work if you don't pass a View reference to it. According to what is done inchustilla(View) (best known to you) you can pass it a View from the layout (Also, best known to you).
Your method public void chustilla (View v) expects to be passed a View object. But in onCreate() You simply call chustilla(). You need pass it a view object.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(a);
}
public void chustilla (Int a)
{
//your code
}
Try the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();
}
public void chustilla ()
{
//Do anything
}
or use this for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s;
chustilla(s);
}
public void chustilla (String v)
{ //write code here }
example parameter "new View(this)" :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(new View(this));}
public void chustilla (View v){ //Do anything }

custom titlebar button in tabactivity android

Heey. How to add button in custom title bar through TabActivity ? I can set title TextView depending on which subactivity is called. But i cant figure out how to add button ?
My TabActivity:
public class TabsManager extends TabActivity {
static TabsManager mTab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTab = this;
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.tabhostbottom);
}
public void setMyTitle(String string) {
((Activity) mTab).getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
TextView textView = (TextView) findViewById(R.id.header);
textView.setText(string);
}
And my subactivity:
#Override
protected void onResume() {
super.onResume();
TabsManager.mTab.setMyTitle("Activity1");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_club);
TabsManager.mTab.setMyTitle("Activity1");
}

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

Want to Run a function without click any button when application start

I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}

Categories

Resources