How to change a view from a different activity. - java

Right now my goal is to have the Colors activity allow users to hit a button to change the color they are drawing with. How do I get the instance of the canvas in my activity_main.xml to my Colors activity so I can change the color that is drawn on it?
activity_main.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="match_parent"
android:layout_height="match_parent"
android:background="#09B8C1"
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.app.MainActivity">
<com.app.color.DrawingView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/drawing"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFFFF"
android:layout_above="#+id/settings"
android:layout_alignParentTop="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeColor"
android:text="#string/Settings"
android:id="#+id/settings"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="#+id/button5"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="clearScreen" />
</RelativeLayout>
MainActivity.java
package com.app.color;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
DrawingView dv = new DrawingView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeColor(View view)
{
Intent intent = new Intent(MainActivity.this, Colors.class);
startActivity(intent);
}
public void clearScreen(View view)
{
}
}
Colors.java
package com.app.color;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Colors extends AppCompatActivity {
DrawingView dv = new DrawingView(,);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colors);
}
public void toWhite(View view)
{
dv.setColor("#ffffff");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
public void toYellow(View view)
{
dv.setColor("#ffff00");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
public void toBlue(View view)
{
dv.setColor("#0000ff");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
public void toRed(View view)
{
dv.setColor("#ff0000");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
public void toGreen(View view)
{
dv.setColor("#00ff00");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
public void toBlack(View view)
{
dv.setColor("#00000");
Intent intent = new Intent(Colors.this, MainActivity.class);
startActivity(intent);
}
}
DrawingView.java
package com.app.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawingView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Get the coordinates of the touch event.
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Set a new starting point
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
// Connect the points
path.lineTo(eventX, eventY);
break;
default:
return false;
}
// Makes our view repaint and call onDraw
invalidate();
return true;
}
public void setColor(String newColor){
invalidate();
int paintColor = Color.parseColor(newColor);
paint.setColor(paintColor);
}
}
activity_colors.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="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.app.Colors">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Choose Color"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:onClick="toWhite"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffffff"
android:text="#string/white" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:onClick="toBlue"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#153fc7"
android:text="#string/blue" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/red"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:onClick="toRed"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#f01010" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/yellow"
android:id="#+id/button4"
android:background="#f7e80f"
android:layout_below="#+id/button3"
android:onClick="toYellow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/button3"
android:layout_alignEnd="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Green"
android:id="#+id/button6"
android:layout_below="#+id/button4"
android:onClick="toGreen"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#00ff00" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Black"
android:id="#+id/button7"
android:layout_below="#+id/button6"
android:background="#000000"
android:onClick="toBlack"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

My understanding is that, you want to change the color of DrawingView view on any event (ex: button click).
For that please follow the below code :
Firstly, create the attribute styles in attrs.xml
<declare-styleable name="DrawingView">
<!-- drawing view color attributes -->
<attr name="dv_background_color" format="color" />
<attr name="dv_foreground_color" format="color" />
<!-- You can add other custom attributes also -->
<attr name="dv_padding" format="dimension" />
<attr name="dv_width" format="dimension" />
</declare-styleable>
Now write the below code in your java class file :
public class DrawingView extends View {
//Default colors
private int mDrawingViewBackgroundColor = 0xFF0000FF;
private int mDrawingViewForegroundColor = 0xFF0000FF;
//Default padding
private int mViewPadding = 5;
private int mViewWidth = 10;
private Paint mForeGroundColorPaint = new Paint();
private Path mBackGroundColorPaint = new Path();
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typeArr = context.obtainStyledAttributes(attrs, R.styleable.DrawingView);
try {
//Inflate all the attributes of DrawingView from xml that are initialize in activity_main.xml layout file.
mDrawingViewBackgroundColor = typeArr.getColor(R.styleable.DrawingView_dv_background_color, mDrawingViewBackgroundColor);
mDrawingViewForegroundColor = (R.styleable.DrawingView_dv_background_color, mDrawingViewForegroundColor);
mViewPadding = (int)typeArr.getDimension(R.styleable.DrawingView_dv_padding, mViewPadding);
mViewWidth = (int)typeArr.getDimension(R.styleable.DrawingView_dv_width, mViewWidth);
}finally {
typeArr.recycle();
}
setupPaints();
}
setupPaints() {
//Render DrawingView colors - Foreground color
mForeGroundColorPaint.setColor(mDrawingViewForegroundColor);
mForeGroundColorPaint.setAntiAlias(true);
mForeGroundColorPaint.setStrokeWidth(5f);
mForeGroundColorPaint.setStyle(Paint.Style.STROKE);
mForeGroundColorPaint.setStrokeJoin(Paint.Join.ROUND);
//Render DrawingView colors - Background color
mBackGroundColorPaint.setColor(mDrawingViewBackgroundColor);
mBackGroundColorPaint.setAntiAlias(true);
mBackGroundColorPaint.setStrokeWidth(5f);
mBackGroundColorPaint.setStyle(Paint.Style.STROKE);
mBackGroundColorPaint.setStrokeJoin(Paint.Join.ROUND);
}
#Override
public void invalidate() {
setupPaints();
super.invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
}

Related

'startActivityForResult(android.content.Intent, int)' is deprecated <<Java>>

how to solve this problem
'startActivityForResult(android.content.Intent, int)' is deprecated
here is for my MainActivity.java
`
package com.example.praktikump10;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnMoveActivity;
private Button btnMoveWithDataActivity;
private Button btnDialPhone;
private Button btnMoveWithObject;
private Button btnMoveForResult;
private TextView tvResult;
private int REQUEST_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMoveActivity = (Button) findViewById(R.id.btn_move_activity);
btnMoveActivity.setOnClickListener(this);
btnMoveWithDataActivity = (Button) findViewById(R.id.btn_move_activity_data);
btnMoveWithDataActivity.setOnClickListener(this);
btnDialPhone = (Button) findViewById(R.id.btn_dial_number);
btnDialPhone.setOnClickListener(this);
btnMoveWithObject = (Button) findViewById(R.id.btn_move_activity_object);
btnMoveWithObject.setOnClickListener(this);
btnMoveForResult = (Button) findViewById(R.id.btn_move_for_result);
btnMoveForResult.setOnClickListener(this);
tvResult = (TextView) findViewById(R.id.tv_result);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_move_activity:
Intent moveIntent = new Intent(MainActivity.this, MoveActivity.class);
startActivity(moveIntent);
break;
case R.id.btn_move_activity_data:
Intent moveWithDataIntent = new Intent(MainActivity.this, MoveWithDataActivity.class);
moveWithDataIntent.putExtra(MoveWithDataActivity.EXTRA_NAME, "Hafidz Izdihar Muslim\n");
moveWithDataIntent.putExtra(MoveWithDataActivity.EXTRA_AGE, 20);
startActivity(moveWithDataIntent);
break;
case R.id.btn_dial_number:
String phoneNumber = "087878336770";
Intent dialPhoneIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
startActivity(dialPhoneIntent);
break;
case R.id.btn_move_activity_object:
Person mPerson = new Person();
mPerson.setName("DicodingAcademy");
mPerson.setAge(5);
mPerson.setEmail("academy#dicoding.com");
mPerson.setCity("Bandung");
Intent moveWithObjectIntent = new Intent(MainActivity.this, MoveWithObjectActivity.class);
moveWithObjectIntent.putExtra(MoveWithObjectActivity.EXTRA_PERSON, mPerson);
startActivity(moveWithObjectIntent);
break;
case R.id.btn_move_for_result:
Intent moveForResultIntent = new Intent(MainActivity.this, MoveForResultActivity.class);
startActivityForResult(moveForResultIntent, REQUEST_CODE);
break;
}
}
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == MoveForResultActivity.RESULT_CODE) {
int selectedValue = data.getIntExtra(MoveForResultActivity.EXTRA_SELECTED_VALUE, 0);
tvResult.setText("Hasil : " + selectedValue);
}
}
}
}
`
and this is my activity_main.xml
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.praktikump10.MainActivity">
<Button
android:id="#+id/btn_move_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity" />
<Button
android:id="#+id/btn_move_activity_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity Dengan Data" />
<Button
android:id="#+id/btn_dial_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Dial a Number" />
<Button
android:id="#+id/btn_move_activity_object"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity dengan Object" />
<Button
android:id="#+id/btn_move_for_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pindah Activity untuk Result" />
<TextView
android:id="#+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hasil"
android:textSize="24sp" />
</LinearLayout>
`
this is for my MoveForResultActivity.java
`
package com.example.praktikump10;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import com.example.praktikump10.R;
public class MoveForResultActivity extends AppCompatActivity
implements View.OnClickListener {
private Button btnChoose;
private RadioGroup rgNumber;
public static String EXTRA_SELECTED_VALUE = "extra_selected_value";
public static int RESULT_CODE = 110;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_for_result);
btnChoose = (Button) findViewById(R.id.btn_choose);
btnChoose.setOnClickListener(this);
rgNumber = (RadioGroup) findViewById(R.id.rg_number);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_choose) {
if (rgNumber.getCheckedRadioButtonId() != 0) {
int value = 0;
switch (rgNumber.getCheckedRadioButtonId()) {
case R.id.rb_50:
value = 50;
break;
case R.id.rb_100:
value = 100;
break;
case R.id.rb_150:
value = 150;
break;
case R.id.rb_200:
value = 200;
break;
}
Intent resultIntent = new Intent();
resultIntent.putExtra(EXTRA_SELECTED_VALUE, value);
setResult(RESULT_CODE, resultIntent);
finish();
}
}
}
}
`
activity_move_for_result.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.praktikump10.MoveForResultActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="Pilih angka yang kamu suka" />
<RadioGroup
android:id="#+id/rg_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="#+id/rb_50"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="50" />
<RadioButton
android:id="#+id/rb_100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="100" />
<RadioButton
android:id="#+id/rb_150"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="150" />
<RadioButton
android:id="#+id/rb_200"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="200" />
</RadioGroup>
<Button
android:id="#+id/btn_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih" />
</LinearLayout>
`
why the
'startActivityForResult(android.content.Intent, int)' is deprecated
on MainActivity.java
any answer i really appreciated
thank you for the valueable answer...

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>

What should i do to move to another activity when only special conditions are met

I am making a quiz app. I want the exit page to be displayed when the next button is pressed after last question has been answered. I have a activity_questions.xml and a activity_exit.xml. The xml for activity_questions.xml is :
<?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.android.crystal.questions">
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:padding="10dp"
android:text=""
android:textColor="#440027"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text3"
android:layout_margin="30dp"
android:background="#9775AA"
android:hint="Answer"
android:inputType="text"
android:padding="10dp"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edit2"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/answer"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onAnswerClick"
android:padding="2dp"
android:text="Okay" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onHintClick"
android:padding="2dp"
android:text="Hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:orientation="vertical"
android:layout_marginTop="26dp"
android:layout_below="#+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/tickcross"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ll"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wierdtick" />
<TextView
android:id="#+id/correctornot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tickcross"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Correct!"
android:textColor="#440027"
android:textSize="30dp" />
<Button
android:id="#+id/nextbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next"
android:onClick="onNextClick"
/>
</LinearLayout>
</RelativeLayout>
And the java is:
package com.example.android.crystal;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class questions extends AppCompatActivity {
Random r = new Random();
private boolean done;
private int QuestionNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
String[] questions = getResources().getStringArray(R.array.Questions);
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
}
public void onFinishClick(View view){
Intent intent = new Intent(this, exit.class);
startActivity(intent);
}
public static void hideKeyboardFrom(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void answersubmitted() {
findViewById(R.id.tickcross).setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
findViewById(R.id.tickcross).startAnimation(animation);
findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);
}
public void onHintClick(View view) {
String[] hints = getResources().getStringArray(R.array.Hints);
Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
toasty.show();
}
public void onAnswerClick(View view) {
if (done == false) {
hideKeyboardFrom(this);
String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
String[] answers = getResources().getStringArray(R.array.Answers);
String correctAnswer = answers[QuestionNo];
correctAnswer = correctAnswer.toUpperCase();
answer = answer.toUpperCase();
if (answer.equals(correctAnswer)) {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("CORRECT!");
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.wierdtick));
answersubmitted();
} else {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("Correct Answer: " + correctAnswer);
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.weirdcross));
answersubmitted();
}
done = true;
}
}
public void onNextClick(View view){
if (done){
String[] questions = getResources().getStringArray(R.array.Questions);
if (QuestionNo < (questions.length - 1)) {
QuestionNo += 1;
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
EditText et = (EditText) findViewById(R.id.edit2);
et.setText("");
done = false;
}
else {
onFinishClick();
}
}
}
}
But the onFinishClick() gives an error
Thanks in advance for your help
What should i do to move to another activity when only special conditions are met
Basically, an if-statement and an Intent.
Raw code with no variables added:
if(condition(s)){
Intent i = new Intent(this, TargetActivity.class);
//Put arguments into the intent if you need them...
startActivity(i);
}
You can put it in a thread, in an onClickListener, anywhere and any way you want to execute the code.
As to the error you are receiving, I cannot help you with that until you supply the stacktrace from Logcat
You can do like this:
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mQuestionCount < questionList.size()){
// set next question
} else {
// call finish
}
}
});

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)
}

Adding a Icon to notification area in android app

O.K i have looked around this site and others and i see something similar to what i am wanting but yet it isn't what i am wanting...let me explain
i am learning to build an android app using eclipse that has a media player linked to my shoutcast server i am running, so far the apps works great but it doesn't have an icon on the top were the notification area is and when i go to a different app (a game) to play while leaving the music playing it minimizes the app .. thats fine but when i reopen the app it doesnt stop the mediaplayer infact if i play the player i will have 2 players running i will then have to force stop it using the task manager,, i know this can be done as i have apps on my phone that has icons and it will do what i am wanting
when i searched this site i found a link to youtube that has soemthing similar to what i am wanting
http://www.youtube.com/watch?v=e74z0_Z5QWI&feature=relmfu the code that is shown is as followed
import android.app.Activity;
public class StatusBar extends Activity implements OnclickListener{
NotificationManager nm;
#Override
Protected void onCreate(bundle savedInstance){
//TODO Auto-generated method Stub
super.oncreate(savedInstaceState);
setContentView(R.layout.statusbar);
Button stat = (Button)findViewById(R.id.bStatusBar);
stat.setOnClickListener(this);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public void onClick(View v){
//TODO Auto-generated method Stub
Intent intent = new Intent(this, StatusBar.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
}
i have add this code to my MainActivity.java but it spits several errors to me that i have tried to fix using quickfix here is the script i currently have in my MainActivity.java file
package com.myapp.mytapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private Button btn_play,
btn_pause,
btn_stop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private final String URL = "http://link to url:8002";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() {
#Override
public void run() {
updateSeekProgress();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btn_play = (Button)findViewById(R.id.btn_play);
btn_play.setOnClickListener(this);
btn_pause = (Button)findViewById(R.id.btn_pause);
btn_pause.setOnClickListener(this);
btn_pause.setEnabled(true);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(true);
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
seekBar.setSecondaryProgress(percent);
}
#Override
public void onCompletion(MediaPlayer mp) {
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar tmpSeekBar = (SeekBar)v;
mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
}
return false;
}
#Override
public void onClick(View view) {
try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
lengthOfAudio = mediaPlayer.getDuration();
} catch (Exception e) {
//Log.e("Error", e.getMessage());
}
switch (view.getId()) {
case R.id.btn_play:
playAudio();
break;
case R.id.btn_pause:
pauseAudio();
break;
case R.id.btn_stop:
stopAudio();
break;
default:
break;
}
updateSeekProgress();
}
private void updateSeekProgress() {
if (mediaPlayer.isPlaying()) {
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
handler.postDelayed(r, 1000);
}
}
private void stopAudio() {
mediaPlayer.stop();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
seekBar.setProgress(0);
}
private void pauseAudio() {
mediaPlayer.pause();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
}
private void playAudio() {
mediaPlayer.start();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}
public void homepage (View view) {
goToUrl ( "http://myhomepage.com/");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
here is what is in my activity_main.xml file
<?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:background="#color/black"
android:icon="#drawable/ic_launcher"
android:orientation="vertical"
startForeground()>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="homepage"
android:text="Home Page" />
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_play"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/play"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/play" >
</Button>
<Button
android:id="#+id/btn_pause"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/pause"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/pause" >
</Button>
<Button
android:id="#+id/btn_stop"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/stop"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/stop" >
</Button>
</LinearLayout>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp" >
</SeekBar>
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/scast" />
<TextView
android:id="#+id/Textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="#string/version" >
</TextView>
</LinearLayout>
can someone tell me what i did wrong or can rewrite what i have and still keep everything working
Thanks in advance
Rob

Categories

Resources