How to make Bottom Sheet Dialog that shows one time only - java

I want to create a Bottom Sheet Dialog. It looks like the image below
enter image description here
public class MainActivity extends AppCompatActivity {
//this is Button that open dialog
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});
}
}
I have tried many methods such as SharedPreferences. But it not working

SharedPreferences preference = getSharedPreferences("mPreference", MODE_PRIVATE);
boolean isUserAgreed = preference.getBoolean("isUserAgreed", false);
if (!isUserAgreed){
// show bottomSheetDialog
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
MainActivity.this, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.layout_bottom_sheet, (LinearLayout) findViewById(R.id.bottomSheetContainer));
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetView.findViewById(R.id.doNotAgree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.exit(0);
}
});
bottomSheetView.findViewById(R.id.agree).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("mPreference", MODE_PRIVATE).edit();
editor.putBoolean("isUserAgreed", true);
editor.apply();
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
});
}
});

Related

Want to show interstitial ads on every button clicked but ad show on one button

This is my code for main_activity.i want to implement ads on every button visible on screen and when clicked interstital ad should show. But it only shows ad on btn_button.Kindly guide me how to resolve this issue.
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private InterstitialAd mInterstitialAd2;
Button btn_button,btn_button2,btn_button3,btn_button4,btn_button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}});
btn_button=(Button) findViewById(R.id.button);
btn_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i3=new Intent(MainActivity.this,Button.class);
startActivity(i3);
}
});
btn_button2=(Button) findViewById(R.id.button);
btn_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i2=new Intent(MainActivity.this,Button.class);
startActivity(i2);
}
});
btn_button3=(Button) findViewById(R.id.button);
btn_button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Button.class);
startActivity(i);
}
});
btn_button4=(Button) findViewById(R.id.button);
btn_button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i4=new Intent(MainActivity.this,Button.class);
startActivity(i4);
}
});
btn_button5=(Button) findViewById(R.id.button);
btn_button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i5=new Intent(MainActivity.this,Button.class);
startActivity(i5);
}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
btn_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
mInterstitialAd2 = new InterstitialAd(this);
mInterstitialAd2.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd2.loadAd(new AdRequest.Builder().build());
btn_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd2.isLoaded()) {
mInterstitialAd2.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
btn_button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
});
}
}
You have same button id, that's why it is showing one only one button as #Anon mentioned.
But actually you don't need so many interstitial instances as you are only using one ad unit. Only one instance is sufficient to show ad on every button clicked.
BUT... your AdMob account will be disabled very soon if you try to show ad on every click.
Every button you are using refers to the same button id, so you have only one button working. =)
'btn_button5=(Button) findViewById(R.id.button);
btn_button4=(Button) findViewById(R.id.button);'

createDialog for onBackPressed but my app showing error close app

I have created a new layout name dialog_exit for onBackPressed but when I install and open, my application can not open and showing error close app
Please review my whole code and guide me how can I solve this problem
Here is my main activity code
public class MainActivity extends AppCompatActivity {
public Dialog mDialog;
public Button mDialogyes, mDialogno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDialog();
}
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) findViewById(R.id.yes);
mDialogno = (Button) findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
});
}
#Override
public void onBackPressed() {
mDialog.show();
}
}
Here is my layout code as screenshot because
stackoverflow not allow me to add more code that why sharing image
Updated code of createDialog function
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
try this code
mDialogyes = (Button)mDialogyes. findViewById(R.id.yes);
mDialogno = (Button)mDialogyes. findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
You have to do like this
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);

Call a fragment from an activity with a fragmenttransaction

what I am trying to do is call fragment from an activity with a FragmentTransaction, at that point it works for me, the problem is that when the activity calls the fragment, the two are presented on the screen.
Activity java code :
public class ElectricalCalculators extends AppCompatActivity implements View.OnClickListener {
Button kwbutton , evbutton ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_electrical_calculators);
findViewById(R.id.menu_back).setOnClickListener(this);
findViewById(R.id.menu_home).setOnClickListener(this);
Button kwbutton = (Button) findViewById(R.id.ampsBoutton);
kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , AmpsConversion.class);
startActivityForResult(intent,0);
}
});
Button evbutton = (Button) findViewById(R.id.evBoutton);
evbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , ElectronVoltsConversion.class);
startActivityForResult(intent,0);
}
});
Button jbutton = (Button) findViewById(R.id.joulesBoutton);
jbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , JoulesConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwbutton = (Button) findViewById(R.id.kwBoutton);
Kwbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwConversion.class);
startActivityForResult(intent,0);
}
});
Button Kwhbutton = (Button) findViewById(R.id.kwhBoutton);
Kwhbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( v.getContext() , KwhConversion.class);
startActivityForResult(intent,0);
}
});
}
#Override
public void onClick(View v) {
Class clazz = null;
FragmentManager fragmentManager = getSupportFragmentManager();
if (v.getId() == R.id.menu_back){
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
}
/** switch (v.getId()) {
case R.id.menu_back:
//clazz = Fragmen2.class;
fragmentManager.beginTransaction().replace(R.id.elctricalCalculator, new Fragmen2()).commit();
break;
case R.id.menu_home:
clazz = MainActivity.class;
break;
/**case R.id.five_tabs_changing_colors:
clazz = FiveColorChangingTabsActivity.class;
break;*/
//}
// startActivity(new Intent(this, clazz));
}
}
Fragment code :
public class Fragmen2 extends Fragment {
Button Tboton,
Lboton,
Pboton,
Dboton,
wboton,
eboton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragmen2, container, false);
Tboton = (Button)view.findViewById(R.id.boton1fragmen2); /**boton que conecta el fragment 2 con la actividad temperatura */
Tboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Tboton = new Intent( getActivity() ,Temperatura.class);
startActivity(Tboton);
}
});
Lboton =(Button)view.findViewById(R.id.boton2fragmen2);
Lboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Lboton = new Intent(getActivity() , Dimensions.class);
startActivity(Lboton);
}
});
View v = inflater.inflate(R.layout.fragment_fragmen1, container, false);
/**Pboton = (Button)view.findViewById(R.id.boton3fragmen2);
Pboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});*/
Dboton = (Button)view.findViewById(R.id.boton4fragmen2);
Dboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent Dboton = new Intent(getActivity(),Dimensions.class);
startActivity(Dboton);
}
});
wboton = (Button)view.findViewById(R.id.boton3fragmen2);
wboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent wboton = new Intent(getActivity(),Weight.class);
startActivity(wboton);
}
});
eboton = (Button)view.findViewById(R.id.boton4fragmen2);
eboton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent eboton = new Intent(getActivity(),ElectricalCalculators.class);
startActivity(eboton);
}
});
return view;
}
}

Adding Difficulty to a game

I want to add a difficulty in my game, for example when I click a category like category "A" it will show to the next layout and the user has to pick a difficulty to play "easy, normal or hard" .
sorry for my bad sentence
here is my categories:
public class CategoryActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Button A = (Button) findViewById(R.id.btn_a);
Button B = (Button) findViewById(R.id.btn_b);
Button C = (Button) findViewById(R.id.btn_c);
Button D = (Button) findViewById(R.id.btn_d);
Button E = (Button) findViewById(R.id.btn_e);
Button F = (Button) findViewById(R.id.btn_f);
Button G = (Button) findViewById(R.id.btn_g);
Button H = (Button) findViewById(R.id.btn_h);
Button I = (Button) findViewById(R.id.btn_i);
Button J = (Button) findViewById(R.id.btn_j);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg1) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
C.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg2) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
D.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg3) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
E.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg4) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
F.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg5) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg6) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
H.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg7) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
I.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg8) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
J.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg9) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
}
You should use radio buttons, so the user can select only one option and not two or more.
This is an example taken from the official android devs website.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
PS: You're question is not really clear. It makes it difficult to help you. I hope my answer is acceptable. Refer to the manuals and do some tutorials.

Pass a XML resource from one activity to another activity using java code in Android Studio?

I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}

Categories

Resources