Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out
Related
I try to flash an image view for 3 seconds and then return it to its original image. here is my code:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageView).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ImageView pic = findViewById(R.id.imageView);
pic.setImageDrawable(getResources().getDrawable(R.drawable.devil));
//Wait Block
try
{
Thread.sleep(3000);
} catch (InterruptedException e){
Toast.makeText(this,"Problem occurred",Toast.LENGTH_LONG).show();
}
//End of wait block
pic.setImageResource(R.drawable.angel);
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
}
So basically I want this code flow:
While the actual flow is the following :
Could you tell me the reason for this behavior as well as how to solve this?
this is oncreate function im calling Appdata() inside this method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
email=(EditText)findViewById(R.id.editText4);
pass=(EditText)findViewById(R.id.editText5);
conpass=(EditText)findViewById(R.id.editText6);
register=(Button)findViewById(R.id.btn_register);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddData();
}
public void AddData() {
register.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
boolean isInserted = db.insertData(email.getText().toString(),pass.getText().toString(), conpass.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
In Appdata function OnClickListener() is created but my problem is when i run this program app become stop and restart
when i hide inside code of Appdata() program ,its run without any error
i uploaded image click here
There is no button exists with the id R.id.btn_register in your xml file this is causes NullPointerException when you tried to set up the click listener. Add the buttons and EditTexts in your xml layout and you're good to go.
Add the below code in your activity_main.xml
<include
layout= "#layout/your_extra_layout_file"/>
You can use android:onclick="function_name" attribute with the button
And implement the function in the activity like below.
public void function_name(View view) {
//do the action here
}
I wanted delete or reset value 0 once user click on reset button ,
here is code which i am using , but seem not be work ..
What wrong I am doing , i am not able to figure out , ..I also tried all way , using google to figure it out , but still stuck.
sharedPreferences = getActivity().getPreferences(1);
sharedPreferences.edit().remove("ccs11").commit();
sharedPreferences.edit().remove("spinner_paneer").commit();
Below is my fragment , on which i am trying to implement .
public PlanFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gain, container, false);
sharedPreferences = getActivity().getPreferences(1);
String cp1s11=sharedPreferences.getString("cp111", "0");
String cf1s11=sharedPreferences.getString("cf111", "0");
String ccs11=sharedPreferences.getString("cc111", "0");
//Button
btnfinish=(Button)rootView.findViewById(R.id.finish_gain);
btnfinish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences = getActivity().getPreferences(1);
edt = sharedPreferences.edit();
edt.putString("ccs11", pro11.getText().toString());
edt.apply();
Intent intent = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(intent);
}
});
btnreset=(Button)rootView.findViewById(R.id.reset);
btnreset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sharedPreferences = getActivity().getPreferences(1);
sharedPreferences.edit().remove("ccs11").commit();
sharedPreferences.edit().remove("spinner_paneer").commit();
}
});
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
viewPager4 Fragment Activities
If I click on the Texts that play short sounds, one after another then after sometimes the mediaplayer hangs and doesn't play any sound. But if I'm able to destroy activity and recreate the same activity with refresh button in Action Bar, I'd be able to click sounds again.
So what to write in the code for R.id.item2?
Or there is any other way that continuous clicking on short sounds by these texts is possible without any hang kind of problem?
Following is the reference code:
public class module1 extends FragmentActivity {
static Context con;
static int length = 0;
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
static MediaPlayer mediaplayer, mediaplayert, m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con = this;
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mains, menu);
// Just .main into .mains [created new for different behavior of Action Bar]
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.item1) {
Intent in = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(in);
}
if (item.getItemId() == R.id.item2) {
//what should i write here? to destroy and recreate the same fragment activity again.
//Problem: After clicking fast on one after another text links, mediaplayert hangs and doesnt play
//Solution: exit app destroy and reopen, then mediaplayer works fine...
//SO, what to write here? kindly help!
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment ff = new DummySectionFragment1();
switch (arg0) {
case 0:
ff = new DummySectionFragment1();
break;
}
Bundle args = new Bundle();
args.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, arg0 + 1);
ff.setArguments(args);
return ff;
}
#Override
public int getCount() {
return 1;
}
#Override
public CharSequence getPageTitle(int arg0) {
Locale l = Locale.getDefault();
switch (arg0) {
case 0:
return getString(R.string.title_section27).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment1 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.m01set01, container, false);
// Genius Shot by Stupid vIC//
TextView Text = (TextView) rootView.findViewById(R.id.textView2);
Text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound1);
mediaplayert.start();
}
});
TextView Text1 = (TextView) rootView.findViewById(R.id.textView4);
Text1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound2);
mediaplayert.start();
}
});
TextView Text2 = (TextView) rootView.findViewById(R.id.textView6);
Text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound3);
mediaplayert.start();
}
});
return rootView;
}
}
#Override
protected void onDestroy() {
if (mediaplayert != null) {
mediaplayert.stop();
mediaplayert= null;
}
super.onDestroy();
}
}
My question is how to stop the media player when the user presses the back or the home button? I really need help..Can some one please give a code and tell me where to incorporate it in the activity.
This is my code:
MY JAVA CODE:
public class fbactivity extends Activity {
private MediaPlayer myMediaPlayer;
private MediaPlayer my1MediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
myButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.Button03);
myButtonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);
//Button related to play btn
Button myButtonOne1 = (Button) findViewById(R.id.Button02);
myButtonOne1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
myButtonTwo2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.pause();
}
});
Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
}
});
}
}
You inflate two buttons, one from R.id.magnumsilah, and another from R.id.Button03. However, the ClickListener is bound to only one of them, and no listener is bound to the back button.
It may well be worth it to be more thorough in you research before posting questions. Just a thought.