Why doesnt the onClickListener do anything? - java

Following a tutorial, I did set up onClick-Listeners before.
Now I am trying to make a simple app for listening to one stream, but my onClickListener doesn't do anything.
I created a new very simple app in android studio 3.3.2 to find out what is happening. I guess it is something very basic.
package com.example.musicplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
final Button play_button = findViewById(R.id.play_button);
Log.i("Button found","this one: " + play_button.getText());
play_button.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the play button is clicked on.
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "A click happened!", Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_main);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/play_button"/>
</LinearLayout>
The toast message should appear when the button is clicked, but nothing happens.

You are calling setContentView twice. This will replace the view hierarchy set up by the first call. Remove the second call to setContentView.

Related

WebView will not load when in another layout, but works fine in main

I am working on an android project that reqires a home screen that I just have image buttons on for the time being, but would like to access different layouts, it access the different layouts just fine, but when using the same code for webview, webview refuses to load and just displays a blank screen, the same code worked fine on the main activity, but is giving me trouble now and im not sure where to go from here, I only have an image on the second activity as a sanity check that my buttons were going to the right places
Main Activity
package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
ImageButton foodButton = (ImageButton) findViewById(R.id.imageButtonFood);
foodButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
ImageButton techButton = (ImageButton) findViewById(R.id.imageButtonTech);
techButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
ImageButton calendarButton = (ImageButton) findViewById(R.id.imageButtonCalendar);
calendarButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bcs_calendar_widget);
}
});
}
}
Second activity
package com.example.bsc_group;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class BcsCalendarWidget extends AppCompatActivity {
public WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
webView = findViewById(R.id.webviewWidget);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://google.com");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(R.layout.bcs_calendar_widget);
{
//Spinner spinnerBcsListings = findViewById(R.id.spinner_bcs_group);
//ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.bcs_group_listings, android.R.layout.simple_spinner_item);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
//spinnerBcsListings.setAdapter(adapter);
}
}
#Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
}else {
setContentView(R.layout.activity_main);}
}
}
XML file for second activity
<?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=".MainActivity">
<WebView
android:id="#+id/webviewWidget"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_foreground" />
</RelativeLayout>
The issue is that you are not opening the activity. Only giving a context view without executing the code. This bad. Whenever you want to go to another activity, use this code:
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
Also in the onBackpressed I see that you are just doing setContentView. Don't do that. Use this instead:
finish();

Java-app closes, when i push Button to start a Toast. But why?

I made VERY simple java toast that appears when you press a button. But when I run it on my phone, it stops the app and quits without an error. What did I do wrong?
MainActivity:
package com.example.ras.tests;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonWasClicked(View Button) {
Toast.makeText(this , "Button wurde geklickt!" , Toast.LENGTH_SHORT).show();
}
}
Button:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="#+id/activity_main"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="#+id/activity_main"
android:layout_marginLeft="16dp"
android:onClick="buttonWasClicked (MainActivity)"
android:visibility="visible" />
</android.support.constraint.ConstraintLayout>
Simple: android:onClick="buttonWasClicked" in your xml should do the trick.
Or in your OnCreate method you can assign listener to your button like this:
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonWasClicked(button);
}
});
In this case you delete android:OnClick from your xml.
And you can do it event better: remove parameter View button from method buttonWasClicked this allows you not to declare your variable final.
Try removing the "(MainActivity)" in the android:onClick in your xml file.

type cannot be resolved or is not a field

i have just started building android application. And this error
button cannot be resolved to a type
constantly bugs me. here is the java code
package com.android.revision1;
import android.app.Activity;
import android.os.Bundle;
public class Revision1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewByid(R.layout.button1);
}
}
Here is the Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
button seems undeclared, so declare that field
private Button button;
or local variable:
Button button=(Button) findViewByid(R.layout.button1);
Assuming your variable is declared and you just aren't showing it, you need to import the Button class. Add the following line next to your other imports:
import android.widget.Button;
You need to declare button as it's currently not declared before being set.
Here is the complete code:
package com.android.revision1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Revision1Activity extends Activity {
private Button button; // this is what you are missing
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewByid(R.layout.button1);
}
}

Button not showing up on Activity that opens from Fragment,but button shows up in Graphical Layout

I have created fragments(6 fragment) in my app.In my first fragment(android_frag) there is a button "Show Places on Map". When I click on that button new activity names "ShowPlaces" opens up. In the show_places xml file I have created one button that should return me to the previous screen i.e the android fragment but button is not showing up in when the ShowPlaces activity opnes up, though I can see the button in graphical layout of the xml file. I have not been able to find out where the problem is.Please help.
Here is my show_places.xml file
<?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="center"
android:orientation="vertical" >
<!-- Show on Map button -->
<Button
android:id="#+id/btn_show_map1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="#string/showplaces"
/>
</LinearLayout>
Here is the ShowPlaces.java file
package com.example.tabswipe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.app.Activity;
import android.view.Menu;
public class ShowPlaces extends Activity implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.show_places, container, false);
Button button = (Button) ios.findViewById(R.id.btn_show_map);
button.setOnClickListener(new OnClickListener()
{ #Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
Intent openStartingPoint = new Intent("com.example.tabswipe.Android");
startActivity(openStartingPoint);
}
});
return ios;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Looks like you have android:id="#+id/btn_show_map1" in the xml but are trying to find the id btn_show_map. These one should be changed to be the same as the other.

Can I use setContentView outside the oncreate method?

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Categories