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?
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 have a simple app that has a Button and a TextView, when I try screen rotation, the TextView stays same while the Button disappears as below.
The Button comes back when I rotate the screen again.
I couldn't find a solution, is this a possible bug?
1- Why does this happening, I guess it's about onCreate() state, but why does TextView stays while the Button disappears?
2- How to solve this issue
3- I'm using Bundle to save the number, is it related to this problem?
I'm using the latest version of Android Studio. Here is my code
MainActivity.java
package com.example.kalicidegiskenler;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t1;
Button b1;
int say;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.text1);
b1 = (Button) findViewById(R.id.button);
if (savedInstanceState != null){
say = savedInstanceState.getInt("sayac");
t1.setText(String.valueOf(say));
}
else {
say = 0;
}
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
say ++;
t1.setText(String.valueOf(say));
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("sayac", say);
}
}
activity_main.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">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="159dp"
android:layout_marginLeft="159dp"
android:layout_marginBottom="513dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is because the button is drawn out of screen; somewhere above the toolbar.
With app:layout_constraintBottom_toBottomOf used in combination with a padding of 513dp, the button falls out of the screen width (or landscape height) of 320 dp.
If you align it directly under the text, it will be always visible:
<?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">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.12" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Button" />
</androidx.constraintlayout.widget.ConstraintLayout>
Pro-tip: Google advises to use multiples of 8dp for paddings, margins and sizes. This scales well on different screen sizes and it is aesthetically pleasing if views fall in a grid.
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'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.
I am new in java and xml programming, i am making tabbed activity with fragment, there is no error when running or debug the code but VideoView is not showing, i am using Android Studio,
the idea is i want to make tabbed activity, with textView and videoview in fragment as a content that scrollable(possible ?),one more thing is the API level somehow have influence ? my phone still on KitKat
here is the (fragment) code :
package com.panduanberwudhu;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;
public class MencuciTangan extends Fragment {
private VideoView player;
private String videopath;
private MediaController mediacon;
public View rootView;
//#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.mencucitangan_layout, container, false);
mediacon = new MediaController(getActivity());
player = (VideoView) rootView.findViewById(R.id.videoplayer);
videopath = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.washhand;
player.setVideoURI(Uri.parse(videopath));
player.setMediaController(mediacon);
mediacon.setAnchorView(player);
player.start();
return rootView;
//return inflater.inflate(R.layout.mencucitangan_layout,container,false);
}
}
and the layout code :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mencucitangan_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.panduanberwudhu.MencuciTangan">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoplayer"
android:layout_gravity="bottom"
/>
</LinearLayout>
</ScrollView>
Use this layout, where the height Relativelayout is wrap_content which will increase/wrap according to content because it is inside the ScrollView .
I removed Linearlayout as hard coded some value:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mencucitangan_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:id="#+id/ap"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<VideoView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:foregroundGravity="center"
android:id="#+id/videoplayer" />
</RelativeLayout>
</ScrollView>
Output: