I've implemented the Google Play Services demo code found at this url:
https://github.com/kpbird/google-play-service/blob/master/src/com/kpbird/googleplayservice/MainActivity.java
And everything works. However if the function getAccountNames() is called within onCreate, the application momentarialy flashes before loading the set content view. It's like there are two activity views are being initiatied.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAccount = (TextView) findViewById(R.id.txtAccount);
txtToken = (TextView) findViewById(R.id.txtToken);
getAccountNames();
}
Why would placing getAccountNames() in the onCreate create this visual effect when the application launches.
Related
I was developing an exam score calculator app.
When I want to call AD methods,advertisements don't show up.
Calculation process happens in OnCreate method:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
/*Calculation...*/}
and other voids like:
public void requestAd() {
/*AD RQUESTING PROCESS...*/
}
and
public void showAd() {
/*AD SHOWING PROCESS...*/
}
AD team gave me this code to call the method and it works well:
requestButton.setOnClickListener(v -> requestAd());
showButton.setOnClickListener(v -> showAd());
But the Problem is I don't have buttons to call them so I tried this:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
requestAd();
showAd();
/*Calculation...*/}
But when the activity starts ads don't show up!
The whole question is I want this methods to be called while this activity starts.
thank you.
Try building up the release version APK and test on it. Maybe your Ad-provider have some restrictions in debug version?
I made another class and moved request Ad and showAd there. Then, I made an object and called the method through object.
I have to mention that I changed a minor thing in requestAd but the main job was done by the object.
Thank You All.
When I try to follow the guide of Cordova to embed it into a native android application I can't build.
I use this guide:
https://cordova.apache.org/docs/en/latest/guide/platforms/android/webview.html
When I put everything in my main activity I receive an error on config.
It says can't resolve symbol 'config'. I have no clue what I am doing wrong or how to solve it.
onCreate
/* Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
config.init(this);
cwv.loadUrl(config.getStartUrl());
}
I need to execute a method before initiating the layout in an activity. If I call the method I need to execute inside onCreate(), would it be executed before the layout is set?
The reason is because I need the method to return a piece of information that is displayed in the layout before initiating it. Would love some feedback on this.
You can do whatever you like before setContentView like so:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int i = 0;
setContentView(R.layout.main);
}
}
As long as you do not interact with views that have not been inflated yet
For example this is an error:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ERROR, CAN'T TOUCH UI ELEMENTS
ImageView img = (ImageView)findViewById(R.id.img);
setContentView(R.layout.main);
}
}
Default activity created with Android Studio contains following code
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here is code, that executed before layout inflated
setContentView(R.layout.example_activity); //This line inflates layout
}
BTW, you can even remove setContentView and inflate layout programmaticaly.
Do it in onCreate(), preferably before calling setContentView().
However, if the data you want to receive comes from the network, then it will be obtained on a separate Thread (as no network calls can be done on the main Thread). In this situation the layout will almost certainly display before the data is obtained.
A solution would be to obtain the piece of data before you start the Activity, pass it in the Intent as extra and then retrieve in onCreate() using getIntent().getStringExtra()
You are probably inflating your layout in Activity.onCreate() with setContentView(), so you need to put your function call in that method before the call to setContentView().
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
yourFunctionCallHere();
setContentView(R.layout.act_main);
}
I created a map with Google Maps that has symbols on it : https://www.google.com/maps/d/u/0/viewer?mid=1IOw_y51yt8YaQeTzgRvoEhn5drk&ll=32.11239510425183%2C34.80320922355645&z=17
I would like to put this map in my android application, is there a way to do so? everything I saw online was to create a map and add markers, but if I already have a map how can I put it?
You can make a WebView in your Layout, and just load the url in it.
public class Main extends Activity {
private WebView mWebview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.loadUrl("https://www.google.com/maps/d/u/0/viewer?mid=1IOw_y51yt8YaQeTzgRvoEhn5drk&ll=32.11239510425183%2C34.80320922355645&z=17");
setContentView(mWebview);
}
}
When an app first launches it says the app name when in talk back mode. Afterwards i'd like to to say "please wait" automatically without user pressing any button. How can this be achieved ?
here is my activity code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}