ScrollView auto-scroll off on android app open - java

I'm appending 5 video players in my app via java. When the app opens, the ScrollView's scroll-Y is positioned to show the 5th video player. How can I avoid this? How can I turn off auto-scroll so when the app opens, the 1st video player appears and the scroll-Y is 0?
Colors:
<color name="secondary_color">#6CABF7</color>
<color name="light_black">#202125</color>
<color name="dark_black">#0F0F0F</color>
activity_main.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"
android:background="#color/light_black"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#color/secondary_color"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:tag="yo">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
MainActivity.java:
package com.example.test;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.*;
import android.view.*;
import android.widget.*;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
LinearLayout content;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = findViewById(R.id.content);
for (int x=0;x<5;x++) {
RelativeLayout video_player_box = new RelativeLayout(this);
RelativeLayout.LayoutParams params;
VideoView player = new VideoView(this);
TextView details = new TextView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 600);
param.setMargins(0,0,0,50);
video_player_box.setLayoutParams(param);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(5,5,5,5);
player.setLayoutParams(params);
player.setMediaController(null);
video_player_box.addView(player);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 50);
params.setMargins(5,5,5,0);
details.setLayoutParams(params);
details.setPadding(15,0,0,0);
details.setText(Integer.toString(x+1));
details.setTextSize(25);
details.setTextColor(getResources().getColor(R.color.dark_black));
details.setGravity(Gravity.CENTER_VERTICAL);
video_player_box.addView(details);
content.addView(video_player_box);
}
((VideoView) ((RelativeLayout) content.getChildAt(0)).getChildAt(0)).setVideoURI(Uri.parse("https://dash.akamaized.net/akamai/bbb/bbb_320x180_60fps_600k.mp4"));
((VideoView) ((RelativeLayout) content.getChildAt(0)).getChildAt(0)).start();
}
}

Pasting this on my parent linear layout that comes right as the main child of the scrollview solved the issue for me.
android:descendantFocusability="blocksDescendants

Related

Android: Why does calling viewToFront() on a SeekBar not bring it to the top of the UI?

I am trying to make a hidden SeekBar (originally with visibility = "gone") visible and be displayed on top of a black background LinearLayout that covers the entire screen. However, when I run my code, I just see the fullscreen LinearLayout and I do not see a SeekBar that is on top of that LinearLayout:
Below is my code:
activity_main.xml:
<?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:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >
</LinearLayout>
<SeekBar
android:id="#+id/seekbar"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:max="100"
android:progressDrawable="#android:color/transparent"/>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Make the image preview box visible
SeekBar seekbar = findViewById(R.id.seekbar);
seekbar.setVisibility(View.VISIBLE);
seekbar.bringToFront();
// Place the image preview box at a specified location
seekbar.setX(150);
seekbar.setY(150);
seekbar.setVisibility(View.VISIBLE);
seekbar.bringToFront();
// Place the image preview box at a specified location
seekbar.setX(150);
seekbar.setY(150);
}
}
This doesn't answer your question but it may solve the problem. You can use a ConstraintLayout to position the SeekBar. It is a flat hierarchy so you can stack views on top of each other and then you can use visibility to hide/show it, instead of moving it into place it is always there.

Text not visible when used setAlpha()

I am simply trying to make night mode activity but when the background turns black the text is not visible on screen. Text Disappears when switched to Dark Mode. Any solutions?
I tried tv.setTextColor(Color.WHITE); is also not working when the screen is black.
Any help would be appreciated. Thanks in advance.
Here are the images:
Image 1
Image 2
Here is the 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="com.saipriyank.daynight.MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_margin="50px"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/background_view"
android:background="#android:color/background_dark"
android:alpha="0"/>
<com.mahfa.dnswitch.DayNightSwitch
android:id="#+id/dayNight"
android:layout_width="80dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is the Java Code:
package com.saipriyank.daynight;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.mahfa.dnswitch.DayNightSwitch;
import com.mahfa.dnswitch.DayNightSwitchListener;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DayNightSwitch dayNight = (DayNightSwitch)findViewById(R.id.dayNight);
final View background_view = (View)findViewById(R.id.background_view);
final TextView tv = (TextView)findViewById(R.id.tv);
dayNight.setDuration(450);
dayNight.setListener(new DayNightSwitchListener() {
#Override
public void onSwitch(boolean isNight) {
if(isNight){
Toast.makeText(MainActivity.this, "Night Mode Enabled",Toast.LENGTH_SHORT).show();
tv.setAlpha(0f);
background_view.setAlpha(1f);
}
else {
background_view.setAlpha(0f);
}
}
});
}
}
You are making the TextView invisible by calling tv.setAlpha(0f).
If you want to set the text color white, you can use tv.setTextColor(Color.White);

Button not moving from top-left to bottom-right when user touch screen

I want button to change its size and move from top left to bottom right when user touches screen.But my app stop running when i touch screen.If i delete part of code that change position of the button everything is ok and the button change its size.
Here is MainActivity.java
package com.example.user.a34;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ViewGroup main_layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_layout = (ViewGroup)findViewById(R.id.main_layout);
main_layout.setOnTouchListener(new RelativeLayout.OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
moveButton();
return true;
}
});
}
public void moveButton(){
View button1 = (View)findViewById(R.id.button1);
//Change position---------------------
RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
positionRules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
button1.setLayoutParams(positionRules);
//Change size-------------------------
ViewGroup.LayoutParams sizeRules = button1.getLayoutParams();
sizeRules.width = 350;
sizeRules.height = 200;
button1.setLayoutParams(sizeRules);
}
}
and here is activity_main.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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="38dp"
android:layout_marginLeft="66dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
The direct cause of your crash is likely trying to assign RelativeLayout.LayoutParams to a view that is in a ConstraintLayout and should thus have layout params of type ConstraintLayout.LayoutParams.
You either need to use a RelativeLayout in your xml, or assign your button layout params of type ConstraintLayout.LayoutParams.
I would suggest using a RelativeLayout in your xml as follows:
<?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:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.a34.MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="38dp"
android:layout_marginBottom="38dp"
android:layout_marginLeft="66dp"
android:layout_marginRight="66dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
and change your moveButton method as follows:
public void moveButton() {
View button1 = findViewById(R.id.button1);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)button1.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.width = 350;
layoutParams.height = 200;
button1.setLayoutParams(layoutParams);
}

Programmatically create scroll view in android studio with buttons.!

I am trying to make an app with this look enter image description here
but programmatically such that I can change the number of shown boxes in runtime.
I have tried the following, but no success-
1)MainActivity.java
package com.bigx.dynamictesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout=(RelativeLayout)findViewById(R.id.activity_relative);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for(int i = 0; i < 4; i++)
{
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int)(300 * getResources().getDisplayMetrics().density));
layoutParams.setMargins(0, 0, 0, (int)(20 * getResources().getDisplayMetrics().density));
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
textView.setText("TextView" + i);
linearLayout.addView(textView);
Button buttonA = new Button(this);
Button buttonB = new Button(this);
RelativeLayout.LayoutParams layoutParamsA = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_END, textView.getId());
RelativeLayout.LayoutParams layoutParamsB = new RelativeLayout.LayoutParams((int)(60 * getResources().getDisplayMetrics().density),(int)(60 * getResources().getDisplayMetrics().density));
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_LEFT, textView.getId());
layoutParamsA.addRule(RelativeLayout.ALIGN_PARENT_START, textView.getId());
buttonA.setLayoutParams(layoutParamsA);
buttonA.setText(String.valueOf(i));
linearLayout.addView(buttonA);
buttonB.setLayoutParams(layoutParamsB);
buttonB.setText(String.valueOf(i));
linearLayout.addView(buttonB);
}
relativeLayout.addView(linearLayout);
}
2) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
android:fillViewport="false"
android:background="#color/colorAccent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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.bigx.dynamictesting">
</RelativeLayout>
</ScrollView>
Which yields this result-enter image description here
Kindly suggest me how to achieve my objective.
Thank you.
P.S.-Sorry for my bad English.

Video player not working - Android Studio

I am trying to add a video played on button click with android studio.
However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.
Can you help me see where I'm going wrong.
Below is the code I approached the goal with
Trialvideo.java
package android.com.trialvideo;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class TrialVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** // Video view: to view our video
VideoView video = (VideoView) findViewById(R.id.surface_view);
//set video path to our video(in this case man-cheetah-gazalle.3gp)
video.setVideoPath("/raw/jeewan.mp4");
video.start();
**/
final Button play =(Button)findViewById(R.id.play);
play.setOnClickListener(new OnClickListener(){
public void onClick(View V){
videoPlayer();
}
});}
public void videoPlayer(){
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = (VideoView)findViewById(R.id.surface_view);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp");
videoHolder.requestFocus();
videoHolder.start();
}
}
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"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
<?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"
>
<Button
android:layout_height="50dip"
android:text="play"
android:id="#+id/play"
android:layout_width="50dip"
>
</Button>
<VideoView android:id="#+id/surface_view"
android:layout_width="475px"
android:layout_height="440px"
/>
</LinearLayout>
Hi try the following code:
VideoPlaying.java
public class VideoPlaying extends Activity {
private MediaController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.requestFocus();
vd.setVideoURI(uri);
vd.start();
}
}
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" android:gravity="center">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/VideoView"></VideoView>
</LinearLayout>
place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.
Android does not have a C: drive. You need to put the video file on the device (e.g., copy it to the device's external storage), then supply VideoView with an appropriate path to the file (e.g., using Environment.getExternalStorageDirectory()).

Categories

Resources