I have created a custom dialog layout and I am trying to display the dialog as a Activity instead of inflating the view inside the alert dialog. But the dialog is not being displayed as desired. Please have a look.
Custom Dialog Layout
The dialog activity is being shown like below
You can take a look at the code below
MainActivity
public class MainActivity extends AppCompatActivity {
private static final int PROFILE_PHOTO = 1;
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showImageOptionsDialog();
}
});
}
private void showImageOptionsDialog() {
Intent imageDialogIntent = new Intent(this, ImageDialogActivity.class);
startActivityForResult(imageDialogIntent, PROFILE_PHOTO);
}
}
ImageDialogActivity
public class ImageDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_dialog);
}
}
activity_image_dialog.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:padding="#dimen/sixteen"
android:clipToPadding="false"
tools:context=".ImageDialogActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="#dimen/eight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label_profile_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/label_profile_photo"
android:textColor="#color/primary_text"
android:textSize="#dimen/dialog_label"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:layout_marginTop="40dp"
android:background="#color/secondary_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/option_gallery" />
<Button
android:id="#+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/cancel_button_bg"
android:text="#string/action_cancel"
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/divider" />
<TextView
android:id="#+id/option_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:drawableTop="#mipmap/ic_pick_img"
android:drawablePadding="#dimen/eight"
android:gravity="center"
android:text="#string/option_gallery"
android:textColor="#color/primary_text"
app:layout_constraintEnd_toStartOf="#+id/option_remove_pic"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/label_profile_pic" />
<TextView
android:id="#+id/option_remove_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:drawableTop="#mipmap/ic_remove_pic"
android:drawablePadding="#dimen/eight"
android:gravity="center"
android:text="#string/option_remove_photo"
android:textColor="#color/primary_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/option_gallery"
app:layout_constraintTop_toTopOf="#+id/option_gallery"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
Manifest
<activity
android:name=".ImageDialogActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog" />
I'm not able to figure out the issue. Please help. Regards!
It is worked for me
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
dialog.getWindow().setLayout((6 * width) / 7, LinearLayout.LayoutParams.WRAP_CONTENT);
Related
First of all, I wanna say sorry about my English.
I am trying to build my first android application using android studio. I created the main page containing two taps one for login activity and the other one for the About section.
what I did exactly was create the Login activity for the fragments to hold the tabs and the basic design and one layout only without the java file for the about_tab_fragment section and finally the login_tab activity.
the problem is that I can not get the input data from the view and no action happened when I click on the login button.(and there is no error in the Run).
and this is my code:
Login activity (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=".Login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/login_header"
app:layout_constraintHeight_percent=".27"
android:scaleType="centerCrop"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".78"
app:layout_constraintVertical_bias="1"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintHeight_percent=".7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tab_layout"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/google_img"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/facebook_img"
app:layout_constraintHorizontal_bias="1"
android:layout_marginRight="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/fab_google"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_insta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/insta_img"
app:layout_constraintHorizontal_bias="0"
android:layout_marginLeft="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fab_google"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Login activity (Java)
public class Login extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
FloatingActionButton facebook,google,instagram;
float v = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
facebook = findViewById(R.id.fab_facebook);
google = findViewById(R.id.fab_google);
instagram = findViewById(R.id.fab_insta);
tabLayout.addTab(tabLayout.newTab().setText("Login"));
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final LoginAdapterClass adapter = new LoginAdapterClass(getSupportFragmentManager(),this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//For animations
facebook.setTranslationY(300);
google.setTranslationY(300);
instagram.setTranslationY(300);
tabLayout.setTranslationY(300);
facebook.setAlpha(v);
google.setAlpha(v);
instagram.setAlpha(v);
tabLayout.setAlpha(v);
facebook.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(400).start();
google.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(600).start();
instagram.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(800).start();
tabLayout.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(100).start();
}
}
about_tab_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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Login_tab (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=".LoginTab">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="24dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_email_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_lock_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<Button
android:id="#+id/loginBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="16sp"
android:textStyle="bold"
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.758"
app:layout_constraintWidth_percent="0.8" />
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
My problem occurs here:
I can not take the text from the inputs email and password plus that I can not add event to the loginBtn. I tried to put Toast in the On Click Listener but nothing happened.
Login_tab (Java)
public class LoginTab extends AppCompatActivity {
EditText emailLogin,passwordLogin;
Button loginBtn;
TextView forgetPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_tab);
emailLogin = findViewById(R.id.email);
passwordLogin = findViewById(R.id.password);
loginBtn = findViewById(R.id.loginBtn);
forgetPassword = findViewById(R.id.forgetPassword);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this,"This is the login button",Toast.LENGTH_LONG).show();
if(emailLogin.getText().toString().isEmpty()){
emailLogin.setError("Email is required");
return;
}
if(passwordLogin.getText().toString().isEmpty()){
passwordLogin.setError("Password is required");
return;
}
}
});
}
}
Can any one help me to fix the problem.
I did a lot of searches but I could not find any answer.
I found the problem.
the problem because I have to put the action in the class loginTab that extends Fragment not AppCompatActivity.
public class LoginTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_login_tab,container,false);
Button b = root.findViewById(R.id.loginBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(root.getContext(), "This is the Login button....",Toast.LENGTH_LONG).show();
}
});
return root;
}
}
I have a TextView in which i am setting text dynamically(text length is not fixed ) and end of the text there is a icon,So My question is I want to show tooltip like tooltip arrow should be at top of info icon , as shown in attached image
You can follow this way
XML File
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/ivDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_done_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tvShipInBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Ship in Box($10 Extra per box)"
android:textColor="#E83535"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/ivDone" />
<ImageView
android:id="#+id/ivInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="#drawable/ic_info_black_24dp"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/tvShipInBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clToolTip"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:background="#drawable/ic_tooltip_bg"
app:layout_constraintBottom_toTopOf="#id/clMain"
app:layout_constraintHeight_percent="0.075"
app:layout_constraintStart_toStartOf="#id/clMain"
app:layout_constraintWidth_percent="0.65">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginHorizontal="8dp"
android:alpha="0.87"
android:text="A box increases an items volumetric weight and costs more."
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File
public class MainActivity extends AppCompatActivity {
ImageView ivInfo;
ConstraintLayout clToolTip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivInfo = findViewById(R.id.ivInfo);
clToolTip = findViewById(R.id.clToolTip);
ivInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ivInfo.setVisibility(View.INVISIBLE);
ivInfo.postDelayed(new Runnable() {
public void run() {
clToolTip.setVisibility(View.VISIBLE);
}
}, 7000);
}
});
}
}
I am trying to load a custom view of my dialog in my activity. I want to remove the white space to the right of the dialog.
My activity
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
showClippingCoupon();
}
}
My dialog code
void showClippingCoupon()
{
LayoutInflater layoutInflater = getLayoutInflater();
View toastView = layoutInflater.inflate(R.layout.clipping_coupon_toast, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(toastView);
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
TextView couponCodeText = (TextView) toastView.findViewById(R.id.coupon_code_text);
couponCodeText.setText("Y34UIDEFDK");
ImageView closeButtonImageView = (ImageView) toastView.findViewById(R.id.coupon_close);
if (closeButtonImageView != null)
{
closeButtonImageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
}
dialog.show();
}
My activity layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/layout">
</FrameLayout>
I am trying to load a custom dialog view in my activity. This is how it is supposed to look like.
But this is what I get
This is what it looks in a preview.
Here is the layout of the dialog.
<?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:background="#color/coupon_text_color_v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/coupon_clip_success_icon"
android:src="#drawable/ic_check_circle_green"
app:layout_constraintStart_toStartOf="parent"
android:contentDescription="#android:string/ok"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="16dp"
android:layout_toStartOf="#+id/coupon_body"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:id="#+id/coupon_body"
android:focusable="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/coupon_clip_success_icon"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:id="#+id/coupon_copied_text"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="48dp"
android:text="#string/coupon_code_copied"
android:textAppearance="#style/Small"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/coupon_code_text"
android:layout_below="#+id/coupon_copied_text"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:text="Test"
android:textAppearance="#style/Small.Bold"
/>
</RelativeLayout>
<ImageView
android:id="#+id/coupon_close"
android:src="#drawable/ic_clear"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintStart_toEndOf="#+id/coupon_body"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_gravity="top|right"
android:contentDescription="#string/close" />
</androidx.constraintlayout.widget.ConstraintLayout>
What should I be doing to remove the white space at the dialog's right?
ConstraintLayout :- width : match_parent and replace your Imageview
<ImageView
android:id="#+id/coupon_close"
android:src="#drawable/ic_clear"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="8dp"
android:contentDescription="#string/close" />
I have two activities. The second activity has a textview that shows a string(about 600 words) from xml resources. When I want to start the second activity with a simple intent it shows the black screen for more than 30 seconds. Is this because of a long string? How i can solve this?
Intent intent = new Intent(Home.this, Text.class);
startActivity(intent);
Second avtivity:
public class Text extends AppCompatActivity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
txt = findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(this.getAssets(), "B Roya.ttf");
txt.setTypeface(font);
}
}
and it's a layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/colorPrimaryDark"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".Text">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_weight="20"
android:background="#drawable/janin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:layout_weight="50"
android:paddingBottom="8dp">
<com.uncopt.android.widget.text.justify.JustifiedTextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/music"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:lineSpacingExtra="5dp"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:textColor="#android:color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
</ScrollView>
</LinearLayout>
Seems it was because of using below library and i replaced it with TextView then it solved:
com.uncopt.android.widget.text.justify.JustifiedTextView
I want to continuously show different text on text view which animates from left to right, waits on the center of the screen for 3 sec and animates outside of the screen and replaced by a new text which animates from left to right
I have included a textview inside a viewflipper.
<android.support.constraint.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">
<TextView
android:id="#+id/tv__inc_pre_sing__screen_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:gravity="center"
android:text="PRACTICE"
android:textAllCaps="true"
android:textColor="#color/colorAccent"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/rl__inc_pre_sing__tm_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#efff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv__inc_pre_sing__screen_title">
<ImageView
android:id="#+id/tv__inc_pre_sing__quotation_mark"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginTop="12dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintTop_toTopOf="parent" />
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:animateLayoutChanges="true"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/tv__inc_pre_sing__quotation_mark"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hiiiiii" />
</ViewFlipper>
<ImageView
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/view_flipper" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="16dp"
android:background="#drawable/ic_launcher_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/rl__inc_pre_sing__tm_container" />
</android.support.constraint.ConstraintLayout>
MainActivity Code
public class Main6Activity extends AppCompatActivity {
private ViewFlipper mViewFlipper;
private int count = 0;
private TextView textView;
private ConstraintLayout rootContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
mViewFlipper = findViewById(R.id.view_flipper);
textView = findViewById(R.id.tv);
rootContainer = findViewById(R.id.rl__inc_pre_sing__tm_container);
mViewFlipper.setAutoStart(true);
mViewFlipper.startFlipping();
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(rootContainer.getContext(), android.R.anim.slide_in_left));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(rootContainer.getContext(), android.R.anim.slide_out_right));
Resources resources = getApplicationContext().getResources();
final String[] textString = resources.getStringArray(R.array.teacher_messages);
new Thread() {
#Override
public void run() {
super.run();
try {
while (!isInterrupted()) {
Thread.sleep(3000);
runOnUiThread(() -> updateText(textString));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
public void updateText(String[] strings) {
if (count >= strings.length) {
count = 0;
}
textView.setText(strings[count]);
count++;
}
}
I guess the outanimation does not work as the textview length changes and I want to animate the constraint layout according to the textview height.
Not sure if this is the right reason.
The outAnimation of the view flipper does not work.
That's not how ViewFlipper works . the View flipper will only animate the view added to it .
Here's an example :
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/loading"
android:autoStart="true"
android:flipInterval="5000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right">
<TextView
fontPath="fonts/benton_light.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text1"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
fontPath="fonts/benton_light.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text3"
android:textColor="#color/white"
android:textSize="18sp" />
<TextView
fontPath="fonts/benton_light.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/tex2"
android:textColor="#color/white"
android:textSize="18sp" />
</ViewFlipper>
Source
If that does not suit your needs . you can take a look at this solution. (Animating text changes in a TextView)