Floating Refresh-Button for WebView - java

I want to make a floating Refresh-Button for my WebView.
I did some research but I wasn't able to fix my problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url = "https://www.rtl.de/cms/sendungen/serie/alarm-fuer-cobra-11.html";
WebView view = (WebView) this.findViewById(R.id.WebView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view.reload();
}
});
I also tried MainActivity.this.webView.loadUrl("https://www.rtl.de/cms/sendungen/serie/alarm-fuer-cobra-11.html"); instead of view.reload(); but there is always an error.
Cannot resolve method 'reload()'
or
Cannot resolve symbol 'WebView'
It would be very nice if anyone could help me.

You can actually just make an Intent and start your current activity as a new one. For example in your activity, make a button like this:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
Please tell me if it is ok.
Take care!

Related

"cannot resolve findviewbyid in fragment" error

I'm trying to move Floating Action Button into my Fragment. But I'm getting an error about findViewById. It's "Cannot resolve method 'findViewById'
I researched, i found so many things but i'm not good at this. Can anyone explain me how can i solve this error? I tried "Invalidate Caches/Restart" and "Clean and Rebuild Project", but it didn't work.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Floating Action Button
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// intent
}
});
}
findviewById() do not belongs to Fragment class, but Activity.
You said you're inside a Fragment and want to access some view in the Activity, you must use getActivity().findViewById(R.id.fab).

Why does Android Studio keep asking for a "Expression Expected"?

Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)

OnClickListener Android Studio

Hi I'm trying to create an application for Android, but when I try to start it with 2 setOnClickListener, it crashes,
Infact if i delete one of the two events it doesen't crash
how can I do?
P.S. Sorry for my bad english, but im italian
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that wil 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);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.FeedBackHome);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.contact);
}
});
Button button= (Button) findViewById(R.id.NotReg);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Register.class));
}
});
}
first illegal code :
you can't set new Content View onClick like you do here :
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.contact); // <-- this is wrong
}
});
be sure that Register.class contain layout and able to open from another activity.

Getting a NullExceptionPointer on my setOnClickListener()

I'm getting a NullException error on my Button setOnClickListener() method. I'm a bit flabbergasted as to why this is happening. Please bear in my mind that this is my first Android app, and I assume that there are bound to be some runtime errors (there are no compilation errors).
btnNaira.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToNaira();
}
});
The dollarToNaira() method can be found here:
public void dollarToNaira() {
try {
dollarAmt = txtAmount.getText().toString();
nairaAmt = (Double.parseDouble(dollarAmt) * 199.00);
lblOutput.setText(dollarAmt + "U.S. Dollars = " + nairaAmt + " Nigerian Naira.");
}catch(Exception ex) {
message = "Please enter a valid numerical amount";
lblOutput.setText(message);
}finally {
txtAmount.requestFocus();
txtAmount.selectAll();
}
}
Please tell me what I am doing wrong I would like to know what causes the setOnClickListener method to return NULL. Thanks!
EDIT: this is the entire onCreate() method, I don't believe that I made an error:
protected void onCreate(Bundle savedInstanceState) {
txtAmount = (EditText) findViewById(R.id.txtAmount);
btnNaira = (Button) findViewById(R.id.btnNaira);
btnCFA = (Button) findViewById(R.id.btnCFA);
lblOutput = (TextView) findViewById(R.id.lblOutput);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set up event listener on btnNaira and btnCFA
btnNaira.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToNaira();
}
});
btnCFA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dollarToCFA();
}
});
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();
}
});
}
there is no problem in your code.
i guess you've made a mistake in initializing buttons and text views.
1- make sure that you define your listener inside a method, like onCreate(),
i think you had defined that in class body.
2- i guess that you had defined your button or text views,
but you did not initialized them , so they are null.
here is an example of initializing a Button
Button btnDoSomething = (Button)findViewById(R.id.idOfButton);
now you can setOnClickListener for it .
You should write in this way
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtAmount = (EditText) findViewById(R.id.txtAmount);
btnNaira = (Button) findViewById(R.id.btnNaira);
btnCFA = (Button) findViewById(R.id.btnCFA);
lblOutput = (TextView) findViewById(R.id.lblOutput);
Make sure you call setContentView(R.layout.activity_main); before initialize your button View.
Hope it helps.

Why unable to start new activity when click on the rate design button

When I click on the rate design button it stops, I doubt is my manifest because I have check it already below are my codes. I have also enter the error log. Can someone help me instead of rating down for this question
Your forget to add
setContentView(R.layout.yourlayout);
before Button initialization in onCreate(....)
Correct:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
Button btnRating = (Button) findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NearablesDemoActivity.this, MainActivityRating.class);
startActivity(intent);
}
});
}

Categories