I am currently implementing a back button action for navigating webview history. My webview has been working(including handling opening links within the webview). However, after adding the back button action it crashes whenever I try to use the back button. This is my Main Activity Code:
public class MainActivity extends Activity {
WebView mWebView;
//Back Button Code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(amapps.com.uhss.R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(false);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://uhsswordandshield.com/");
mWebView.getSettings().setSupportMultipleWindows(true);
}
Could someone please tell me what is wrong with the code. I believe it has to do with my
WebView mWebView;
declaration before I initialize the webview. But I am not sure of any way to handle webview history navigation. Also, I am unsure of how to post my log cat and what to post so if someone could please tell me which part of the logcat I need to post that would be great. Thanks!
try this code:
if((keyCode==KeyEvent.ACTION_DOWN)){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
Related
I am trying to make a social media app , Im using fragments within BottomNavigation (home - smart room - notifications - profile), And Im using a floating button to switch to "Add post" activity , Now Im having a problem with the back button , i want it to take me to the recent visited fragment but it always take me to the home fragment
here is my BottomNavigation activity code :
public class BottomNavigationActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation_layout);
//setEnabled(false) for the 2nd index because we're using a floating button instead
bottomNav.setBackground(null);
bottomNav.getMenu().getItem(2).setEnabled(false);
bottomNav.setOnItemSelectedListener(navListener);
//start AddPostActivity class
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), AddPostActivity.class));
finish();
}
});
}
//Switch between fragments
public BottomNavigationView.OnItemSelectedListener navListener =
new BottomNavigationView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_sr:
selectedFragment = new SmartRoomFragment();
break;
case R.id.nav_notif:
selectedFragment = new NotificationsFragment();
break;
case R.id.nav_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
I want the best solution to handle this problem , thanks in advance!
I am trying to create a dialog inside a fragment.when I am trying to press on the button and enter the dialog the app collaspe.
I guess the code is not right can you please help me with that?
Here is my code:
private void openDialog(){
Dialog dialog=new Dialog(getContext());
//AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater=this.getLayoutInflater();
View custom_dialog=getActivity().getLayoutInflater().inflate(R.layout.geo_dialog,null);
dialog.setContentView(custom_dialog);
// add_geofence_radius= custom_dialog.findViewById(R.id.radius_size);
save_btn=custom_dialog.findViewById(R.id.save_btn);
cancel_btn=custom_dialog.findViewById(R.id.cancel_btn);
/* save_btn.setOnClickListener(new View.OnClickListener() {
#Override
}
});
*/
/*cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
*/
// dialog.setTitle("hello");
dialog.show();
}
The best way to show dialog in android is to use "DialogFragments" since they are aware of the lifecycle of the view it is attached on (ie. fragments/activities).
Here is an examples provided in Android docs:
public class PurchaseConfirmationDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
.create();
}
public static String TAG = "PurchaseConfirmationDialog";
}
To show the dialog use:
new PurchaseConfirmationDialogFragment().show(
getChildFragmentManager(), PurchaseConfirmationDialog.TAG);
For more reference on dialogFragments, checkout : Create a DialogFragment
This is the code to test the thread and how it works! (It's not really connecting just to look like one) I wanted to add a progress bar when the alert dialog is gone by pressing the yes button, but it doesn't work. I set the Visibility to gone before, and visible to after. What seems to be the problem? I done casting right, and there's nothing wrong elsewhere. The text doesn't change also..... please help :)
private void connectRequest() {
AlertDialog.Builder builder = new AlertDialog.Builder(ThreadActivity2.this);
AlertDialog dialog = builder.setMessage("Do you request Remote Access?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
accessTv.setText("Requesting Remote Access ...");
accessPgb.setVisibility(View.VISIBLE);
try {
Thread.sleep(5700);
} catch (InterruptedException e) {
e.printStackTrace();
}
accessTv.setText("Server Access Successful!");
accessPgb.setVisibility(View.GONE);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
dialog.show();
}
Try using java.util.concurrent.TimeUnit:
TimeUnit.SECONDS.sleep(1);
To sleep for one second or
TimeUnit.MINUTES.sleep(1);
I am doing an app that creates an ethereum wallet and send some ether when you touch the button register, it takes about 1 minute to do this, while it happens I want to show a message saying: Creating a wallet, wait please.
When I show the message it won't create the wallet or it will create the wallet but it won't show the message.
PS: If someone knows how to down the time to do this, it will help me a lot.
Thanks
thanks for the help... and the negative.
I have solved it anyway, I leave my solution below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ActivityMain);
errorMsg = new AlertDialog.Builder(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyAsyncTasks().execute();
}
});
}
class MyAsyncTasks extends AsyncTask<Void, Void, String> {
// ProgressDialog dialog;
#Override
//It will show a message during your background
protected void onPreExecute() {
dialog = new ProgressDialog(mainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Add title");
dialog.setMessage("Add message");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
#Override
protected void onPostExecute() {
dialog.dismiss(); // Close the Dialog
//Show a window with error or operation succesfully
if(hash.equals("Error")){
displayError("Error", "Error");
dialog.cancel();
}
else{
displayConfirmation("Operation Succesfully");
dialog.cancel();
}
}
#Override
protected String doInBackground(Void... voids) {
dialog.show();
// your code to do you background activity
}
}
I'm trying to hide a button inside getView method of an Adapter. Unfortunately, I can't do it.
private class AppListAdapter extends ArrayAdapter<Info> {
public AppListAdapter(Activity activity, List<Info> apps) {
super(activity, android.R.layout.simple_list_item_1, apps);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// if we weren't given a view, inflate one
if (null == convertView) {
convertView = getLayoutInflater()
.inflate(R.layout.activity_apps, null);
}
btnUninstall = (Button) convertView.findViewById(R.id.uninstallButton);
btnUninstall.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
btnUninstall.setVisibility(View.INVISIBLE);
}
}
);
return convertView;
}
}
Any help shall be appreciated?
Try changing this line
btnUninstall.setVisibility(View.INVISIBLE);
To this
v.setVisibility(View.INVISIBLE);
That's because in the adapter android passes the same view over and over again (recycling), try to set the visibility of the button to visible every time.
I checked the code and it works fine for me.!
Hey quick question though, have you declared your btnUninstall anywhere?
I cannot see it anywhere in the code you have provided, thats all.
Button btnUninstall;