I'm trying to play a video in a videoView, but I can't get it to play.
Here's my java code:
package com.ggblbl.videoView;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;
public class RandomVideoBlueActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_video_blue);
VideoView videoBlue = (VideoView) findViewById(R.id.videoViewBlue);
String videoPathBlue = "android.resource://"+getPackageName()+"/"+R.raw.blue_video;
Uri videoUriBlue = Uri.parse(videoPathBlue);
videoBlue.setVideoURI(videoUriBlue);
videoBlue.start();
}
}
My video name is video_blue.mp4.
https://www.dropbox.com/s/iefnweekgbb9owz/blue_video.mp4?dl=0
Here's my activity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.ggblbl.videoView.RandomVideoBlueActivity">
<VideoView
android:id="#+id/videoViewBlue"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've tested it on my LG L70 (D320n).
On my Motorola Moto G1 it does work, however.
I've done some testing and it seems the resolution of the video is too high for my poor, old phone...
I guess I'll have to find a way of scaling it down for every phone.
Related
I am a beginner to android. I want to slide a slidebar in a horizontal linear layout but when I run the code, the application ends giving error FallingBall keeps stopping
My xml:
<?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"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:weightSum="100"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="72"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="horizontal">
<ImageView
android:id="#+id/slideBar"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="#android:drawable/bottom_bar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/leftBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/left_but"
android:onClick="hitLeft"/>
<ImageButton
android:id="#+id/rightBut"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/right_but"
android:onClick="hitRight"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My java file is like this:
package com.example.fallingball;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView s = findViewById(R.id.slideBar);
float x = s.getX();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
void hitLeft(View view){
x--;
s.setX(30);
}
void hitRight(View view){
x++;
s.setX(40);
}
}
Note: leftBut and rightBut are two Image button and I have copied the images to the drawable folder so if you are trying this code on your system, make sure you adjust this else it will show you Image not found or so...
In MainActivity.java you should initialize your value into onCreate() method
ImageView s;
float x;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
s = findViewById(R.id.slideBar);
x = s.getX();
}
Well, I found my own mistake. The thing what hossein answered earlier was right but the application was still crashing. Actually the problem was coming that I was triggering the methods hitLeft and hitRight which were not initialized as public. That was the mistake and now it is working alright...
I am trying to get Glide to display a local jpeg file in an ImageView.
I have created this simple example that shows my problem. Normally, I obtain the URI programatically, but for this example I am just using the hard coded uri string from one of the files I am trying to display. When I run it, the ImageView is empty.
package com.example.g1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.image);
Glide.with(this)
.load("content://org.chromium.arc.file_system.fileprovider/download/assets/images/DSC01062.JPG")
.into(imageView);
}
}
And here is the layout
<?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"
tools:context=".MainActivity">
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="356dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>
Any idea on what I am doing wrong?
How do you change the calendar layout to vertical mode?
I am using CalendarView within ScrollView. Now I am implementing the scroll up and down view in Vertical scroll for getting previous or next months calendar.
I am trying to implement like below CalendarView.....
https://ibb.co/TLDFbrm "CalendarView Like that"
Please help me I am in trouble.
<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">
<CalendarView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
</CalendarView>
</LinearLayout>
</ScrollView>
Any ideas? Let me know! thanks...
Follow the below code.
Activity_main.xml code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="ganeshannt.calendarview.MainActivity">
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="51dp"
android:text="Date"
android:textAlignment="center"
android:textSize="45dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.219" />
<Button
android:id="#+id/btngocalendar"
android:layout_width="266dp"
android:layout_height="47dp"
android:text="Go to calendar"
tools:layout_editor_absoluteX="47dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
Create new calendar_layout.xml file (File ⇒ New ⇒Activity⇒Empty_activity).
Go to calendar_layout.xml then click the text bottom.
This xml file contains the designing code for android app,Into the calendar_layout.xml copy and paste the below code
calendar_layout.xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CalendarView
android:id="#+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Create new CalendarActivity.java file (File ⇒ New ⇒Java class).
Into the CalendarActivity.java copy and paste the below code.java programming is the backend language for android. Do not replace your package name otherwise app will not run.
CalendarActivity.java code
package ganeshannt.calendarview;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CalendarView;
public class CalendarActivity extends AppCompatActivity {
private static final String TAG = "CalendarActivity";
private CalendarView mCalendarView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {
String date = year + "/" + month + "/"+ dayOfMonth ;
Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);
Intent intent = new Intent(CalendarActivity.this,MainActivity.class);
intent.putExtra("date",date);
startActivity(intent);
}
});
}
}
I'm setting up an image button in Android Studio. All looks fine in design view, but when the application is loaded, the image button is moved to the top left corner and reduced in size?
Shown below:
Design View
XML Code - current
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".ui.PhysicalActivity">
<ImageButton
android:id="#+id/btnImageGallery"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:onClick="onImageGalleryClicked"
android:src="#drawable/placeholder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.921"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>
PhysicalActivity Code
package com.example.futura.ui;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.futura.R;
import java.io.File;
public class PhysicalActivity extends AppCompatActivity {
public static final int IMAGE_GALLERY_REQUEST = 20;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_physical1);
}
public void onImageGalleryClicked(View v){
// invoke the image gallery using an implict intent.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
// finally, get ui represenation
Uri data = Uri.parse(pictureDirectoryPath);
// set the data and type, get all image types
photoPickerIntent.setDataAndType(data, "image/*" );
startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
}
}
Preview
Does any one have any advice on correcting this?
Try add layout_constraintHorizontal_bias and layout_constraintStart_toStartOf
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<ImageButton
android:id="#+id/btnImageGallery"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginStart="8dp"
android:onClick="onImageGalleryClicked"
android:src="#drawable/placeholder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.921"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>
Remove margins (start, top, bottom, end) and adjust layout_constraintHorizontal_bias, layout_constraintVertical_bias to achieve desired result.
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/startSyncButton"
android:layout_width="200dp"
android:layout_height="300dp"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintVertical_bias="0.7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
i am a beginner programmer of Android, when i create an App and install it on my real phone or other phones it works very slowly (i dont talk about emulator).
- The size of file is only 3 MB
- This happens to all applications that i make
- I cleaned the cache and the project before building
the devices that i installed the application on : HTC630, Samsung galexy S7, Samsung Duos j1.
any idea ?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#drawable/bcc">
<LinearLayout
android:layout_width="0dp"
android:layout_height="495dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/edge"
android:layout_marginTop="#dimen/edge"
android:text="Enter your name:"
android:textColor="#FFFFFF"
android:textSize="#dimen/fsize"
android:textStyle="bold"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/edge"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textSize="#dimen/fsize"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/edge"
android:layout_marginRight="#dimen/edge"
android:onClick="nxt"
android:text="Next"
android:textSize="#dimen/fsize"
tools:layout_editor_absoluteX="38dp"
tools:layout_editor_absoluteY="0dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
java code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void nxt(View view) {
EditText name =(EditText)findViewById(R.id.editText2);
Intent txtint= new Intent(this,Main2Activity.class);
Bundle b = new Bundle();
b.putString("user",name.getText().toString());
txtint.putExtras(b);
startActivity(txtint);
}
}
Finally i solved the problem.. i had a mistake in resize of background (to fit screens).