I need to add a GIF in my code snippet on the screen, i need it to be played continuously
and without stopping I saw many blogs, QnAs on google but I didn't find the answer, here goes my MainActivity.java(Showing MainActivity.class as it was not allowing me to post the whole code) -
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView lightUp = findViewById(R.id.lightUp4);
Button unLight = findViewById(R.id.unlight);
ImageView AgarbattiBtn = findViewById(R.id.agarbattiBtn);
MediaPlayer Sound_matchstick = MediaPlayer.create(this, R.raw.matchstick);
MediaPlayer Om_Bhur = MediaPlayer.create(this, R.raw.om_bhur);
lightUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView image1 = findViewById(R.id.diya);
image1.setImageResource(R.drawable.lit_diya);
lightUp.setVisibility(View.INVISIBLE);
Sound_matchstick.start();
if(lightUp.getVisibility() == View.INVISIBLE && AgarbattiBtn.getVisibility() == View.INVISIBLE){
Om_Bhur.start();
}
}
});
unLight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView image2 = findViewById(R.id.diya);
image2.setImageResource(R.drawable.not_lit_diya);
AgarbattiBtn.setVisibility(View.VISIBLE);
findViewById(R.id.agrabatti).setVisibility(View.INVISIBLE);
lightUp.setVisibility(View.VISIBLE);
Om_Bhur.stop();
}
});
AgarbattiBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AgarbattiBtn.setVisibility(View.INVISIBLE);
findViewById(R.id.agrabatti).setVisibility(View.VISIBLE);
if(lightUp.getVisibility() == View.INVISIBLE && AgarbattiBtn.getVisibility() == View.INVISIBLE){
Om_Bhur.start();
}
}
});
}
}
This is my 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">
<ImageView
android:id="#+id/mandir"
android:layout_width="508dp"
android:layout_height="521dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39"
app:srcCompat="#mipmap/mandir" />
<Button
android:id="#+id/unlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.96" />
<ImageView
android:id="#+id/diya"
android:layout_width="78dp"
android:layout_height="62dp"
app:layout_constraintBottom_toBottomOf="#+id/mandir"
app:layout_constraintEnd_toEndOf="#+id/mandir"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="#+id/mandir"
app:layout_constraintTop_toTopOf="#+id/mandir"
app:layout_constraintVertical_bias="0.75"
app:srcCompat="#mipmap/not_lit_diya" />
<ImageView
android:id="#+id/lightUp4"
android:layout_width="142dp"
android:layout_height="70dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/unlight"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/matchstick" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="68dp"
android:layout_height="81dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.55"
app:srcCompat="#drawable/krishna_ji" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="78dp"
android:layout_height="84dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.76"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.55"
app:srcCompat="#drawable/mata_rani" />
<ImageView
android:id="#+id/agarbattiBtn"
android:layout_width="80dp"
android:layout_height="105dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.86"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/agarbatti" />
<ImageView
android:id="#+id/agrabatti"
android:layout_width="86dp"
android:layout_height="104dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.56"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.59000003"
app:srcCompat="#drawable/agarbatti" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks in advance...
You can use 3rd party view library like this because custom solutions are too complicated to write from what I've seen.
Just import it into you project
dependencies {
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'
}
And use:
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/src_anim"
android:background="#drawable/bg_anim"
/>
If drawables declared by android:src and/or android:background are GIF
files then they will be automatically recognized as GifDrawables and
animated.
There are methods for GIF control such as:
stop() - stops the animation, can be called from any thread
start() - starts the animation, can be called from any thread
isRunning() - returns whether animation is currently running or
not reset() - rewinds the animation, does not restart stopped one
setSpeed(float factor) - sets new animation speed factor, eg.
passing 2.0f will double the animation speed seekTo(int position)
seeks animation (within current loop) to given position (in milliseconds)
getDuration() - returns duration of one loop of the animation
getCurrentPosition() - returns elapsed time from the beginning of a
current loop of animation
Please refer to the docs for more info :)
Related
I want to set an image to ImageView as soon as I call a new activity. But for some reason, I can't do it, although seems to be alright
Here is an XML snippet
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/magneticImgBox"
android:layout_width="327dp"
android:layout_height="211dp"
android:src="#drawable/magneticimg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/magneticNameBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/magneticImgBox"
android:text="Набор Magnetic"
android:textSize="35sp"/>
<TextView
android:id="#+id/boxDiscrition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/magneticNameBox"
android:text="Бюджетный комплект, который не имеет каких-то дополнительных функций, но до сих пор пользуется спросом и выполняет свои функции без проблем.
\n\nВ комплекте:\n- Антенна\n- Гарнитура с микрофоном\n- Извлекатель-внедритель\n- Магнитные динамики - 2шт.\n- Батарейка Крона"/>
<VideoView
android:id="#+id/magneticVideoPlayerBox"
android:layout_width="wrap_content"
android:layout_height="450dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/boxDiscrition"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is a Java snippet
public void openExamLight(View view) {
MagneticActivity mag = new MagneticActivity();
Intent intent = new Intent(this, MagneticActivity.class);
startActivity(intent);
mag.changePicture();
}
public void changePicture() {
ImageView img = findViewById(R.id.magneticImgBox);
img.setImageResource(R.drawable.magneticimg);
}
Here I showed the method of clicking in which I call a new activity (open Magnetic) and the method in which I try to change the picture (changePicture, yes, I know that it could not be taken out in a separate method, it's just an attempt to fix everything)
I solved the problem, I had to do a type conversion in the changePicture method
public void changePicture() {
ImageView img = (ImageView)findViewById(R.id.magneticImgBox);
img.setImageResource(R.drawable.magneticimg);
}
I'd like to show the progress bar, generate the computation, then after I do that I'd like the progressbar to disappear and I want to set the textview to the result of my generate result. Yet when I do the below, the progress bar never displays, yet the textview updates appropriately. I originally wanted to do this using AsyncTask but since thats been deprecated I wanted to try a modern method but I'm afraid I can't get it to work. Can anyone see what is wrong?
genResult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
// do onPreExecute stuff
pb.setVisibility(View.VISIBLE);
}
});
// do your stuff
boolean res = exp.genResult();
long startTime = System.nanoTime();
while(System.nanoTime()-startTime<50000){
}
result.outcomes.add(res);
runOnUiThread(new Runnable() {
public void run() {
// do onPostExecute stuff
pb.setVisibility(View.INVISIBLE);
lastRes.setText(Boolean.toString(res));
}
});
}
}).start();
}
});
Below is the XML file
<?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">
<Button
android:id="#+id/gen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="generate result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941" />
<TextView
android:id="#+id/title_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
app:layout_constraintBottom_toTopOf="#+id/gen_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />
<Button
android:id="#+id/detail_bin_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Details"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.941"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.005" />
<Button
android:id="#+id/backbutton_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="go back"
app:layout_constraintEnd_toStartOf="#+id/detail_bin_button"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/lastresultbin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:text="lastresult"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/backbutton_bin"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/probabilityViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="Probability"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toEndOf="#+id/lastresultbin"
app:layout_constraintTop_toBottomOf="#+id/detail_bin_button" />
<TextView
android:id="#+id/plaintext_lastres_bin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="Last Result"
app:layout_constraintBottom_toTopOf="#+id/lastresultbin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/backbutton_bin"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/plaintext_prob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="164dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:text="Probability of Success"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/detail_bin_button" />
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:layout_marginTop="148dp"
android:indeterminate="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
The issue is that System.nanoTime() used in the while loop was too quick to complete. When we switch to using System.currentTimeInMillis() we can notice a difference in the delay and thus the spinner shows and hides as expected.
I'm trying to hide hide all contents of my screen for 10 seconds with a white image but it seems that the text appears on top of the white image no matter what. Is there a way to hide the textView (or any other imageViews in the future) using the white image? Thanks!
Here's my main activity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView splash = findViewById(R.id.imageView);
splash.setScaleType(ImageView.ScaleType.FIT_XY);
splash.postDelayed(new Runnable() {
#Override
public void run() {
splash.setVisibility(View.GONE);
}
}, 10000);
}}
Here's my XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.544"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/white" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginTop="268dp"
android:text="TextToHide"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
If You want ImageView to be on TextView You have to first add to XML TextView then ImageView like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.544"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginTop="268dp"
android:text="TextToHide"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/white" />
Now Your TextView will be behind ImageView
I don't know what You want to do but I think it will be just easier to set TextView visibility to GONE or INVISIBLE than to put ImageView on content that You want to hide.
I am going to implement a progress bar with two clip textview on it.
Something like:
I use some trick (using paddingEnd/paddingStart, back/front textview to achieve the clip effect for textview) to implement it:
// part of activity
TextView leftFrontText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.test_page_layout, containerLayout);
View progressBar = view.findViewById(R.id.progress_bar);
leftFrontText = view.findViewById(R.id.left_front_textview);
int progress = 59;
ValueAnimator va = ValueAnimator.ofFloat(0f, progress);
int mDuration = 1000;
va.setDuration(mDuration);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float)animation.getAnimatedValue();
setViewWidth(progressBar, (int)value);
setLeftFrontTextViewPadding((int)value);
}
});
// va.addListener(new Animator.AnimatorListener() {
// #Override
// public void onAnimationStart(Animator animation) {
//
// }
//
// #Override
// public void onAnimationEnd(Animator animation) {
// setLeftFrontTextViewPadding(progress);
// }
//
// #Override
// public void onAnimationCancel(Animator animation) {
//
// }
//
// #Override
// public void onAnimationRepeat(Animator animation) {
//
// }
// });
va.start();
}
private void setViewWidth(View view, int dp) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = ThemeUtil.dpToPx(this, dp);
view.setLayoutParams(layoutParams);
}
private void setLeftFrontTextViewPadding(int progressBarDp) {
int marginStart = ThemeUtil.dpToPx(TestPageActivity.this, 12);
leftFrontText.post(new Runnable() {
#Override
public void run() {
int textViewLength = leftFrontText.getWidth();
int progressbarDistance = ThemeUtil.dpToPx(TestPageActivity.this, progressBarDp);
if (textViewLength >= 0) {
int padding = marginStart + textViewLength - progressbarDistance;
leftFrontText.setPadding(0, 0, padding > 0 ? -padding : 0, 0);
}
}
});
}
And the layout is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="28dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/rounded_corner_bg_for_cashlfow_homepage_category_cell"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
>
<TextView
android:id="#+id/left_bottom_textview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="Entertatinment"
android:textSize="#dimen/main_text_size"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/grey1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="12dp"
/>
<View
android:id="#+id/progress_bar"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="#drawable/rounded_corner_front_progreebar_homepage_category_cell"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="#+id/left_front_textview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical|start"
android:text="Entertatinment"
android:textSize="#dimen/main_text_size"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="12dp"
/>
<TextView
android:id="#+id/right_bottom_amount_textview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical|end"
android:text="$0 / $0"
android:textSize="#dimen/main_text_size"
android:maxLines="1"
android:textColor="#color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="12dp"
/>
<TextView
android:id="#+id/right_front_amount_textview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center_vertical|end"
android:text="$0 / $0"
android:textSize="#dimen/main_text_size"
android:maxLines="1"
android:textColor="#color/black1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="12dp"
android:paddingStart="0dp"
/>
</android.support.constraint.ConstraintLayout>
When I run this code, it almost achieved the effect I want. But it has one issue. Textview keeps flashing while doing animation, and when animation finished, the color of textview sometime turn all white.
I think there may be something wrong in leftFrontText.post() of setLeftFrontTextViewPadding, like how I get width of textview. But I can't be quite sure. Can anyone show me the right way to do this? Or any other better way to implement this kind of progress bar.
Why not try this approach and leverage the power of ConstraintLayout as your progressBar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentview"
android:layout_width="360dp"
android:layout_height="28dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ddddff"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
<TextView
android:id="#+id/left_bottom_textview"
android:layout_width="180dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Entertainment"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#404040"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="#+id/right_front_amount_textview"
android:layout_width="180dp"
android:layout_height="0dp"
android:gravity="center_vertical|end"
android:text="$0 / $0"
android:maxLines="1"
android:textColor="#color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/progress_bar"
android:layout_width="50dp"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
android:background="#004090">
<TextView
android:id="#+id/left_front_textview"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Entertainment"
android:singleLine="true"
android:textColor="#fff"
android:ellipsize="none"
android:layout_paddingStart="12dp"
android:layout_paddingLeft="12dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="#+id/right_bottom_amount_textview"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical|end"
android:text="$0 / $0"
android:layout_alignRight="#id/left_front_textview"
android:textColor="#ffffff"
android:layout_paddingEnd="12dp"
android:layout_paddingRight="12dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/left_front_textview"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
So basically we would have:
The Parent layout (parentview): having a fixed layout_width 360dp and contains the initial Grey TextViews
The Progress layout (progress_bar): also having a fixed size 360dp and contains the front White TextViews
Hence we overlay the Progress layout (progress_bar) ontop of its parent layout and increase or decrease the layout_width to simulate a progress without flickering:
The image below shows the progress layout having a static layout_width of 74dp; Also note that progress layout_width based on the example provided above can range from 1dp to max:360dp or otherwise 1% to 100% of parentview width.
EDIT2:
Using
fragTransaction.add(fragment, tag);
instead of
fragTransaction.add(id, fragment);
OR
Removing all Fragments code in MainActivity makes the app works again, for some odd reason.
Getting the text via .getText().toString() returns null/empty when button is pressed.
Tried to manually .setText("something"), it works when .getText().toString() is called but,
when the text is modified, calling .getText().toString() will still return "something".
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
Fragment loginFrag = new LoginFragment();
fragTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragTransaction.add(R.id.mainFrag, loginFrag);
fragTransaction.commit();
}
LoginFragment.java
public class LoginFragment extends Fragment{
EditText fieldID;
EditText fieldPassword;
Button logButton;
private FirebaseAuth mAuth;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_menu, container, false);
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
mAuth = FirebaseAuth.getInstance();
view = v;
fieldID = v.findViewById(R.id.fieldID);
fieldPassword = v.findViewById(R.id.fieldPassword);
logButton = v.findViewById(R.id.button_login);
logButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signIn(fieldID.getText().toString(), fieldPassword.getText().toString());
}
});
}
private void signIn(String email, String password) {
System.out.println("Email: " + email);
System.out.println("Pass: " + password);
if(!email.isEmpty() && !password.isEmpty()) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("Auth", "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
view.findViewById(R.id.logoMain).setVisibility(View.GONE);
} else {
// If sign in fails, display a message to the user.
Log.w("Auth", "signInWithEmail:failure", task.getException());
//updateUI(null);
}
}
});
}
}
}
Main Activity
<?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=".presenter.Activity.MainActivity">
<fragment
android:id="#+id/mainFrag"
android:name="com.raze.mfa.presenter.Fragment.LoginFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment 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"
android:background="#FFFFFF"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="110dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logoMain"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/bak"
tools:srcCompat="#drawable/bak" />
<ImageView
android:id="#+id/iconlock"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldPassword"
app:layout_constraintHorizontal_bias="0.523"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iconuser"
app:srcCompat="#mipmap/lockicon" />
<ImageView
android:id="#+id/iconuser"
android:layout_width="43dp"
android:layout_height="43dp"
android:layout_marginStart="8dp"
android:layout_marginTop="320dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="#+id/fieldID"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/usericon" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/loginText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldPassword"
app:layout_constraintVertical_bias="0.188" />
<EditText
android:id="#+id/fieldPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fieldID" />
<EditText
android:id="#+id/fieldID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:layout_marginEnd="72dp"
android:autofillHints=""
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/logoMain"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="108dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/fieldID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="#drawable/logoimg" />
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="#string/register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_login"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Unrelated:
I've Googled mostly everything but it seems that I haven't understand the basics of using Fragments, Activites, context, Views, and etc.
Ok I see, you are using a <fragment/> tag already in your MainActivity layout, and then you add with the FragmentTransaction a fragment to it.
The transaction is for FrameLayout containers that are empty and take a fragment. When you add the fragment statically through the <fragment/> tag the fragment is already loaded into the screen.
Something weird was happening when you added a new fragment to a fragment tag, maybe it duplicated the fragment and it was reading the one that is behind the actual fragment and it wasn't getting the input from the user.
Read here for the proper use of FragmentTransaction: https://developer.android.com/training/basics/fragments/fragment-ui
Taken from the docs above, see how they use a FrameLayout as the container.
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Java:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
}