Call View from separate Activity - java

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.

Related

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.

How to save user input from EditText to a variable to be used on a different Activity

I have an edit text in Android Studio which I want a user to enter a name which can then be stored as a variable. I then want to add this variable to a textview in my main activity.
Right now all a have is a blank activity with an edit text and button to save the user input as a variable.
Edit
Ok, I am going to change my approach. In my main activity, I have two text views. If I changed them to edit texts, then how would I save them from the main activity without a save button so that what the user typed would still be there?
Save it in Shared Preferences and then retrieve it from the other activity.
To save a String in shared preferences, you can create a method like the following:
public static void setUsername(Context context, String username) {
SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.commit();
}
To retreive it :
public static String getUsername(Context context) {
SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
return prefs.getString("username", "");
}
Edited:
In the activity which contains the EditText, you can call the method as follows:
setUsername(this,myEditText.getText().toString());
And in the Activitiy that contains the TextView:
myTextView.setText( getUsername(this) );
you ça use edit.getText().toString() tout get the user input.
To launch new activity and pass the string to it use
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", theString);
startActivity(intent);
EDIT
To be more easy you should add a button to your layout so when the user press that button you can launch the next activity with the string.
This should go into your activity
public class BlankActivity implements OnClickListener {
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//find editText
mEditText = (EditText) findViewById(R.id.your_edit_text_id);
//listen button
findViewById(R.id.your_button_id).setOnClickListener(this);
}
#Override
public void onClick(View v) {
String theString = mEditText.getText().toString();
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", theString);
startActivity(intent);
}
}

Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

I have an app, and I am trying to limit the number of button clicks to five, and then once the user has pressed this button five times it should disable.
However I am getting the above error and I am not sure why.
Any ideas ?
buttonadd.setOnClickListener(new OnClickListener () {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
int clicks = 0;
clicks++;
if (clicks >= 5){
buttonadd.setEnabled(false);
}
SharedPreferences prefs = Context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("clicks", clicks);
editor.apply();
}
});
You are wrongly trying to use the virtual method getSharedPreferences() in a static way, that's why its giving that compile-time error.
If that code is in an Activity, replace
Context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
with
getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
If it is in a Fragment, use
getActivity().getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
EDIT:
use
if (clicks >= 5){
buttonadd.setEnabled(false);
buttonadd.setClickable(false);
buttonadd.setFocusable(false);
buttonadd.setFocusableInTouchMode(false);
}
and make clicks a class member, i.e. declare it as
private int clicks;
in the Activity.
EDIT 2:
I think I have understood the mistake you are making. In your code, replace
int clicks = 0;
with
SharedPreferences prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int clicks = prefs.getInt("clicks", 0);
Try this. This should do it.
It means that you need an instance of a Context object to call the getSharedPreferences() method. If you're inside an Activity, try this:
this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE)
As the error message says getSharedPreferences() is a non-static method. When you do Context.getSharedPreferences(...) you are trying to call it directly from the class. Instead, you need to call it from a Context instance.
If your code is inside an Activity (as Activity extends Context) you can simply do:
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);

Android ViewPager guideline

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 .

boolean won't stay on the right input when i kill my app or reboot the device

im building a app, found a java part that lets me show an activity only at first start.
this works but when i kill the app or reboot my phone it goes back, can anyone help me out with this?
im using this in my main activity (Toolz.java)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.firststart == false) {
setContentView(R.layout.toolz);
} else {
Intent i = new Intent(this, First.class);
startActivity(i);
finish();
}
}
#Override
protected void onResume() {
super.onResume();
}
and i added this to First.java
Prefs.firststart=false;
and i made the Prefs.java and added this
public class Prefs {
public static boolean firststart = true;
}
you will need to use SharedPreferences instead o static variable for saving data which also available when app killed or device reboot.
you can see following tutorial how we use SharedPreferences for saving and reading data from SharedPreferences
Shared Preferences
When the app restarts all your code will start from the starting point (i.e Prefs.firststart will be set to true). Instead, you need to make a persistent variable that is saved throughout sessions. If you're using a DB you could use that, or you could use the built-in SharedPreferences, as such:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = this.getPreferences(MODE_PRIVATE);
if (prefs.contains("started")) {
setContentView(R.layout.toolz);
} else {
//Add the preference:
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("started",true);
editor.apply();
Intent i = new Intent(this, First.class);
startActivity(i);
finish();
}
}
When you kill your app, the values are going to reset to their initial values. Your boolean will be equal to true. I suggest saving the state of the variable using SharedPreferences. Check out this other question someone asked about saving.
How to save a state of a variable in android app and use it
Try it like this
//**** First start *****
//you get the prefs for your app using a pref name
String PREFS_NAME = "myAppPrefs";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//persist the boolean value in preferences using a key-value approach
editor.putBoolean("pref_app_started", true);
//commit the changes
editor.commit();
and when you start the app you check for that pref
//**** next start *****
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//now we get the saved boolean using the key we previously uesd when setting it
//the second parameter is the default value for the flag in case it was never set
boolean wasAppStarted = settings.getBoolean("pref_app_started", false);
//now you can use your wasAppStarted flag to decide what you do further

Categories

Resources