Android OnCreate input bundle always null - java

I am undertaking project but I have been having some issues with activities and saving state. So I have gone back to basics to try and understand what I was missing. However, even with a very simple set up I am having the same issues.
So I started with an empty project and placed 2 textViews and 1 button on the activity_main.xml and 1 button on the activity_2.xml. The user will enter some text in a text box, press the button to advance to the Activity2 Activity and then press the button (in the Activity2 layout) to return back to the MainActivity.
My hope is that the text entered into the text box has been saved by the overriden onSaveInstanceState method and then the bundle argument in the OnCreate will hold this value. However, when MainActivity is hit, the bundle is alywas null despite onSaveInstanceState being called when button to start Activity2 is pressed.
Am I missing something? The onSaveInstanceState method is definitely initiated and savedInstanceState is populated within it, so why when the MainActivity is started again and onCreate is called is the Bundle savedInstanceState null? Is it the way I am setting up starting other Activities?
Thanks for your help!
For reference, here is my setup:
My MainActivity:
public class MainActivity extends AppCompatActivity {
Button forwardButton;
TextView editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forwardButton = findViewById(R.id.buttonForward);
editText = findViewById(R.id.editText);
forwardButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
Intent activity2 = new Intent(MainActivity.this,Activity2.class);
startActivity(activity2);
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
String textToSave = editText.getText().toString();
savedInstanceState.putString("editText",textToSave);
super.onSaveInstanceState(savedInstanceState);
}
}``
My Activity2 class:
public class Activity2 extends AppCompatActivity {
Button goBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
goBack = findViewById(R.id.goBack);
goBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
Intent activity2 = new Intent(Activity2.this,MainActivity.class);
startActivity(activity2);
}
});
}
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/buttonForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="232dp"
android:layout_marginEnd="162dp"
android:layout_marginBottom="76dp"
android:text="Forward"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="43dp"
android:layout_marginEnd="102dp"
android:layout_marginBottom="268dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Enter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2">
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="232dp"
android:layout_marginEnd="162dp"
android:layout_marginBottom="76dp"
android:text="Go back"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

In your Activity2
goBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Activity2.this.finish();
}
});
And get your saved instance in MainActivity from onRestoreInstanceState
//this will trigger when the same instance of activity is restored
//by the back press that finishes the Activity2
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String data = savedInstanceState.getString("editText");
}

Related

Can't dismiss a popup window (other solutions do not work)

I am trying to create a simple PopupWindow with a text entry field.
I know there are many questions of this type, but none seem to solve my problem. This is the XML of my popup layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="#color/white"
android:gravity="center"
android:padding="15dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dodaj produkt po identyfikatorze"
android:textSize="15sp"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:textSize="20sp"
android:hint="Identyfikator"
android:minWidth="250dp"
android:id="#+id/insert_edit"
android:gravity="center"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_ok"
android:text="OK"/>
<Button
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:id="#+id/insert_cancel"
android:text="Anuluj"/>
</LinearLayout>
</LinearLayout>
Now, I want to create this layout in my main activity when a FAB is pressed. This is how I do it:
public class ScrollingActivity extends AppCompatActivity {
private ActivityScrollingBinding binding;
private int ACTIVITY_LOAD_DATABASE = 1;
AbstractDatabase database;
RowAdapter rowAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = Room.databaseBuilder(getApplicationContext(), AbstractDatabase.class, "magazyn_db")
.allowMainThreadQueries().build();
binding = ActivityScrollingBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
FloatingActionButton fab = binding.addCode;
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.println(Log.INFO, "VIEWs", "Showing popup");
PopupWindow codePopup;
View popupView = getLayoutInflater().inflate(R.layout.insert_code, binding.getRoot());
codePopup = new PopupWindow(popupView);
codePopup.setFocusable(true);
codePopup.setOutsideTouchable(true);
View cancelButton = codePopup.getContentView().findViewById(R.id.insert_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
codePopup.dismiss();
}
});
View acceptButton = codePopup.getContentView().findViewById(R.id.insert_ok);
acceptButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View editView = codePopup.getContentView().findViewById(R.id.insert_edit);
String id = ((EditText)editView).getText().toString().trim();
insertNewElement(id);
codePopup.dismiss();
}
});
}
});
[...]
For an unknown reason my popup doesn't go dismissed, both when I click outside of it, or press the cancel button (when I tried to print some logs from the handler, they worked). I have tried many solutions, also inflating it with no root, but then 'showAtPosition' doesn't even show it, and no solutions here on stackoverflow seem to help me even though there are many of them.
Is anyone able to point the problem here?
public void dismiss () Disposes of the popup window. This method can be invoked only after showAsDropDown(android.view.View) has been executed. Failing that, calling this method will have no effect.

Why SetText not work in OnClick() function

I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
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);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}

Cannot Start another activity after click the Button (android)

I cannot start my activity (Display Message) when I click the button that show in activity_main.xml. I have try fix the problem in onClick but still cannot Run it. Can I know what is the problem ? Thank You.
Here are the code :
MainActivity.java
public class MainActivity extends AppCompatActivity{
public Button but1;
public void init(){
but1= (Button)findViewById(R.id.button1);
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent display = new Intent(MainActivity.this, DisplayMessage.class);
startActivity(display);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.products));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="358dp"
android:layout_height="38dp"
android:elevation="5dp"
android:layout_margin="5dp"
android:padding="10dp"
/>
<Button
android:id="#+id/button1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="Confirm" />
</LinearLayout>
It looks like you never called init() from your onCreate() method. Therefore, but1 was never initialized and OnClickListener was never set.

Error as soon as I add setOnClickListener Android Studio

so for a school project I'm making an app, but I"m stuck.
I've got a button on my main class.
XML code of this class:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.larsb.csvvg.Home"
android:background="#drawable/home">
<Button
android:id="#+id/Lariks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button"
android:visibility="visible" />
<Button
android:id="#+id/Salland"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:text="Button"
android:visibility="visible"
android:layout_alignTop="#+id/Lariks"
android:layout_alignLeft="#+id/Lariks"
android:layout_alignStart="#+id/Lariks" />
<Button
android:id="#+id/CSG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="visible"
android:layout_below="#+id/Salland"
android:layout_alignLeft="#+id/Salland"
android:layout_alignStart="#+id/Salland" />
</RelativeLayout>
So what I want to do is when I button click Lariks I switch to a new activity.
I try to do this with the following code:
public class MainActivity extends AppCompatActivity {
private static int WELCOME=4000;
Button lariks;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lariks = (Button)findViewById(R.id.Lariks);
lariks.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent=new Intent(MainActivity.this,Home.class );
startActivity(intent);
finish();
}
},WELCOME);
}
}
In the public void onClick(view) thing I got the code for switching activity, that's not the problem.
The problem is that as soon as I add the:
lariks.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
the app can't launch anymore. It starts the emulator but it keeps saying the app has stopped. Anyone has any idea why this is? If I remove that part of the code the app actually does run.
Your Button with android:id="#+id/Lariks" is not in the activity_main layout (as evidenced by tools:context="com.example.larsb.csvvg.Home" and your crash). Therefore findViewById(R.id.Lariks) returns null, and attempting to call setOnClickListener() on this null reference causes a crash.
Your findViewById()-setOnClickListener() pair should likely be in the Home activity, or the button should be in the activity_main layout.

How can I change the content of a single WebView (Android) depending on which button the user clicks (out of three) in one activity?

Hi I am new to Android development and StackOverflow.
I have an Android WebView application in which a 'starter activity' (just intro text and a button to start main intent) starts my WebView activity, which loads a single URL (this is in the onCreate method).
In this main activity, I have three buttons, one of which opens the same URL in the current activity (effectively refreshing the page). The other two buttons I would like to open two different URLs in the same WebView activity, and I was wondering if/how this is possible.
Here is the main ReadWebpageAsyncTask.java activity code:
public class ReadWebpageAsyncTask extends Activity {
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView browser, String urlStrOne) {
browser.loadUrl(urlStrOne);
return true;
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(browser.canGoBack()) {
browser.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
And the corresponding XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="onClick"
android:text="#string/load_page" />
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/soon"
android:background="#+drawable/custom_button_one"
android:onClick="exitButton"
android:text="#string/exit_button" />
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toLeftOf="#+id/exitapp01"
android:layout_toRightOf="#+id/readWebpage"
android:layout_alignParentTop="true"
android:onClick="comingSoon"
android:text="#string/future_courses" />
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/WebView01" >
</WebView>
</LinearLayout>
P.S. Please excuse the terrible naming of everything, I wrote this a while back and haven't gotten around to changing it all.
To clarify, the main URL which is loaded first is the same as the button "readWebpage", and the other two URLs which I would like to be loaded on click are buttons "exitapp01" (what can I say, I am a noob) and "soon".
You can add OnClickListeners to your buttons and switch the url depending on the case:
public class ReadWebpageAsyncTask extends Activity implements OnClickListener{
private WebView browser;
protected String urlStrOne = "url one";
protected String urlNewCourses = "url two";
protected String urlFutureCourses = "url three";
Button reload, exit, soon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_webpage_async_task);
browser = (WebView)findViewById(R.id.WebView01);
browser.setWebViewClient(new MyBrowser());
browser.loadUrl(urlStrOne);
reload = (Button)findViewById(R.id.readWebpage);
exit = (Button)findViewById(R.id.exitapp01);
soon = (Button)findViewById(R.id.soon);
reload.setOnClickListener(this);
exit.setOnClickListener(this);
soon.setOnClickListener(this);
}
#SuppressLint("SetJavaScriptEnabled")
public void onClick(View view){
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.readWebpage:
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.loadUrl(urlStrOne);
break;
case R.id.exitapp01:
//Do whatever you want with this button
break;
case R.id.soon:
//Do whatever you want with this button
break;
}
}
}
And remove all the android:onClick="" from your xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|right"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<Button
android:id="#+id/readWebpage"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"/>
<Button
android:id="#+id/exitapp01"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/soon"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<WebView
android:id="#+id/WebView01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
If The buttons are on Activity You have to set the OnClickListener on all the buttons and within the onClick() do like this:
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.[Your first button Id]:
{
//Functionality
}
case R.id.[Your Second button Id]:
{
//Functionality
}
case R.id.[Your first button Id]:
{
//Functionality
}
//End Switch
}
//You also need To Write implements OnClickListener at the Class Definition Of Your activity

Categories