I am working on a new app, and copied a little bit of the code of my other working app. The errorlog and problems-log doens't give me any errors, but when I press the buttons (test/test1/test2) nothing is happening.
MenuScreen.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("DEBUG", "test");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivityForResult(intent, 0);
}
});
findViewById(R.test1).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test1");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.test2).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test2");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
}
});
findViewById(R.id.verlaat_app).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test3");
MenuScreen.this.finish();
}
});
}
}
MenuScreen.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class FotoMaker extends Activity
{
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.testpic);
iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.testpic);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick (View v){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}
}
}
CHECK OUT THIS ANSWER i have made some corresctions....
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MenuScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById (R.id.test);
Button btn2 = (Button) findViewById (R.id.test1);
Button btn3 = (Button) findViewById (R.id.test2);
Button btn4 = (Button) findViewById (R.id.verlaat_app);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("DEBUG", "test");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test1");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test2");
Intent intent = new Intent(MenuScreen.this, FotoMaker.class);
startActivity (intent);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Log.d("DEBUG", "test3");
MenuScreen.this.finish();
}
});
}
}
Related
Hi there i have a problem error that from NextActivity cannot go to the MainActivity3. Here is the Code NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= findViewById(R.id.textView);
textView.setText(message);
}
public void onBtnClick(View view){
Intent intent = getIntent();
Intent intent1= new Intent(this,MainActivity3.class);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message1 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE1);
TextView textView= findViewById(R.id.textView);
TextView plain1= findViewById(R.id.plain1);
TextView plain2= findViewById(R.id.plain2);
String b=plain1.getText().toString();
if(b.equals(message.toString())&&plain2.getText().toString().equals(message1.toString())){
startActivity(intent1);
}
else{
textView.setText("Password or Username is Wrong");
}
}
}
and here is the code of MainActivity3 that cannot be open through NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.ActionProvider;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent= getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
FrameLayout lay= (FrameLayout) findViewById(R.id.frames);
if(number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
}
else{
}
}
public void onBtnClick (View view){
Intent intent = new Intent(this,MainActivity2.class);
startActivity(intent);
}
}
The Problem is that through the NextActivity button click the apps will restart in it's own that supposed to go to the page of MainActivity3
I have fixed it by
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent = getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
if(number != null) {
FrameLayout lay = (FrameLayout) findViewById(R.id.frames);
if (number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
} else {
}
}
else{}
}
Maybe any others that have a better ways?
In my app, I have music playing when the app starts. If the user goes to settings they can change the volume or turn the music on and off, however I do not know how to recall the information from the radio buttons to turn the music on or off in other activities. I know if I turn the music off in the main activity under onCreate it will listen to the radio buttons, but I want music playing when the app starts and when you resume the activity it inputs the info from the radio buttons.
This is the code of my main activity
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent svc = new Intent(this, music.class
);
startService(svc);
}
public void onPlayPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), Game.class);
startActivity(myIntent);
}
public void onSettingsPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), Settings.class);
startActivity(myIntent);
}
public void onHowtoPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), HowtoPlay.class);
startActivity(myIntent);
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
Intent svc = new Intent(this, music.class);
stopService(svc);
}
}
and this is the code of my Settings activity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
public class Settings extends AppCompatActivity {
private RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final Intent svc = new Intent(this, music.class
);
RadioButton rdb = (RadioButton) findViewById(R.id.on);
rdb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((RadioButton) v).isChecked();
// Check which radiobutton was pressed
if (checked){
startService(svc);
}
else{
stopService(svc);
}
}
});
RadioButton rdb2 = (RadioButton) findViewById(R.id.off);
rdb2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((RadioButton) v).isChecked();
// Check which radiobutton was pressed
if (checked){
stopService(svc);
}
else{
startService(svc);
}
}
});
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.seekBar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
}
public void onMainMenuPressed(View view){
Intent myIntent=new Intent(getBaseContext(),MainActivity.class);
startActivity(myIntent);
}
public void onResumePressed (View view){
Intent myIntent=new Intent(getBaseContext(),Game.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
finish();
}
} ```
Use SharedPreferences with simple boolean flag.
You should use onCheckedChangeListener on the radio group rather than onClickListener on individual radio button, like so:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(...) {
getSharedPreferences("prefs", 0).edit().putBoolean("music", rdb.isChecked()).apply();
}
});
Then inside other activity:
boolean music = getSharedPreferences("prefs", 0).getBoolean("music", true);
Then check value of the flag to determine whether or not to start the music service.
$ second activity.java
package com.example.applincatio.buttonclick;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
public class Main2Activity extends Activity {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
private WebView mWebView;
ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewmoddle);
imageButton1=(ImageButton)findViewById(R.id.MyButton1);
imageButton2=(ImageButton)findViewById(R.id.MyButton2);
imageButton3=(ImageButton)findViewById(R.id.MyButton3);
imageButton4=(ImageButton)findViewById(R.id.MyButton4);
mWebView = (WebView) findViewById(R.id.moddlewebview);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mWebView.setWebViewClient(new myWebclient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://www.kluniversity.in/");
}
public class myWebclient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
$ main activity.java
package com.example.applincatio.buttonclick;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton button1;
ImageButton button2;
ImageButton button3;
ImageButton button4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button1 = (ImageButton) findViewById(R.id.MyButton1);
button2 = (ImageButton) findViewById(R.id.MyButton2);
button3 = (ImageButton) findViewById(R.id.MyButton3);
button4 = (ImageButton) findViewById(R.id.MyButton4);
// Capture button clicks
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
}
}
I want to load multiple urls in one webview
for example
if i click on image 1 then i want to open url 1
if i click on image 2 then i want to open url 2
like this each button should have different url
here I loaded only one URL, but I want to add three more URL by using if statements, but I cant change that.please anyone help me
thanks in advance
Use listeners for ur imageViews.
imageButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mWebView.loadUrl("your url");
}
});
I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
I am building an app in which I have 3 fragment tabs. In the first tab I have 4 buttons, which will open 4 different activities, but when I try to launch a new activity on button's onClick my app crashes. I will post my code here for first fragment tab which has 4 buttons in it.
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Fragment;
import android.content.Intent;
public class Tab1 extends Fragment {
Button btn1, btn2, btn3, btn4;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.clienttab1, container, false);
btn1 = (Button)rootView.findViewById(R.id.button1);
btn2 = (Button)rootView.findViewById(R.id.button2);
btn3 = (Button)rootView.findViewById(R.id.button3);
btn4 = (Button)rootView.findViewById(R.id.button4);
addListenerOnButton();
return rootView;
}
public void addListenerOnButton() {
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Activity1.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Activity2.class);
startActivity(intent);
}
});
btn3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Activity3.class);
startActivity(intent);
}
});
btn4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Activity4.class);
startActivity(intent);
}
});
}
}