My onCreate method sets the content view and sets up an image button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
But I want to create a method that changes the background of the ImageButton:
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
The problem is, the mUpdateBackground method doesn't know about the layout. If I declare the layout in the method it resets all the changes made programatically in the MainActivity.
Any ideas how I can solve this problem?
the mUpdateBackground method doesn't know about the layout
I am going to translate that to mean:
the mUpdateBackground method doesn't know about the ImageButton
If that is correct, make ImageButton mGetClickTime be a data member of your activity, instead of a local variable within onCreate(), and have mUpdateBackground() be a method on that same activity.
You need to move the ImageButton declaration to your Activity scope instead of your onCreate() method scope. Try like this:
public class YourActivity extends Activity{
ImageButton mGetClickTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
}
Related
I try to do some changes(change text, change positon ...) but it doesn't work.
For example in this function i'll change text of button correctly(i checked with getText function) but in view doesn't change anything.
When a button pressed, this function calls and change view. After this function, i want set view back.
setButton.setText("CHANGE RANGE");
setContentView(R.layout.main_layout);
I also have another layout with imageView in this main layout. I also want change that image's position.
Try this :
public class TestActivity extends Activity
{
Button setButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
setButton = (Button) findViewById(R.id.testButton);
setButton.setText("CHANGE RANGE");
}
}
setButton.setText("CHANGE RANGE"); should be called after setContentView(R.layout.main_layout);
Hope this helps.
Is there a way to get a pointer of activity which containing object of action Listener?
code snippet:
public class ActivityAsContainer exteneds Activity{
public static ActivityAsContainer _brokeTheAbstractBarier;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_brokeTheAbstractBarier = this;
setContentView(R.layout.SomeBigLie);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
brokeTheAbstractBarier.dance;
}
}
}
Is there a better way to pass the activity over to the action listener, then use a static global that could break if the object is created twice, and could a view be created twice and still live?
Just use
ActivityAsContainer.this
e.g
button.setOnClickListener(new View.OnClickListener() {
ActivityAsContainer.this.dance;
}
It should not be static.
You can use this definition in OnCreate and then use myActivity in the listener.
final Activity myActivity=this;
Hope you will be able to solve after using this line
_brokeTheAbstractBarier = YoursMainActivity.this;
instead of
_brokeTheAbstractBarier = this;
or you can use
ActivityAsContainer.this
which is
button.setOnClickListener(new View.OnClickListener() {
ActivityAsContainer.this.dance;
}
This is on create of my activity and after setting content view , I am calling LoadButton().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Loadbuttons();
}
public void LoadButtonss()
{
Here I am fetching data from database ,and plotting 50 buttons dynamically
}
So My Problem is : It is taking time in loading activity.
Any Idea how to LoadButtons() after Loading full activity. It should not wait for the ButtonLoad() function.
Any Idea how to do that?
paste this code in your activity class block:
#Override
protected void onStart() {
// TODO Auto-generated method stub
LoadButtons();
super.onStart();
}
According to the activity life cycle, you must call LoadButtons in onStart overrided method. Because activity will be visible in this state.
You could set the buttons to invisible and if you fetch the data you can update the UI through an AsyncThread. It is not the nicest way, but it works. Otherwise use instead of Buttons a CustomListView.
I would try this: Load buttons in onCreate and set View invisible. in onResume make it visible again.
View v;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v= findViewById(R.layout.main);
v.setVisibility(View.INVISIBLE);
Loadbuttons();
}
#Override
protected void onResume() {
v.setVisibility(View.VISIBLE);
super.onResume();
}
public void LoadButtons()
{
FrameLayout layout = (FrameLayout) findViewById(R.id.MarkerLinearlayout);
//Here I am fetching data from database ,and plotting 50 buttons dynamically
}
I'm changing my ContentView of the Activity at some point. (to View2).
After changing it back to View1, the listeners are not more working.
I already tried putting the Listener in the onResume() method.
Is it common anyway to use setContentView() to display e.g. a Progress screen/please wait,...(while an asyncTask is running).
Or should you only have ONE mainView for each Activity? (and replacing the content dynamically).
//EDIT: To be more specific:
I am looking for something like
LinearLayout item = (LinearLayout) findViewById(R.id.mainView);
View child = getLayoutInflater().inflate(R.layout.progress, null);
item.addView(child);
but instead of adding the "progress.xml", it should remove the current layout and ONLY show "progress.xml".
Do I need an "container" and show/hide mainView/progress?
But that doesn't seem very proper to me...
See also code below (stripped)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
}
setContentView(R.layout.view2);
[...]
setContentView(R.layout.view1);
//Listener not more working
Thank you all, for your reply. You made me realize, the onClickListeners are getting lost when I remove or replace (using setContentView()) the main view. I ended up this way now:
onCreate:
setContentView(R.layout.parse);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(getLayoutInflater().inflate(R.layout.dialog, null));
container.addView(getLayoutInflater().inflate(R.layout.progress, null));
onStartDoingSomething:
findViewById(R.id.dialog).setVisibility(View.INVISIBLE);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
onEndDoingSomehting:
findViewById(R.id.dialog).setVisibility(View.VISIBLE);
findViewById(R.id.progress).setVisibility(View.INVISIBLE);
I might change View.INVISIBLE to View.GONE, like nmr said, but since I have never used View.GONE, I have to check the Android doku first ;)
Assuming you use 'findViewById' to initialize 'button', you would need to do that every time that you do setContentView(R.layout.view1);
You can only have one UI for each Activity, you should create another activity and in its onCreate method set the content view
Steps:
Create Activity
Use the following code to set the content View:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2);
}
Define the Activity in the applications manifest as such :
Create an intent in the first Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view1);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
intent();
}
});
}
void intent(){
Intent intent = new Intent();intent.setClass(Activity1.this, Activity2.class);
startActivity(intent);
}
And there you go c:
This is my code :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(8); //GONE
setContentView(R.layout.main);
}
without the line " editTextDay.setVisibility(8); " the app doesn't crash.
any idea on what's wrong ?
you should set the content layout before any call of the method : findViewById() ;that's why it return null . So your code will work like this :
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(8); //GONE
}
Move this line setContentView(R.layout.main); after super.onCreate(savedInstanceState);
.Currently your textview is null so it is throwing exception. You first have to set view and then use findViewById method.
Put setContentView right under the super call.
You can't access views before setting the content view:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView editTextDay = (TextView)findViewById(R.id.editTextDay);
editTextDay.setVisibility(View.GONE); //GONE
}