Android ViewPager guideline - java

I'm working on a new application in which i want create a intro guideline or wizard or something like tha with a viewpager style. I need display this "Intro" part only the first time i open the application and then never more. In the last tab i want a button and when tapped it brings me to the real MainActivity in which execute the operations. I'm able to create the ViewPager following this example ViewPager tutorial but i can't understand how pass from those fragments to my MainActivity and never more display those tabs. I don't know if my question is clear. I can create a button in the last fragment and onClick open the New activity.. But what about never more display the "tutorial" intro? Thanks
EDIT with button:
startAppBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent startApp = new Intent(getActivity(), MainActivity.class);
startActivity(startApp);
}
});

i think you should use SharedPreferences to save a boolean flag that your first activity "the guideline" activity have been displayed .
and on your button click set the value to true like the following :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyApplicationPref", Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putBoolean("firstTime" , Boolean.TRUE);
editor.commit();
and in your "guideline" in your onResume method do the following :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyApplicationPref", Context.MODE_PRIVATE);
boolean firstTime = pref.getBoolean("firstTime", false);
if(firstTime == true){
Intent myIntent = new Intent(getApplicationContext() , MainActivity.class);
startActivity(myIntent);
}
Please give me some feedback .
Hope That Helps .

Related

Save settings button and back to previous activity

I have an activity that has edit text and you can type your name in it, and when you click Save button you will be redirected to MainActivity, but I don't want to open MainActivity by Intent, I make that save button save your name with shared prefs and everything works fine but I don't want to open my Main in Intent I want that when I clicked on save button the current activity that saves data close and previous activity open.
sorry for my bad English.
this is my code for save button
submitButt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == submitButt.getId()) {
String name = inputName.getText().toString().trim();
if (isValidInput(name)) {
Intent setint = new Intent(SettingsActivity.this, MainActivity.class);
setint.putExtra("name", name);
saveData();
startActivity(setint);
Toast.makeText(SettingsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();
}
}
}
Then you just open the PreviousActivity instead of the MainActivity. Add a checker inside the PreviousActivity if "name" has data or not. You can also use finish(); this will kill the activity.
** Update **
In your MainActivity, let's say you have TextView name. Add:
#Override
public void onResume(){
super.onResume();
// Check your variable if it has value or none
textViewName.setText(variableForSharedPref)
}
Then you can proceed with usual process to go in Settings and Go Back to MainActivity.

Android toasts sometimes not showing when the app was started for the first time

I understand that the title might sound a little bit misleading, but it will make sense in a second.
When does the problem occur?
The toasts are shown always, but not at the first time when the user launches the app. When the app is fired for the first time an App Intro is being displayed, In my MainActivity i check my SharedPreferencesand start an Intent which holds the AppIntro when the user visits the app the first time.
I show a quick Intro and also ask the User for the Storage Permission Then my SearchFragment in the corresponding ViewPager is shown.
When and where are the Toasts being fired?
When the user clicks on a Button, I call the method showInfoToast() where I pass the Context and the String as Argument and then my Toasts are being shown. I use the Library Toasty from GrenderG but it doesn't make any difference with the default Toast.
public static void showInfoToast(String infoMessage, Context context) {
Toasty.info(context, infoMessage, Toast.LENGTH_SHORT, true).show();
}
What have I tried / what do I think the problem is?
The Context passed to the method. As said I call the method showInfoToast in my onClickListener in the SearchFragment I pass getContext() But it seems not to be the problem, as I have seen when I stepped over it with the Debugger, Context is from the MainActivity.
The Library Toasty but the same error occures in the default Toasts
That's why in my opinion somehow the App Intro Intent messes up the Context/The SearchFragment the problem has to be there as it only happens on the first launch of the app.
Code snippet onClickListener in Search Fragment:
goViralButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
synchronized (this) {
showInfoToast("Please add users", getContext());
}
}
});
Code snippet App Intro launched by the MainActivity:
// Initialize SharedPreferences
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
// Create a new boolean and preference and set it to true
boolean isFirstStart = getPrefs.getBoolean("firstStart", true);
// If the activity has never started before...
if (isFirstStart) {
// Launch app intro
final Intent i = new Intent(MainActivity.this, IntroActivity.class);
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(i);
}
});
// Make a new preferences editor
SharedPreferences.Editor e = getPrefs.edit();
// Edit preference to make it false because we don't want this to run again
e.putBoolean("firstStart", false);
// Apply changes
e.apply();
}
Logcat output when I press the goViral Button:
Unable to start animation, surface is null or no children.
Toast already killed
...

Check activation code on first app run

I created an android app that when it runs for the first time an activation page loads with these codes .
Button btnInput = (Button) findViewById(R.id.send);
btnInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText m = (EditText) findViewById(R.id.editText);
String strInput = m.getText().toString();
if (strInput.equals("123") || strInput.equals("456"))
startActivity(new Intent(ActiveCode.this, MainActivity.class));
else
startActivity(new Intent(ActiveCode.this,ActiveCode.class));
finish();
}
});
}
And Main activity code like this :
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
startActivity(new Intent(MainActivity.this,ActiveCode.class));
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit();// Very important to save the preference
}
}
the problem is that when on first run user closes the app completely without entering code and open it again they don't need to enter code and it goes directly to main activity , so basically it's a bug of my app .
I want help to change codes in a way that activation page disappear only when user enters code.
Try to initialize preference like this,
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
instead of,
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Because your preference is not binding with your activity.

Call View from separate Activity

I have 2 Activitys "MainActivity" and "MainActivity2". One Admob-Banner is shown in both Activitys at the bottom.
My Problem: I want to disable both Banners by pressing a button in my MainActivity. But as I am new to android and development in general I lack in experience. I search the internet but could not find a valid answer for my problem.
My Question: Is there a way to link both Ad-ids from separate Activitys in my method and what would be the best approach?
This is the method I call from MainActivity so far:
private void hide() {
//This is the Ad from MainActivity
final AdView adLayout = (AdView) findViewById(R.id.adView);
final Button disableAds = (Button) findViewById(R.id.disableAds);
runOnUiThread(new Runnable() {
#Override
public void run() {
adLayout.setEnabled(false);
adLayout.setVisibility(View.GONE);
disableAds.setEnabled(false);
disableAds.setVisibility(View.GONE);
}
});
}
Do one thing when every you press the disable button save the state in Shared preferences. In Each every activity onStart() method check the state of the value. based on that value show/hide the ad-banner in your activity.
String MyPREFERENCES = "myPrefs" ;
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Set the Value when you click the button
Editor editor = sharedpreferences.edit();
editor.putString("show_ads", "no");
editor.commit();
Then onStart() method get the value of "show_ads", based on value show/hide the ADs.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString("show_ads", "yes");
if(name.equals("yes")){
adLayout.setEnabled(true);
adLayout.setVisibility(View.VISIBLE);
disableAds.setEnabled(true);
disableAds.setVisibility(View.VISIBLE);
}else{
adLayout.setEnabled(false);
adLayout.setVisibility(View.GONE);
disableAds.setEnabled(false);
disableAds.setVisibility(View.GONE);
}
If you are calling "MainActivity2" from "MainActivity":
//Code in "MainActivity" while starting "MainActivity2":
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isAdDisabled", true);// pass true if ad is disabled otherwise false
startActivity(intent);
//Code in onCreate of "MainActivity2":
Intent intent = getIntent();
boolean isAdDisabled = intent.getBooleanExtra("isAdDisabled", false);
if(isAdDisabled){
// code to hide adview
}
Use a shared preference to store a boolean (isAdsDisabled) in your main activity when you click a button.
Editor editor = context.getSharedPreferences("ADS_PREF", Context.MODE_PRIVATE).edit();
editor.putBoolean(ADS_DIABLED, isAdsDisabled);
you can query this again when you want to show the ad in the second activity to decide whether it should be displayed or not.
you can do this by using
SharedPreferences preferences = context.getSharedPreferences("ADS_PREF", Context.MODE_PRIVATE);
boolean isAdsDisabled = preferences.getBoolean(ADS_DIABLED, false);
you can check the value of isAdsDisabled to decide it.

Android how to load a new layout

I'm trying to load a new layout when I click a button, this layout has only a webView in it so my goal is, when the button is clicked that it opens the webView and directs the user to a pre-determined page. Right now I have
Button codesBtn = (Button)findViewById(R.id.imagebutton1);
codesBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.codes);
}
});
This is inside my onCreate() method in my main activity. I have a couple concerns:
1) is this the correct place to put this block of code into?
2) Do I need to create a separate activity for the webView and what I want the button to do?
3) If so, what is the basic structure of the activity needed?
Thanks in advance!
In general, rather than changing the layout in the current activity it is easier to launch a new activity with the new layout.
If you want to direct the user to a website, you could use an intent to ask the browser to open (example taken from this question)
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Or, you could create an Activity that just has a WebView and launch that by saying;
Intent i = new Intent(this, MyWebViewActivity.class);
i.putExtra("destination", myDestination);
startActivity(i);

Categories

Resources