How to execute code after Activity creation in Android? - java

I want to execute some code immediately after the form is shown.
I want to check the size of a button on the form and use the same size to create a new button at runtime.
I tried onStart() and onResumed(), but they do not work.
Thanks,
Ashok

You can add a globallayout listener. Add the listener at onActivityCreated.
Check this example:
button1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
button1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
button1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//here the size is already available. create new button2 here with the size of button1
}
});

you are looking at the onCreate() method, that is the function that will run when your app is first loaded in to memory

Related

Dynamically Add and Save buttons in Android studio

Currently i managed to create the buttons dynamically on a view
Here is the my code to create the buttons;
public void Add_on(View v) {
AlertDialog.Builder mbuilder = new AlertDialog.Builder(Mb.this);
View mview = getLayoutInflater().inflate(R.layout.activity_mb1, null);
EditText number = (EditText) mview.findViewById(R.id.etnum);
Button Create = (Button) mview.findViewById(R.id.etcreate);
Button Cancel = (Button) mview.findViewById(R.id.etcancel);
Create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if (!number.getText().toString().isEmpty())
{
Toast.makeText(Mb.this, "Number can be NULL",Toast.LENGTH_SHORT).show();
LinearLayout yenilayout = new LinearLayout(Mb.this);
int n =1;
for(int i=0; i<n; i++)
{
Button yeniButton = new Button(Mb.this);
yenilayout.addView(yeniButton);
yeniButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Mb.this, "Button is working",Toast.LENGTH_SHORT).show();
}
});
}
altlayout.addView(yenilayout);
} else {
Toast.makeText(Mb.this, "Number cannot be NULL",Toast.LENGTH_SHORT).show();
}
}
});
But whenever i recall the activity, the buttons are no longer exist. So May i know can i place the button there permanently?
Thank you for suggestions
You can use Bundle to save an activity's state and recreate it in the onCreate() method. This works for a particular instance of Activity, so can be used to save data concerning selection, or user input etc., but not data that you need to be persistent across application launches.
To use the Bundle, override the onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) methods in the Activity class. You can use methods from Bundle to save whatever data you like in a map, and get it back in onRestoreInstanceState(Bundle), which is called in onStart().
The default implementations already handle most UI stuff though, and I would have thought this would keep track of your buttons for you, so it may be that your question is actually about associating some persistent data with your application. (this also means that if you do override the above methods, you should make sure to call the super methods in the first line of your implementation).
If you need persistent data across application launches, then the quickest and easiest way would be to use SharedPreferences, see this answer for an example.

Can't re-enable a view after disabling it with CheckBox

I'm trying to enable/disable a seekbar when a checkbox is checked(or not).
I'm using this to create and refer:
CheckBox checkBoxProva = (CheckBox) findViewById(R.id.checkbox_prova);
boolean varCheckBoxProva = checkBoxProva.isChecked();
And this simple if to disable/enable the view:
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}
All this is inside the onCreate.
When the app starts the seekbar is disabled (so the if works), but if I check the CheckBox it doesn't change to enabled.
EDIT: I've succed thank to the reply of #rohan bhatia.
You are doing all of this in your onCreate method that only gets called when the activity is first created. You need to setup a listener that will be fired when the checkbox is clicked. See: https://developer.android.com/guide/topics/ui/controls/checkbox.html
You need a Listener that notifies when a checkbox is selected or deselected..
checkBoxProva.setOnCheckedChangeListener(new new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
seekBarProva.setEnabled(isChecked);
}
}
);
Accordingly, whenever you click the checkbox the method onCheckedChanged() will be executed.. You will still need to specify this in onCreate -
seekBarProva.setEnabled(checkBoxProva.isChecked());
As onCheckedChanged() only listens for changes in checked state, so to specify initial stage you will need that above line too.
Simply insert an else block
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}else{
seekBarProva.setEnabled(true);
}
Alternative solution:
Personally, I'd recommend adding a listener to the checkbox inside your onCreate method.
checkBoxProva.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(varCheckBoxProva){
seekBarProva.setEnabled(true);
}else{
seekBarProva.setEnabled(false);
}
}
});

Start activity after finish current

I need to complete three task in one button click. its look like on button click called SaveQuote
1) Hide Adview
2) Make Screenshot of Layout and Save it
3) Show Adview
Now I have implemented method for do above three task in my java as like below
else if(menuItem.getItemId() == R.id.save_image) {
adView.setVisibility(View.GONE);
saveQuote();
adView.setVisibility(View.VISIBLE);
But I am facing issue that ad always staying visible. If I use on ViewGone method and saveQuote method than its working fine, but if I add VISIBLE method than its not hiding my adview.
Note : I am not getting any error for same. I just need to know how can I achieve above three task via one button click
My saveQuote method is like below
private void saveQuote(){
String id=getQuote(mItemIndx).get(KEY_ID);
View v1=null;
List<Fragment> activeFragments=getSupportFragmentManager().getFragments();
for(Fragment fragment:activeFragments){
QuoteCard cardFrag=(QuoteCard)fragment;
if(cardFrag!=null&&cardFrag.mId.equals(id)){
v1=cardFrag.getCardView();
}
}
TextView textView = (TextView)v1.findViewById(R.id.textAuthorSign);
textView.setVisibility(TextView.VISIBLE);
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ImageLoader.getInstance().saveQuoteImage(bitmap);
Snackbar.make(v1,"Quote Saved",Snackbar.LENGTH_SHORT).show();
adView.setVisibility(View.VISIBLE);
}
I am getting error like below if set VISIBLE method on end of saveQuote
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.NativeExpressAdView.setVisibility(int)' on a null object reference
Thanks
You can do something like this.
adView.setVisibility(View.GONE);
saveQuote();
now in your saveQuote() method after all your logic is set.. add last line..
adView.setVisibility(View.VISIBLE); make note that your adView should be defined globally and you have initialized your adVIew.
You should review "Handle,Looper,Message"
adView.setVisibility(View.GONE);
saveQuote();
adView.post(runable)
Runnable runable=new Runnable(){
#Override
public void run(){
adView.setVisibility(View.VISIBLE);
}
}
Declare as global
final Handler handler = new Handler();
Change your code like this
else if(menuItem.getItemId() == R.id.save_image) {
adView.setVisibility(View.GONE);
saveQuote();
handler.postDelayed(new Runnable()
{ #Override public void run() {
adView.setVisibility(View.VISIBLE); } }, 5000);
}
Here the delay is 5 s reduce the delay if don't need that much.

How can i stop sending messages which are sending in loop on click of an button in android

I want to know how can I stop message sending in a loop.
When I press stop, messages are still sent.
I have provided a stop() method.
for (i = 0; i < copy; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(stop!=1)
sms.sendTextMessage(number,null,message,sentPIn,pdelint);
}
}, 25000);
stop method
int stop=0;
public void st(View view){
Button b=(Button)findViewById(R.id.button2);
b.setText("Stopped");
stop=1;
}
Modify St function to this
public void st() {
Button b = (Button) findViewById(R.id.button);
#Override
b.setOnClickListener(new View.OnClickListener() {
b.setText("Stopped!");
stop=1;
});
}
The problem was u were not using an listener to detect button press. It is not advisable to change the button text use a text view instead!
Most likely he has set onClick attribute in the xml, although only he'll be able to clarify that.
I'm saying that because I see View as a parameter in the method st(View v).
Although if the attribute onClick is not set in xml #Audi 's solution would be good. Though on the contrary if the attribute has been set in the xml then
you should check if view.getId() == R.id.button then apply a button cast to your view and set its text to "Stopped".

Android View onResume method?

When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}

Categories