How to start shared element transition between 2 activity? - android

I just learn about Shared Element in Android and I have one image which one of them in ActivitySplashScreen and one other is into Toolbar and I want to use shared element to move this image from ActivitySplashScreen to ActivityMain
after some search and try to implement that simple way to make this feature don't work on my code, for example:
style.xml:
<style name="AppTheme" parent="BaseTheme">
<item name="android:windowContentTransitions">true</item>
</style>
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
...
</style>
ActivitySplashScreen.java:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
ButterKnife.bind(this);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(ActivitySplashScreen.this, MainActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(ActivitySplashScreen.this,
app_logo,
ViewCompat.getTransitionName(app_logo));
startActivity(intent, options.toBundle());
finish();
}
}, 3000);
}
ActivityMain.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
supportPostponeEnterTransition();
}
and ImageView widget on my ActivitySplashScreen and ActivityMain xml layout:
<ImageView
android:id="#+id/instagram_add_story"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:src="#drawable/img_wizard_1"
android:transitionName="app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="VectorDrawableCompat" />
this code is not working as well and I'm not sure what exactly problem on that
UPDATED
ActivitySplashScreen 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">
<LinearLayout
android:id="#+id/alachiq_header_animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/instagram_animation_gradient_list"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="40dp">
<ImageView
android:id="#+id/app_logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:transitionName="app_logo"
android:tint="#android:color/white"
app:srcCompat="#drawable/img_wizard_1" />
<TextView
android:id="#+id/title"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="#font/iran_sans_bold"
android:gravity="center"
android:text="#string/app_name"
android:textColor="#color/mdtp_white" />
<TextView
style="#style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/iran_sans_light"
android:gravity="center"
android:textColor="#color/mdtp_white" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:slidingLayer="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/grey_5"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/Toolbar.Light">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/activityTitle"
style="#style/Base.TextAppearance.AppCompat.Caption"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="#font/iran_sans_bold"
android:gravity="center|right"
android:text="#string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/application_logo"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/application_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:scaleType="centerCrop"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_app_logo"
android:transitionName="app_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/drawerMenu"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:ignore="VectorDrawableCompat" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

I can use in this way here is my solution according to your scenario.
Style:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
Splash.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">
<LinearLayout
android:id="#+id/alachiq_header_animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/ic_launcher_background"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="40dp">
<ImageView
android:id="#+id/app_logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:tint="#android:color/black"
app:srcCompat="#android:drawable/ic_dialog_email" />
<TextView
android:id="#+id/title"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="#string/app_name"
android:textColor="#android:color/background_dark" />
<TextView
style="#style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:fontFamily="sans-serif"
android:textColor="#android:color/background_dark" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Splash.Java
public class SplashActivity extends AppCompatActivity {
#BindView(R.id.app_logo)
ImageView app_logo;
Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ButterKnife.bind(this);
mActivity = this;
ViewCompat.setTransitionName(app_logo, "app_logo");
new Handler().postDelayed(new Runnable() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("transition_name", ViewCompat.getTransitionName(app_logo));
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity,app_logo,ViewCompat.getTransitionName(app_logo));
startActivity(intent, options.toBundle());
finish();
}
}, 3000);
}
}
MainActivity.XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:slidingLayer="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="#android:color/darker_gray"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
app:contentInsetStartWithNavigation="0dp"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/activityTitle"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center|right"
android:text="#string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/application_logo"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp" />
<ImageView
android:id="#+id/application_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:scaleType="centerCrop"
android:layout_marginBottom="8dp"
android:tint="#android:color/black"
app:srcCompat="#android:drawable/ic_dialog_email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginRight="8dp" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
#BindView(R.id.toolbar)
Toolbar mToolbar;
Activity mActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mActivity = this;
setSupportActionBar(mToolbar);
ActivityCompat.postponeEnterTransition(this);
ImageView application_logo = (ImageView) mToolbar.findViewById(R.id.application_logo);
if (getIntent() != null) {
Bundle extras = getIntent().getExtras();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String imageTransitionName = extras.getString("transition_name");
application_logo.setTransitionName(imageTransitionName);
ActivityCompat.startPostponedEnterTransition(mActivity);
}
}
}
}
Here is the Sample code for SharedElementTransition according to your scenario. Whenever you use some kind of style always write in style file then use it.
Note: import android.support.v7.widget.Toolbar; for ToolBar in Java file. This is working fine.

Related

BottomNavigationBar goes down partially when I change fragments

I use BottomNavigationBar(bnb) with Navigation(android jetpack). It's a single-activity app. There are 3 fragments. One of them is starting. When I perform the navigation from starting fragment to any outher, bnb goes down for half of it hight. When I go back or perform navigation between not starting fragments everything is ok.
Activity
<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="#color/colorPrimary"
tools:context=".ui.activities.MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconSize="#dimen/bnb_icon_size"
android:background="?attr/colorPrimary"
app:itemIconTint="#drawable/selector"
app:elevation="#dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_navigation_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation_view"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
one of the fragments
<layout 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">
<data>
<variable
name="callback"
type="(...)ui.info.InfoFragmentCallback" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/set_coord_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:fitsSystemWindows="true">
<TextView
android:id="#+id/info_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_email"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/info_contact_us_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:gravity="center"
android:text="#string/title_contact_us"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/info_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_large"
android:gravity="center"
android:text="#string/app_version"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constraintBottom_toTopOf="#+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/info_app_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_medium"
android:gravity="center"
android:text="#string/title_rate_app"
android:textColor="#color/colorWhiteOverlayTransparency45"
android:textSize="#dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="#+id/info_google_badge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/info_google_badge"
android:layout_width="wrap_content"
android:layout_height="#dimen/icon_size_large"
android:elevation="#dimen/margin_medium"
android:onClick="#{(view)->callback.onAppRateClicked()}"
android:src="#drawable/google_play_badge"
app:layout_constraintBottom_toTopOf="#+id/info_privacy_policy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/info_email"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/info_privacy_policy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_huge"
android:onClick="#{(view)->callback.onPrivacyClicked()}"
android:text="#string/privacy_policy_title"
android:textColor="#color/colorSecondary"
android:textSize="#dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController mNavController;
private final BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = item -> {
/*multiple touches may must nothing*/
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, mNavController);
initBottomNavigationMenu();
}
private void initBottomNavigationMenu() {
bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);
}
public void performNavigation(int navId) {
mNavController.navigate(navId);
}
#Override
protected void onDestroy() {
super.onDestroy();
bottomNavigationView.setOnNavigationItemReselectedListener(null);
}
}
bnb menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/starting_fragment"
android:icon="#drawable/ic_image"
android:title=""
app:showAsAction="ifRoom" />
<item
android:id="#+id/favorites_fragment"
android:icon="#drawable/ic_star"
android:title=""
app:showAsAction="ifRoom"/>
<item
android:id="#+id/info_fragment"
android:icon="#drawable/ic_info"
android:title=""
app:showAsAction="ifRoom"/>
</menu>
Where can be a problem?

My application does not detect white color

I have a problem,but no one could help me.the problem is:
I have detail activity for show details of my product.
this activity has a toolbar(relative layout) with white background color.
everything is good and no problem but when I come from a fragment to this detail activity my toolbar color turn to blue!
I try to check what is the reason, and I finally detect my application does not know the white color!! if I use the #ffffff (hardcode or get from resource) my toolbar turn to blue but if I use any other color, it's okay even #fffffe
it is my style:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:fontFamily">#font/font</item>
<item name="fontFamily">#font/font</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
detailactivity code:
public class DetailActivity extends AppCompatActivity {
DetailViewModel detailViewModel = new DetailViewModel();
CompositeDisposable compositeDisposable = new CompositeDisposable();
String id,title;
TextView txtTitle, txtName, txtColor, txtguarantee, txtPrice, txtPoints, txtMore, txtDescription;
ImageView imgImage, imgShare, imgFav, imgCart, imgMore, imgBack;
RecyclerView recyclerView;
CardView properties;
CardView comments;
RatingBar ratingBar;
Button btnAddToBasket;
List<RatingModel> ratingModels;
NestedScrollView nestedScrollView;
Thread thread;
RelativeLayout toolbar;
int toolbarMergeColor, drawableMergeColor;
TextView txtToolbarTitle;
float ratio;
int startHeight;
int toolbarTitleYPosition = -1;
String imageUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
setupViews();
getIntentId();
txtToolbarTitle.setTranslationY(150);
// observeForDetail();
}
private void getIntentId() {
id = getIntent().getExtras().getString("id");
}
private void setupViews() {
txtToolbarTitle = findViewById(R.id.txt_detail_toolbarTitle);
imgImage = findViewById(R.id.img_detail_image);
toolbar = findViewById(R.id.rel_detail_toolbar);
ratingModels = new ArrayList<>();
txtDescription = findViewById(R.id.txt_detail_description);
imgShare = findViewById(R.id.img_detail_share);
imgFav = findViewById(R.id.img_detail_favorite);
imgBack = findViewById(R.id.img_detail_back);
imgMore = findViewById(R.id.img_detail_more);
imgMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu=new PopupMenu(DetailActivity.this,imgMore);
popupMenu.getMenuInflater().inflate(R.menu.detail_more_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
if(menuItem.getItemId()==R.id.action_more_chart){
Intent intent=new Intent(getApplicationContext(), ChartActivity.class);
intent.putExtra("id",id);
intent.putExtra("title",title);
startActivity(intent);
}else{
Intent intent=new Intent(getApplicationContext(), CompareActivity.class);
intent.putExtra("image_url",imageUrl);
startActivity(intent);
}
return true;
}
});
popupMenu.show();
}
});
imgCart = findViewById(R.id.img_detail_cart);
txtTitle = findViewById(R.id.txt_detail_title);
txtName = findViewById(R.id.txt_detail_name);
txtColor = findViewById(R.id.txt_detail_color);
txtguarantee = findViewById(R.id.txt_detail_guarantee);
txtPrice = findViewById(R.id.txt_detail_price);
txtPoints = findViewById(R.id.txt_detail_points);
txtMore = findViewById(R.id.txt_detail_more);
txtMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (txtMore.getText().toString().equals("ادامه مطلب")) {
startHeight = txtDescription.getHeight();
txtDescription.setMaxLines(Integer.MAX_VALUE);
int widthSpec = View.MeasureSpec.makeMeasureSpec(txtDescription.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
txtDescription.measure(widthSpec, heightSpec);
int targetHeight = txtDescription.getMeasuredHeight();
final int heightSpan = targetHeight - startHeight;
txtDescription.getLayoutParams().height = startHeight;
txtDescription.setLayoutParams(txtDescription.getLayoutParams());
Animation animation = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
txtDescription.getLayoutParams().height = (int) (startHeight + heightSpan * interpolatedTime);
txtDescription.setLayoutParams(txtDescription.getLayoutParams());
}
};
animation.setDuration(300);
txtDescription.startAnimation(animation);
txtMore.setText("بستن");
} else {
startHeight = txtDescription.getHeight();
txtDescription.setMaxLines(8);
int widthSpec = View.MeasureSpec.makeMeasureSpec(txtDescription.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
txtDescription.measure(widthSpec, heightSpec);
int targetHeight = txtDescription.getMeasuredHeight();
final int heightSpan = targetHeight - startHeight;
txtDescription.getLayoutParams().height = startHeight;
txtDescription.setLayoutParams(txtDescription.getLayoutParams());
Animation animation = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
txtDescription.getLayoutParams().height = (int) (startHeight + heightSpan * interpolatedTime);
txtDescription.setLayoutParams(txtDescription.getLayoutParams());
}
};
animation.setDuration(300);
txtDescription.startAnimation(animation);
txtMore.setText("ادامه مطلب");
}
}
});
properties = findViewById(R.id.card_detail_properties);
properties.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DetailActivity.this, PropertiesActivity.class);
intent.putExtra("title", title);
startActivity(intent);
}
});
comments = findViewById(R.id.card_detail_comments);
comments.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CommentActivity.class);
intent.putExtra("id",id);
intent.putExtra("name",txtName.getText().toString());
startActivity(intent);
}
});
btnAddToBasket = findViewById(R.id.btn_detail_addToBasket);
recyclerView = findViewById(R.id.rv_detail_points);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ratingBar = findViewById(R.id.rating_detail);
}
#Override
protected void onDestroy() {
compositeDisposable.dispose();
super.onDestroy();
}
}
xml codes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/gray200"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Detail.DetailActivity">
<RelativeLayout
android:id="#+id/rel_detail_toolbar"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:id="#+id/img_detail_back"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/ic_arrow_back_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_centerVertical="true"
android:layout_margin="8dp"
app:srcCompat="#drawable/ic_more_vert_black_24dp"
android:id="#+id/img_detail_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_centerVertical="true"
app:srcCompat="#drawable/ic_shopping_cart_black_24dp"
android:layout_toRightOf="#id/img_detail_more"
android:id="#+id/img_detail_cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_alignParentBottom="true"
android:textColor="#color/colorWhite"
android:text="test test"
android:layout_marginRight="8dp"
android:layout_toLeftOf="#id/img_detail_back"
android:id="#+id/txt_detail_toolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll_detail"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#ffffff"
android:id="#+id/img_detail_image"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v7.widget.CardView
app:cardBackgroundColor="#color/gray100"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:paddingBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img_detail_share"
android:layout_margin="8dp"
app:srcCompat="#drawable/ic_share_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_margin="8dp"
app:srcCompat="#drawable/ic_favorite_black_24dp"
android:layout_toRightOf="#+id/img_detail_share"
android:id="#+id/img_detail_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:ellipsize="end"
android:layout_below="#id/img_detail_share"
android:maxLines="1"
android:minLines="1"
android:textColor="#color/colorBlack"
android:layout_marginRight="8dp"
android:layout_alignParentRight="true"
tools:text="هارد اکسترنال سیلیکن پاور 1 ترا بایت"
android:id="#+id/txt_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginRight="8dp"
android:layout_alignParentRight="true"
tools:text="هارد اکسترنال سیلیکن پاور 1 ترا بایت"
android:textSize="12sp"
android:id="#+id/txt_detail_name"
android:layout_width="wrap_content"
android:layout_below="#id/txt_detail_title"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:background="#color/gray200"
android:gravity="center"
android:padding="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_marginRight="4dp"
android:foreground="?android:selectableItemBackground"
android:layout_width="0dp"
android:id="#+id/card_detail_comments"
android:layout_weight="0.5"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="center"
android:padding="8dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/gray700"
android:layout_marginRight="8dp"
android:text="نظرات کاربران"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
app:srcCompat="#drawable/ic_comment_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_marginLeft="4dp"
android:foreground="?android:selectableItemBackground"
android:layout_weight="0.5"
android:id="#+id/card_detail_properties"
android:layout_width="0dp"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="center"
android:padding="8dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/gray700"
android:layout_marginRight="8dp"
android:text="مشخصات"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
app:srcCompat="#drawable/ic_comment_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<android.support.v7.widget.CardView
app:cardElevation="4dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginRight="8dp"
android:textSize="16sp"
android:layout_gravity="right"
android:textColor="#color/colorBlack"
android:text="رنگ"
android:id="#+id/txt_detail_colorTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginRight="8dp"
android:layout_gravity="right"
tools:text="مشکی"
android:id="#+id/txt_detail_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="8dp"
android:layout_gravity="right"
tools:text="گارانتی سه ساله ۀوا"
android:id="#+id/txt_detail_guarantee"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:background="#color/gray300"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:textColor="#color/red"
android:layout_margin="8dp"
android:layout_gravity="right"
android:text="فروش توسط دیجی کالا"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="8dp"
android:layout_gravity="right"
android:text="آماده ارسال از انبار دیجی کالا"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:background="#color/gray300"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:id="#+id/txt_detail_price"
android:layout_margin="8dp"
android:textColor="#color/green"
tools:text="1230000 تومان"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_detail_addToBasket"
android:layout_margin="8dp"
android:textColor="#color/colorWhite"
android:text="افزودن به سبد خرید"
android:background="#drawable/shape_btn_add_to_basket"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:drawableLeft="#drawable/ic_keyboard_arrow_left_black_24dp"
android:drawablePadding="8dp"
android:drawableRight="#drawable/ic_account_balance_black_24dp"
android:layout_gravity="center"
android:text="8 فروشنده و گارانتی برای این کالا وجود دارد"
android:textColor="#color/colorAccent"
android:layout_width="wrap_content"
android:layout_marginBottom="8dp"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="4dp"
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:maxLines="6"
android:ellipsize="end"
android:padding="16dp"
android:textColor="#color/colorBlack"
tools:text="محصولی با دوام و با کیفیت عالی همراه با ضمانت نامه"
android:id="#+id/txt_detail_description"
android:layout_width="match_parent"
android:layout_height="170dp" />
<View
android:background="#color/gray200"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:id="#+id/txt_detail_more"
android:padding="8dp"
android:text="ادامه مطلب"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="4dp"
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RatingBar
style="?android:attr/ratingBarStyleSmall"
android:layout_centerHorizontal="true"
android:progressTint="#color/red"
android:rating="3.5"
android:id="#+id/rating_detail"
android:numStars="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt_detail_points"
android:layout_marginLeft="8dp"
android:layout_toRightOf="#id/rating_detail"
tools:text="3.5 از 5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:layout_margin="8dp"
android:layout_below="#id/rating_detail"
android:id="#+id/rv_detail_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
What is activity theme in style , if you do not set noActionBar theme android put default toolbar on your relative layout and you cant see it
So define your styles like this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/activityBackground</item>
<item name="colorPrimaryDark">#color/greyTextColor</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and set AppTheme.NoActionBar bar theme to your activity in AndroidManifest file
Or Use
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
Try using -
android:background="#android:color/white"
You may wanna try that; define a new color to the "colors.xml" and use it as background.
Update
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
You should change your #color/colorPrimary and #color/colorPrimaryDark with the colors you want. Define them in colors and assign them instead of those two above. It must be fixed. Good Luck!
You have two choice
1) you can set your toolbar color in your detail activity onCreate
toolbar.setBackgroundColor(Color.WHITE);
check this may this works or
2) in color.xml set white color and get the reference from there
"< color name="colorTransWhite">#ffffffff< /color>" (remove extra spaces)
check any of one and let me know if not work.

Button does not get clicked on the first attempt

There are a few buttons in my activity which do not get clicked on the first attempt but they get clicked from the second time onwards. It seems that the button gets focus on the first click and gets actually pressed from the second time onwards. What can be the reason that these buttons do not get clicked the first time?
I have noticed that this behaviour is only shown by buttons that are at the very bottom of my activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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.me.proj.view.activities.MallActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
app:title=" "
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_mall_showcase_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/info_card" />
<include layout="#layout/photo_card" />
<include layout="#layout/address_card" />
<include layout="#layout/opening_hours_card" />
<include layout="#layout/review_card" /> <!-- Button in this file has the problem-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
review_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0.5dp"
card_view:cardMaxElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_marginTop="7dp"
android:background="#color/colorWhite"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<TextView
android:id="#+id/txt_rating"
android:background="#drawable/blue_button_background"
android:text="3.8"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:textSize="24sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/txt_reviews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:layout_toEndOf="#+id/txt_rating"
android:layout_marginStart="20dp"
android:textStyle="bold"
android:paddingTop="1dp"
android:layout_alignTop="#+id/txt_rating"
android:text="Based on 98 reviews"/>
<TextView
android:id="#+id/txt_read_all_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read All Reviews"
android:layout_alignStart="#id/txt_reviews_title"
android:layout_toEndOf="#id/txt_rating"
android:layout_alignBottom="#id/txt_rating"
android:gravity="bottom"
android:paddingBottom="1dp"
android:textColor="#color/red"
android:layout_below="#id/txt_reviews_title"/>
<View
android:id="#+id/view_dummy1"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="#id/txt_read_all_reviews"
android:layout_marginTop="10dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp"
android:layout_below="#+id/view_dummy1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_how_did_your_visit_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How did your visit go?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#color/primary_text" />
<TextView
android:id="#+id/txt_tell_everyone_about_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tell everyone about it!"
android:layout_marginTop="5dp"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<!--This got some problem-->
<Button
android:id="#+id/btn_add_review"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="Add Your Review"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:background="#drawable/blue_button_background"
android:layout_weight="0.5"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}
Code in my MainActivity.class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.sub_layout);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
}
Code in my 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"
tools:context="eflair.myapplication.MainActivity">
<include
android:id="#+id/sub_layout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Code in my sub_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
This issue is actually a bug in the NestedScrollView class for a while now.
I solved this issue my making some tweaks to the source code of the NestedScrollView class, as follows :-
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView {
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (actionMasked) {
case MotionEvent.ACTION_DOWN: {
if (getChildCount() == 0) {
return false;
}
//add this condition
if (!inChild((int) ev.getX(), (int) ev.getY())) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
...
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
...
switch (action & MotionEventCompat.ACTION_MASK) {
...
case MotionEvent.ACTION_DOWN: {
...
// replace this line:
// mIsBeingDragged = !mScroller.isFinished();
mIsBeingDragged = false;
...
}
}
}
}
After doing this it should work fine.

Collapsing Toolbar Layout expand on click event

I have a simple toolbar with a title and a icon. What I want is that when the toolbar is clicked it expands and shows more fields to do a search with some filters. What I really would like is to have the same effect that the airbnb app has in the explore bar.
Can anyone show me how to handle this or give some tips?
What I currently have is this layout. But the collapsing toolbar overrides the toolbar int the top.
<?xml version="1.0" encoding="utf-8"?>
<com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout
android:id="#+id/coordinator"
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"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar android:id="#+id/toolbarUserProfilePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/first_grey">
<TextView android:id="#+id/textViewUserProfilePageToolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:maxLines="1"
android:text="#string/userProfile.toolbar"
android:textColor="#color/third_grey"
android:textSize="20sp" />
<ImageView android:id="#+id/imageViewUserProfilePageToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="end"
android:tint="#color/third_grey" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.AppBarLayout android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textViewEditProfilePageLastName"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
<TextView android:id="#+id/textViewEditProfilePageLastName2"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
<TextView android:id="#+id/textViewEditProfilePageLastName3"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
</com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout>
And in the class I use the setExpanded(true) and setExpanded(false) when I want to either expand or collapse the toolbar.
public class HomeHostActivity extends AppCompatActivity {
#BindView(R.id.coordinator) DisableableCoordinatorLayout collapsingToolbarLayout;
#BindView(R.id.toolbarUserProfilePage) Toolbar toolbar;
#BindView(R.id.appbar) AppBarLayout searchBarLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teste);
ButterKnife.bind(this);
collapsingToolbarLayout.setPassScrolling(false);
}
#OnClick(R.id.toolbarUserProfilePage)
public void expandView(View view) {
searchBarLayout.setExpanded(true, true);
}
#OnClick(R.id.textViewEditProfilePageLastName)
public void collapseView(View view) {
searchBarLayout.setExpanded(false, true);
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

RelativeLayout in DialogFragment - MarginBottom is ignored

So basically, I have this DialogFragment that is fullscreen and has a button over a progressbar (if you click the button, it disappears and the progressbar is visible). I initially had the xml in an activity and everything worked as expected there, but now I want to refactor it to a fullscreen dialogfragment.
Problem is, the button that was ontop of the progressbar is now aligned to the bottom of the parentLayout
<?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/activity_gps_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_setsensor"
tools:ignore="MissingPrefix"
>
<ImageView
android:layout_width="90dp"
android:layout_height="38dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:orientation="vertical"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_location"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="17dp"
android:gravity="center_horizontal"
android:text="#string/promptGpsPermissionBody"
style="#style/fullscreenText"
/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnActivateGps"
android:layout_alignTop="#+id/btnActivateGps"
style="?android:attr/progressBarStyle"
/>
<Button
android:id="#+id/btnActivateGps"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="71dp"
android:layout_marginLeft="63dp"
android:layout_marginRight="63dp"
android:background="#color/white"
android:enabled="true"
android:visibility="visible"
/>
</RelativeLayout>
What's especially weird is that the progressbar is aligned to the button, but it's still located at the same position as before
here is the java-code
public class GpsSensorDialog extends DialogFragment {
#NonNull #Override public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_gps_sensor, null);
builder.setView(view);
Dialog dialog = builder.create();
if (dialog.getWindow() != null) {
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow()
.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
return dialog;
}
#Override public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.CustomDialog);
}
public static GpsSensorDialog newInstance() {
Bundle args = new Bundle();
GpsSensorDialog fragment = new GpsSensorDialog();
fragment.setArguments(args);
return fragment;
}
#Override public void onStart() {
super.onStart();
getDialog().getWindow()
.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}
and the Style I set in onCreate
<style name="CustomDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
Try this layout. Also, change your drawables, style and strings accordingly.
<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/activity_gps_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/ic_launcher"
tools:ignore="MissingPrefix">
<ImageView
android:layout_width="90dp"
android:layout_height="38dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="17dp"
android:gravity="center_horizontal"
android:text="obsdas" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnActivateGps" />
<Button
android:id="#+id/btnActivateGps"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="71dp"
android:layout_marginLeft="63dp"
android:layout_marginRight="63dp"
android:background="#android:color/white"
android:enabled="true"
android:visibility="visible" />
</RelativeLayout>

Categories

Resources