How can I declare my LinearLayout element? - java

In my IDE the LinearLayout-tag' is marked as red and the error message reads: "Element LinearLayout must be declared". I have my directory set up like this:
https://imgur.com/a/deneVxN
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_first_fragment"
android:icon="#drawable/ic_1"
android:title="First"
tools:ignore="HardcodedText" />
<item
android:id="#+id/nav_second_fragment"
android:icon="#drawable/ic_2"
android:title="Second"
tools:ignore="HardcodedText" />
<item
android:id="#+id/nav_third_fragment"
android:icon="#drawable/ic_3"
android:title="Third"
tools:ignore="HardcodedText" />
</group>
</menu>
</LinearLayout>
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">
<ImageButton
android:id="#+id/twitchBtn"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="124dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerInside"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/twitch" />
<ImageButton
android:id="#+id/youtubeBtn"
android:layout_width="253dp"
android:layout_height="249dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/twitchBtn"
app:layout_constraintVertical_bias="0.336"
app:srcCompat="#drawable/youtube" />
<TextView
android:id="#+id/textPlattform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Choose platform"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="38sp"
app:fontFamily="sans-serif"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- This DrawerLayout has two children at the root -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_weight="1"
android:background="#android:color/white"
app:menu="#menu/drawer_view.xml"/>
<!-- This LinearLayout represents the contents of the screen -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar displayed at the top -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I want to acheive a hamburger menu and I'am following this tutorial: https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer but if I run the program now I get a NullPointerExeptionbut I think it's because of drawer_view.xml not working.

Is because the definiton of the drawer_view.xml is wrong
drawer_view.xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_first_fragment"
android:icon="#drawable/ic_1"
android:title="First"
tools:ignore="HardcodedText" />
<item
android:id="#+id/nav_second_fragment"
android:icon="#drawable/ic_2"
android:title="Second"
tools:ignore="HardcodedText" />
<item
android:id="#+id/nav_third_fragment"
android:icon="#drawable/ic_3"
android:title="Third"
tools:ignore="HardcodedText" />
</group>
</menu>
In the activity_main.xml the menu not contains the extesion .xml

Related

Support library BottomNavigationView not showing icons

I'm using Android support design widget BottomNavigationView for making my bottom navigation items. It doesn't show the icons, but it shows text and is working fine. I am using the support library version com.google.android.material:material:1.4.0 and my XML code for bottomnavigationView is:
<?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="#color/white"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/gradient_bg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_30sdp">
<ImageView
android:layout_width="#dimen/_55sdp"
android:layout_height="#dimen/_35sdp"
android:src="#drawable/logo1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_54sdp"
android:gravity="center"
android:text=""
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="#dimen/_45sdp"
android:layout_weight="0.90"
android:background="#color/app_col1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_col1"
app:itemIconTint="#color/white"
app:labelVisibilityMode="labeled"
android:layout_gravity="center"
android:theme="#style/Theme.App"
app:itemTextColor="#drawable/tab_color"
app:menu="#menu/bottom_navigation_menu"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My menu XML code is below
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/page_1"
android:enabled="true"
android:iconTint="#drawable/ic_baseline_home_24"
app:showAsAction="ifRoom"
android:title="Home"/>
<item
android:id="#+id/page_2"
android:iconTint="#drawable/ic_baseline_home_24"
app:showAsAction="withText"
android:title="Category"/>
<item
android:id="#+id/page_3"
app:showAsAction="ifRoom"
android:iconTint="#drawable/user"
android:title="Profile"/>
</menu>
How can I solve this issue?
In your activity layout add the following code.
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/btmMenu"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
app:itemIconSize="#dimen/bottom_navigation_icon_size"
app:labelVisibilityMode="labeled"
android:background="#color/white_color"
app:menu="#menu/menu"
app:elevation="0dp"></com.google.android.material.bottomnavigation.BottomNavigationView>
Create a menu.xml file and add the below code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/Home"
android:title="Home"
android:icon="#drawable/home_selector"/>
<item
android:id="#+id/Statistics"
android:title="Statistics"
android:icon="#drawable/statitics_selector"/>
<item
android:id="#+id/Analytics"
android:title="Analytics"
android:icon="#drawable/analytics_selector"/>
<item
android:id="#+id/Profile"
android:title="Profile"
android:icon="#drawable/profile_selector"/>
</menu>

RecyclerView doesn't animate when it's inside a NestedScrollView

My RecyclerView doesn't animate when scrolling. I've narrowed down the problem to the NestedScrollView that the RecyclerView is in. When the RecyclerView is outside the NestedScrollView it animates fine. But when it's in the NestedScrollView it doesn't. How can I animate the RecyclerView while keeping it in the NestedScrollView?
Here's the relevant layout:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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_toBottomOf="#+id/mainToolbar">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#android:color/transparent"
app:elevation="0dp">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/searchToolbar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:background="#A106A1"
android:elevation="2dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways">
<!-- dummy to catch focus -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:animateLayoutChanges="true"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="1dp" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here's the animation applied in the RecyclerView adapter:
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) { //binds the data to the view
holder.container.setAnimation(AnimationUtils.loadAnimation(holder.container.getContext(),R.anim.fade_out));
holder.nameTextView.setText(labels[position]);
holder.packageNameTextView.setText(packageNames[position]);
holder.iconImageView.setImageDrawable(drawables[position]);
}
Update: Here it is using the CollapsingToolbarLayout instead of NestedScrollView. How do I make the toolbar scroll?
<androidx.coordinatorlayout.widget.CoordinatorLayout
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_toBottomOf="#+id/mainToolbar">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#android:color/transparent"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/searchToolbar"
style="#style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:background="#A106A1"
android:elevation="2dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll">
<!-- dummy to catch focus -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:animateLayoutChanges="true"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
It is not a good way to put RecyclerView inside NestedScrollView. If you want to make SearchBar above RecyclerView, you can check this.
You can also check this refference.
I checked your xml and modified it. It is working on my side. Please modify it according to your UI requirements.
N.B.: Check after populating data on recyclerview. First it may seem it is not working due to no data on recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/tab_anim_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:fitsSystemWindows="true"
android:background="#android:color/transparent"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="#+id/toolbar_scrolling">
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:animateLayoutChanges="true"
android:background="#drawable/rounded_bg"
android:layoutDirection="rtl"
app:iconifiedByDefault="true"
app:searchHintIcon="#null" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/myRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In activity:
getSupportActionBar().setBackgroundDrawable(getResources()
.getDrawable(R.drawable.rounded_bg_action_bar));
rounded_bg_action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp" />
</shape>
rounded_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomLeftRadius="100dp"
android:bottomRightRadius="100dp" />
</shape>

How do you remove the gap between NoActionBar and ImageView in Android Studio

How do I remove this gap in a ConstraintLayout? The top is an ImageView with a top constraint to the top of the parent (ConstraintLayout).
Gap at the top between the toolbar and banner
This is how my styles.xml looks like:
<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>
My activity_main.xml looks like this:
<?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/headerImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/header" />
<Button
android:id="#+id/addlog_btn"
style="#android:style/Widget.DeviceDefault.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="108dp"
android:background="#drawable/newlogbtn"
android:fontFamily="#font/roboto_regular"
android:onClick="createNewLog"
android:paddingTop="72sp"
app:layout_constraintStart_toStartOf="#+id/headerImg"
app:layout_constraintTop_toTopOf="#+id/headerImg" />
<Button
android:id="#+id/addlog_btn2"
style="#android:style/Widget.DeviceDefault.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="#drawable/settingsbtn"
android:fontFamily="#font/roboto_regular"
android:paddingTop="72sp"
app:layout_constraintEnd_toEndOf="#+id/headerImg"
app:layout_constraintTop_toTopOf="#+id/addlog_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
My end goal is this:
I would like to achieve this with just editing the XML if it is possible, I tried to change the margins and padding's of both parent and I didn't do it right or it just doesn't seem to work.
You can remove this gap by adding android:scaleType="centerCrop" to the banner ImageView so that it will fill the entire assigned room to it.
So the layout will be after this change:
<?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/headerImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/header" />
<Button
android:id="#+id/addlog_btn"
style="#android:style/Widget.DeviceDefault.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="108dp"
android:background="#drawable/newlogbtn"
android:fontFamily="#font/roboto_regular"
android:onClick="createNewLog"
android:paddingTop="72sp"
app:layout_constraintStart_toStartOf="#+id/headerImg"
app:layout_constraintTop_toTopOf="#+id/headerImg" />
<Button
android:id="#+id/addlog_btn2"
style="#android:style/Widget.DeviceDefault.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:background="#drawable/settingsbtn"
android:fontFamily="#font/roboto_regular"
android:paddingTop="72sp"
app:layout_constraintEnd_toEndOf="#+id/headerImg"
app:layout_constraintTop_toTopOf="#+id/addlog_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>

Why child is clipped in custom toast?

I want to create custom toast with, one view which is a little bit out of parent. So I did something like this
This is my custom toast
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/toast_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="50dp"
android:background="#000000"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="#+id/digit_display"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_marginTop="-24dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="24dp"
android:background="#drawable/round_button"
android:gravity="center"
android:text="1"
android:textSize="64sp" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TextView"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
round_button.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#000000"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#2C2C2C"/>
</shape>
</item>
</selector>
and in java code I am using it like this
LayoutInflater li = getLayoutInflater();
View v = li.inflate(R.layout.toast, null);
TextView numberView = v.findViewById(R.id.digit_display);
TextView descriptionView = v.findViewById(R.id.description);
numberView.setText(number);
descriptionView.setText(description);
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setView(v);
t.show();
And I can see my toast. But digit_display TextView is clipped from the top but I would like to see it out of toast (just top of that view). How can I achieve it? If that does not work.
EDITED
I try to understand why this example does not work, when if I am using similar structure in other project form simple form it is working.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="25dp"
android:background="#drawable/form_desing"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="center"
android:orientation="vertical"
android:padding="25dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="-50dp"
android:layout_marginBottom="50dp"
android:foreground="#drawable/clover_"
android:contentDescription="#string/fourbit_clover_icon" />
<include layout="#layout/email_input" />
<include layout="#layout/password_input" />
<include layout="#layout/buttons" />
</LinearLayout>
any idea?
Think of outer-most Layout as your Toast's root view and arrange items inside it. You're NOT limited to only use one LinearLayout or RelativeLayout . Don't try to go out from root view: It will get clipped.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="top|center">
<LinearLayout
android:layout_height="50dp"
android:layout_width="wrap_content"
android:orientation="horizontal"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="200dp"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp"
android:background="#0A93E1"
android:minHeight="100dp"
android:layout_marginTop="30dp">
</LinearLayout>
<TextView
android:layout_height="60dp"
android:layout_width="60dp"
android:text="Text"
android:id="#+id/digit_display"
android:background="#drawable/round_button"
android:gravity="center"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Text"
android:id="#+id/description"
android:layout_below="#id/digit_display"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textColor="#FFFFFF"/>
</RelativeLayout>
</LinearLayout>

Navigation drawer onClick in items ripple effect lost

Using the new appcompat-v21 I try to build a navigation drawer.. The first time I didn't write any background color or drawable and the navigation drawer started with a transparent background!! So strange! But, when I clicked in the items of it I could see the ripple effect of Android lollipop. (Amazing). So I tried to put a white background like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".HomeActivity"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
</RelativeLayout>
<ListView
android:id="#+id/navdrawer"
android:layout_width="315dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
The navigation drawer now is white and so it's correct. But with this background I lost the ripple effect in the item click. My item XML is this one:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="?android:selectableItemBackground"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/spinnerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/sfondomappa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="0"
android:adjustViewBounds="true"
android:scaleType="center"
android:src="#drawable/banner_nav" />
<TextView
android:id="#+id/drawerWelcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/sfondomappa"
android:layout_centerHorizontal="true"
android:background="#40000000"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="10dp"
android:text="Hola"
android:textColor="#color/ldrawer_color"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:id="#+id/drawerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:layout_margin="12dp"
android:fontFamily="sans-serif-regular"
android:gravity="center_horizontal"
android:textColor="#color/main_orange"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:padding="3dp" />
<TextView
android:id="#+id/drawer_itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="32dp"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:textColor="#E4000000"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I even wrote this android:background="?android:selectableItemBackground" in my root layout but nothing change.. How can I solve?
EDIT:
I wrote again the listview of my drawer in this way
<ListView
android:id="#+id/navdrawer"
android:layout_width="315dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="72dp"
android:background="#android:color/white"
android:choiceMode="singleChoice"
android:divider="#00000000"
android:dividerHeight="1.00dp"
android:elevation="2dp"
android:longClickable="false"
android:orientation="vertical"
android:overScrollMode="never"
android:scrollbars="none" />
and the ripple works. But now i lost the current position i clicked.
I've faced the same problem ... the solution is not documented good enough, but here it is.
Create a selector drawable in your drawable-v21 folder:
v21/activated_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/primary_dark" android:state_activated="true" />
<item android:drawable="#color/primary" android:state_checked="true" />
<item android:state_pressed="true">
<ripple android:color="#color/primary" />
</item>
<item android:drawable="#color/background_light" />
</selector>
As you can see in the code above, the magic is done by adding the <ripple ... /> element above. It's available in v21+ only, so that's why you have to declare separate selectors for -v21 and normal.
Lastly, just put this selector drawable as background of the desired element, in your case the ListView item.

Categories

Resources