Android App - Clicking on Button does not open new activity - java

My problem is as follows: I have an activity activity transport. On this activity I have one button Auto which should open the new activity: activity_auto. Whenever I click on this button the first activity activity transport opens again. No errors are shown and the references are okay, I think.
I attached my code snippet
Class: activity_transport
package com.group6.travlhoe;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class activity_transport extends AppCompatActivity implements View.OnClickListener {
Button btnAuto;
private BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
btnAuto = (Button) findViewById(R.id.Auto);
btnAuto.setOnClickListener(this);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item){
if (item.getItemId()==R.id.menu_start){
startActivity(new Intent(activity_transport.this, MainActivity.class));
} else if(item.getItemId()==R.id.menu_allgemein){
startActivity(new Intent(activity_transport.this, activity_allgemein.class));
} else if(item.getItemId()==R.id.menu_transport){
startActivity(new Intent(activity_transport.this, activity_transport.class));
} else if(item.getItemId()==R.id.menu_rechnung){
startActivity(new Intent(activity_transport.this, activity_rechnung.class));
} else if(item.getItemId()==R.id.menu_unterkunft){
startActivity(new Intent(activity_transport.this, activity_unterkunft.class));
}
return true;
}
});
bottomNavigationView.setSelectedItemId(R.id.menu_transport);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.Auto) {
startActivity(new Intent(activity_transport.this, activity_auto.class));
}
}
}
The XML-file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#drawable/hintergrund">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="0.3" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/Auto"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:alpha="0.75"
android:background="#mipmap/buttom_col"
android:text="Auto"
android:textStyle="bold"/>
<Button
android:id="#+id/Flugzeug"
android:alpha="0.9"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#mipmap/buttom_col"
android:text="Flugzeug"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/Taxi"
android:alpha="0.9"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#mipmap/buttom_col"
android:text="Taxi"
android:textStyle="bold"/>
<Button
android:id="#+id/Bahn"
android:alpha="0.75"
android:text="Bahn"
android:textStyle="bold"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#mipmap/buttom_col" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="0.3" >
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="top"
android:background="#drawable/shadow" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#color/colorWhite"
design:menu="#menu/navigation" />
</FrameLayout>
</LinearLayout>

try this
Intent it=new Intent(getApplicationContext(),activity_auto.class);
startActivity(it)
You may also try
getBaseContext()
If none works use
activity_transport.this.startActivity
instead of
startActivity

You need to add a Listener.Try out the code below
btnAuto.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.getId() == R.id.Auto) {
startActivity(new Intent(activity_transport.this, activity_auto.class));
}
}
});

Related

How can my buttons be visible after adding a new code?

my code works well, with 2 buttons ("home" and "chiudi") at the bottom, but I wanted to add more code to my webview app, and this new code hides my buttons.
This is the new code I want to add:
...
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url)
{
if (url.startsWith("mailto:") || url.startsWith("tel:") || url.startsWith("geo:") ||
url.startsWith("http:") || url.startsWith("https:")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("whatsapp:")) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
return true;
} else if (url.startsWith("spotify:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"spotify:album:0sNOF9WDwhWunNAHPD3Baj"));
intent.putExtra(Intent.EXTRA_REFERRER,
Uri.parse("android-app://" + context.getPackageName()));
startActivity(intent);
return true;
}
return false;
}
});
...
And this is my code:
for activity.main.xml:
...
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/button"
android:layout_width="183dp"
android:layout_height="41dp"
android:layout_marginStart="191dp"
android:layout_marginLeft="171dp"
android:layout_x="207dp"
android:layout_y="629dp"
android:background="#0000ff"
android:onClick="clickexit"
android:text="Chiudi"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/swipe">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="183dp"
android:layout_height="41dp"
android:layout_x="207dp"
android:layout_y="629dp"
android:background="#0000ff"
android:onClick="gohome"
android:text="Home"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent">
</Button>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#0000ff"
android:textSize="40sp"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
...
For MainActivity.java:
...
package it...........;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.onesignal.OneSignal;
public class MainActivity extends AppCompatActivity {
private static final String ONESIGNAL_APP_ID = "a..............";
private SwipeRefreshLayout swipeRefreshLayout;
private WebView webView;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textview);
textView.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText("sto caricando");
}
}, 500); // delay of 2 seconds before setting a text to textView
textView.postDelayed(new Runnable() {
public void run() {
textView.setVisibility(View.GONE);
}
}, 2000);
// Enable verbose OneSignal logging to debug issues if needed.
OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
// OneSignal Initialization
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://d...app");
//per fare il refresh
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
webView.reload();
}
}, 2000);
}
});
swipeRefreshLayout.setColorSchemeColors(
getResources().getColor(android.R.color.holo_blue_dark),
getResources().getColor(android.R.color.holo_orange_dark),
getResources().getColor(android.R.color.holo_green_dark),
getResources().getColor(android.R.color.holo_red_dark));
}
public void gohome(View v)
{
webView.loadUrl("https://...app");
}
public void clickexit(View v)
{
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
}
else
super.onBackPressed();
}
}
...
When I add the first code, my buttons disappear.
How can I fix this?
Thank you
Roberto
Your alignment of views is not correct and In ConstraintLayout you have arranged your views by giving reference to other views. It would be good to check this link for a better understanding of how to use ConstraintLayout.
<?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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:onClick="gohome"
android:text="Home"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:onClick="clickexit"
android:text="Chiudi"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/textview"
app:layout_constraintStart_toEndOf="#+id/button2" />
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textAlignment="center"
android:textColor="#0000ff"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to do enable a button and change its colour when textfield has some value?

I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?
I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.
I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.
package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;
public class LoginActivity extends AppCompatActivity {
private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});
login_activation_code.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}
#Override
public void afterTextChanged(Editable s) {
}
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);
}
}
});
login_signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
The XML file is below
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.kaapi.kaapimobileassistant.Activities.LoginActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="#drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:src="#drawable/ic_info_outline_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="#style/Widget.AppCompat.Button"
android:text="Activate"/>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />
<LinearLayout
android:id="#+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="#color/colorPrimary"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
try this
public void enableSubmitIfReady() {
Button login_button = (Button) findViewById(R.id.login_button);
if(login_activation_code.toString().trim().length()==0){
login_button.setClickable(false);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
} else {
login_button.setClickable(true);
login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
}
}

Android playing sounds from a fragment

I've found multiple similar posts but have been unable to resolve so posting my code for help.
I have 3 fragments that use a ViewPager with a PagerAdapter to allow for swiping between fragments. This project started with no fragments and I was able to play sounds from buttons clicked all from MainActivity.
Here's the code that worked.
MainActivity
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
MediaPlayer example4Sound;
MediaPlayer example5Sound;
MediaPlayer example6Sound;
MediaPlayer example7Sound;
MediaPlayer example8Sound;
MediaPlayer example9Sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
example1Sound = MediaPlayer.create(this, R.raw.example1);
example2Sound = MediaPlayer.create(this, R.raw.example2);
example3Sound = MediaPlayer.create(this, R.raw.example3);
example4Sound = MediaPlayer.create(this, R.raw.example4);
example5Sound = MediaPlayer.create(this, R.raw.example5);
example6Sound = MediaPlayer.create(this, R.raw.example6);
example7Sound = MediaPlayer.create(this, R.raw.example7);
example8Sound = MediaPlayer.create(this, R.raw.example8);
example9Sound = MediaPlayer.create(this, R.raw.example9);
}
public void playExampleSound1(View view) {
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
public void playExampleSound4(View view) {
example4Sound.start();
}
public void playExampleSound5(View view) {
example5Sound.start();
}
public void playExampleSound6(View view) {
example6Sound.start();
}
public void playExampleSound7(View view) {
example7Sound.start();
}
public void playExampleSound8(View view) {
example8Sound.start();
}
public void playExampleSound9(View view) {
example9Sound.start();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.anjosoft.MainActivity">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2"
android:onClick="playExampleSound2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3"
android:onClick="playExampleSound3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton4"
android:layout_column="0"
android:background="#drawable/button4"
android:onClick="playExampleSound4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton5"
android:layout_column="1"
android:background="#drawable/button5"
android:onClick="playExampleSound5" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton6"
android:layout_column="2"
android:background="#drawable/button6"
android:onClick="playExampleSound6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton7"
android:layout_column="0"
android:background="#drawable/button7"
android:onClick="playExampleSound7" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton8"
android:layout_column="1"
android:background="#drawable/button8"
android:onClick="playExampleSound8" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton9"
android:layout_column="2"
android:background="#drawable/button9"
android:onClick="playExampleSound9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sound"
android:id="#+id/checkBox"
android:layout_column="1" />
</TableRow>
</TableLayout>
I know it's not pretty. Should have made an array but nevertheless it worked.
So here's my problem. Using the same ideology I tied the same thing with fragment.
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
FragmentOne.java
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
return inflater.inflate(R.layout.fragment_one_layout,container,false);
}
public void playExampleSound1(View view){
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
}
fragnment_one_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#f70808">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1"
android:nestedScrollingEnabled="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Clicking on imagebutton crashes the app. I know the MediaPlayer can work inside the app because I tested inserting example1Sound.start(); like this...
Snippet from FragmentOne.java
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
example1Sound.start();
The sound plays when the fragment loads. How do I get the onclick to work?
You can use onClickListeners for each button, it works.
I think, fragnment_one_layout.xml is linked to FragmentOne.java using inflater
but the fragnment_one_layout.xml has no access to FragmentOne.java , and hence
the statement onClick="methodName" is not able to access the desired method.
mediaPlayer object has no connection with your problem.
You can make one function that redirect sound from Activity to Fragment!
in Activity
fun onSound(sound:Int){
mp = MediaPlayer.create(this,soundFile)
mp.start
}
from fragment
onResume{
(activity as ActivityName).onSound(R.raw.sound)
}

Trying to run program in Android Studio but get error that my app has stopped working

I keep getting the error in my Android Studio Emulator (Nexus_7_API_21) that my app has stopped working whenever i try to run it. I launched LogCat and I think the error is due to a null pointer reference but i don't know where the problem is.
Here is my .java main file.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity
{
RelativeLayout bg;
RadioButton r1;
RadioButton r2;
RadioButton r3;
RadioButton r4;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bg = (RelativeLayout) findViewById(R.id.back);
r1 = (RadioButton) findViewById(R.id.rButton1);
r2 = (RadioButton) findViewById(R.id.rButton2);
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
r1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
r4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
return;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my XML file (activity_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/back"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#210d0a0d">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/welcomeString"
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="180dp"
android:layout_height="150dp" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton1"
android:id="#+id/rButton1"
android:layout_gravity="center_vertical"
android:checked="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton2"
android:id="#+id/rButton2"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton3"
android:id="#+id/rButton3"
android:layout_gravity="center_vertical" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rButton4"
android:id="#+id/rButton4"
android:layout_gravity="center_vertical" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
r1 = (RadioButton) findViewById(R.id.rButton3);
r1 = (RadioButton) findViewById(R.id.rButton4);
is problem. You are using r1 instead of r3 and r4.
r3 = (RadioButton) findViewById(R.id.rButton3);
r4 = (RadioButton) findViewById(R.id.rButton4);

A button with 3 pages, the 3rd one didn't work

I have a page that has a button, when you click it, it goes to another page with another button, when I click the button that takes to third page, it doesn't! I tried to make MainActive2.java and PageOne2.java but it didn't work! Help!
active_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical"
android:textStyle="italic" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/abus"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="About us"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="italic" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
abus.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textStyle="italic"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/abus2"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="About us"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="italic" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
abus2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#1d72c3"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textStyle="italic"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textStyle="italic"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="Hello" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.d.da;
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;
#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);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.da;
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);
}
}
You didn't add OnClickListener to a button on second activity
EDIT:
package com.d.da;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button2 = (Button) findViewById(R.id.abus2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainActivity2.class);
startActivity(intent);
}
});
}
}
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
button1 = (Button) findViewById(R.id.abus2);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//do things here
Intent intent = new Intent(PageOne.this, OtherActivity.class);
PageOne.this.startActivity(intent);
}
});
}
}
Also, you honestly should use some more descriptive IDs. You ARE going to have access to them throughout the application via the R resource class, which means having abus1 abus2 abus3 abus4 abus5 in your activities will eventually get extremely confusing.

Categories

Resources