VideoView is not working and black screen - java

I want to make a video view running at my started_activity background. But it fails to load.
The screen is black and if I navigate or push my page to another page 2 times, it will show app is not responding alert.
I follow this video tutorial: https://www.youtube.com/watch?v=tPeDn18FrGY&t=210s
started_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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=".StartedActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<Button
android:id="#+id/btnToLoginPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:text="Button"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
ActiviyStarted.java
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class StartedActivity extends AppCompatActivity {
private VideoView videoview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_started);
videoview = (VideoView) findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video_background);
videoview.setVideoURI(uri);
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
}
});
Button btn = (Button) findViewById(R.id.btnToLoginPage);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(StartedActivity.this, LoginActivity.class);
StartedActivity.this.startActivity(i);
}
});
}
}
Code with video view
Code without video view
Video Path and error message

Try this way:
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
videoview.stopPlayback();
videoview.setVideoURI(uri);
videoview.start();
}
});
videoview.setVideoURI(uri);
videoview.start();
You can also set setOnErrorListener to check for the error:
videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
//check error here
return false;
}
});

Related

Unable to scroll to top in android WebView

Am working on my first project in android, Since am creating a browser i added progress bar and swipe to refresh option.
when i scrolled to page bottom then i tied to scroll to the top of the page but swipe refresh option is triggered, when i tried to scroll to top.
Problem:
unable to scroll to top of the page.
someone help me solve this issue
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout 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:id="#+id/mySwipeLayout"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/myLinearLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="3dp">
<ProgressBar
android:id="#+id/myProgressBar"
android:layout_weight="0.1"
style="#style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/myImageView"
android:src="#mipmap/ic_launcher"
android:layout_weight="0.9"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<WebView
android:id="#+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
mainactivity.java
package com.sanoj.goproxy;
import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ProgressBar superProgressBar;
ImageView superImageView;
WebView superWebView;
LinearLayout superLinearLayout;
SwipeRefreshLayout superSwipeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
superSwipeLayout = findViewById(R.id.mySwipeLayout);
superLinearLayout = findViewById(R.id.myLinearLayout);
superProgressBar = findViewById(R.id.myProgressBar);
superImageView = findViewById(R.id.myImageView);
superWebView = findViewById(R.id.myWebView);
superProgressBar.setMax(100);
superWebView.loadUrl("http://safebrowser.tk");
superWebView.getSettings().setJavaScriptEnabled(true);
superWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
superLinearLayout.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
superLinearLayout.setVisibility(View.GONE);
superSwipeLayout.setRefreshing(false);
super.onPageFinished(view, url);
}
});
superWebView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
superProgressBar.setProgress(newProgress);
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
getSupportActionBar().setTitle(title);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
superImageView.setImageBitmap(icon);
}
});
superSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
superWebView.reload();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.super_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_back:
onBackPressed();
break;
case R.id.menu_forward:
onForwardPressed();
break;
case R.id.menu_refresh:
superWebView.reload();
break;
}
return super.onOptionsItemSelected(item);
}
private void onForwardPressed(){
if (superWebView.canGoForward()){
superWebView.goForward();
} else {
Toast.makeText(getApplicationContext(), "Can't Go Further..!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onBackPressed() {
if (superWebView.canGoBack()){
superWebView.goBack();
}else {
finish();
}
}
}
I used mScrollView.fullScroll(mScrollView.FOCUS_DOWN); to scroll to top,
and thanks to https://stackoverflow.com/a/8684666/3836908 credit to the answer

Android: Display images in full screen with caption on `onClick` event

I'm a beginner to the android development. I have an android image view that import web image by using Picasso 2.5.2 library. It's working. After click on that imageview, I want to create a full screen Android dialog with that image. I' used following code for it. But after click on the image view, a dialog is displayed without full screen. Finally, I want to zoom in and zoom out that full screen dialog. I want this kind of full screen on imageview onClickListener. Also see how the below image has a title and I also need the image caption (like in Facebook)
Here is my code
Mainactivity.java
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
ImageView imageView1;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUi();
}
public void initUi(){
imageView1 = (ImageView)findViewById(R.id.image);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView1);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showImage();
}
});
}
public void showImage() {
Dialog builder = new Dialog(this, android.R.style.Theme_Light);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(this);
Picasso.with(this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.into(imageView);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
builder.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You can use Chrisbane's PhotoView lib for zooming the image.
Open the detail page by sending the path,
public void showImage() {
Intent intent = new Intent(this, ImageDeatilActvity.class);
intent.putExtra("path", "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");
startActivity(intent);
}
Complete ImageDeatilActvity.java
public class ImageDeatilActvity extends AppCompatActivity implements View.OnSystemUiVisibilityChangeListener {
private boolean mIsFullScreen;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_deatil_actvity);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
mIsFullScreen = true;
String path = getIntent().getStringExtra("path");
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
#Override
public void onPhotoTap(View view, float x, float y) {
updateView();
}
#Override
public void onOutsidePhotoTap() {
updateView();
}
});
Glide.with(this).load(path).into(photoView);
}
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
mIsFullScreen = false;
}
updateView();
}
public void updateView() {
mIsFullScreen = !mIsFullScreen;
if (mIsFullScreen) {
hideSystemUI();
} else {
showSystemUI();
}
}
private void hideSystemUI() {
mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
}
private void showSystemUI() {
mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Detail Activity Layout,
<?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="in.muthu.stackoverflow.ImageDeatilActvity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#33000000"
android:fitsSystemWindows="true"
app:contentScrim="#android:color/transparent"
app:popupTheme="#style/AppTheme.AppBarOverlay"/>
<uk.co.senab.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In My example, I am using Glide not Picaso, because Android
official recommendation is Glide, If you want you can change that.
set image to centre crop and resize it with your screen size like
Picasso.with(MainActivity.this)
.load(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"))
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Use below xml for layout, i added one more ImageView just use make the visibility visible on click of small image_view
<?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:id="#+id/activity_add_info_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="Image buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
android:textSize="16sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"/>
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>

The third link is not working

I created a button to take me to another page and in that other page, there is another button that takes to another page, so page1, page2 and page3 that it's not working with me, check http://oi62.tinypic.com/2m7g4et.jpg when I click on "Fish guide" it takes me to the other one which has another button "Fish", when I click on "Fish" it doesn't take me to the 3rd page!
activity_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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fishguide"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish guide"
android:textSize="20sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fishguide.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:textStyle="italic" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="#+id/fish"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Fish"
android:textSize="22sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
fish.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="#1d72c3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:textStyle="italic" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/bala" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="Balabanka"
android:textSize="18sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fishguide);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
}
}
I did the third page, it runs but it was .. wrong
MainActivity2.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity2 extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
fishlink = (Button) findViewById(R.id.fish);
fishlink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageFish.class);
startActivity(intent);
}
});
}
}
PageFish.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageFish extends Activity {
Button fishlink;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fish);
}
}
Help :)
Edit / it goes "Fish guide" to "Fish" when I click on "Fish" it takes me again to "Fish" and so on, it doesn't go to the third page!
PageThree.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageThree.class);
startActivity(intent);
}
});
}
}
You have used
setContentView(R.layout.fishguide);
in PageThree.java
Change it to the xml you want to show with that third image in image. say, third.xml
setContentView(R.layout.third);
And you are done.
NOTE: You are getting the same Layout again and again because in PageThree.java you are starting the activity with intent PageThree.java. Instead just change that to PageFish.java.
So when you are at second layout (PageThree.java), just start activity for third layout (PageFish.java) on button click as follows -
PageThree.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageThree extends Activity {
Button button3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fishguide);
button3 = (Button) findViewById(R.id.fish);
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(PageThree.this, PageFish.class);
startActivity(intent);
}
});
}
}

image cannot be resolved or is not a field

I am doing an android project to create an animation using eclipse.
On MainActivity.java
it compiler shows 5 lint errors as follows
-image cannot be resolved or is not a field
-activity_main cannot be resolved or is not a field
-translate cannot be resolved or is not a field
-rotate cannot be resolved or is not a field
-shape cannot be resolved or is not a field
Here is doing there my code in MainActivity.java
package ahmed103.appdemo;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.*;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements AnimationListener {
ImageView image;
Animation animation1;
Animation animation2;
Button rotate, translate;
Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
rotate = (Button) findViewById(R.id.rotate);
translate = (Button) findViewById(R.id.translate);
image.setImageResource(R.drawable.shape);
animation1 = AnimationUtils.loadAnimation(this, R.anim.rotate);
animation2 = AnimationUtils.loadAnimation(this, R.anim.translate);
userInputHandler();
}
private void userInputHandler() {
rotate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
image.startAnimation(animation1);
}
});
translate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
image.startAnimation(animation2);
}
});
}
#Override
public void onAnimationEnd(Animation animation) {
image.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
image.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationStart(Animation animation) {
image.setVisibility(View.VISIBLE);
}
}
Here is down there my activity_main.xml file
<html>
<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:background="#DCDCDC"
android:orientation="vertical"
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=".MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="70dp"
android:minHeight="150dp"
android:minWidth="150dp" />
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<Button
android:id="#+id/rotate"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:text="#string/rotate_text" />
<Button
android:id="#+id/translate"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_weight="1"
android:text="#string/translate_text" />
</LinearLayout>
</html>
Remove the below import
import android.R;
Also you have
<html> and </html>// should be removed from xml
Imnport
import ahmed103.appdemo.R;
And use
<?xml version="1.0" encoding="utf-8"?>
as the first statement in your activity_main.xml

Problem with simple onClick action

Simple code which is copied from web doesnt seem to be working. There is no action when i click button but text of it is correctly changed. What's wrong?
package android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button closeButton = (Button)this.findViewById(R.id.button1);
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG);
}
});
closeButton.setText("dupa");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:id="#+id/button1" android:layout_height="wrap_content" android:text="#string/button"></Button>
</LinearLayout>
Thanks for any help,
Chris
You need to call .show() on your Toast.
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Klik!", Toast.LENGTH_LONG).show();
}

Categories