I implemented a RecyclerView and I can't figure out how to get touch feedback (the ripple effect from it).
Here is what i did for the onClickListener:
holder.itemView.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//start Intent
}
});
And I added both clickable and focusable to my XML. This is what the recycler view inflates:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:padding="4dp" >
You have to set a ripple drawable as the background:
android:background="#drawable/ripple"
ripple.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffa0a0a0"/>
You may need to mask the drawable:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffa0a0a0">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#ffa0a0a0"/>
</shape>
</item>
</ripple>
This will create a simple grey ripple upon touch (here's a guide if you need more instructions).
RippleDrawable was added in SDK version 21 (Lollipop). Using Ripple drawable on pre-lollipop will crash the app. Either use a simple selector on pre-lollipop devices or use libraries that recreate the effect. (GitHub)
UPDATE: You can get the ripple effect easily with this piece of code:
android:background="?attr/selectableItemBackground"
or if you don't want the rectangle mask:
android:background="?attr/selectableItemBackgroundBorderless"
This is compatible with pre-lollipop devices and will us a simple selector. I believe this will create light ripples in apps with dark theme and vice versa, so if you want a custom colored ripple you will still need to create a ripple drawable.
In addition to #Longi's answer: If you want your RecyclerViews's Item to have its own background, and at the same time to have the ripple effect, you can do as shown in the example below:
recycler_view_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/recyclerview_item_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackgroundBorderless">
...
</LinearLayout>
</LinearLayout>
In this example #drawable/recyclerview_item_background is a nine-patch png, but you can use any background here.
In my case when I used android:background="?attr/selectableItemBackground", the RecyclerView Item's root Linear Layout did have the ripple effect, but the child Linear Layout's background was overlapping thus hiding the ripple effect.
Related
I'd like to change and align the text, and possibly add features on appbar in the screen above.
However, it'd be best if I could find and modify the text and align it IF I don't intend to add features.
But then I wasn't able to find where in android studio I could change the text.
I thought I could find one in the main activity xml file but I couldn't. I looked up at themes.xml and AndroidManifest.xml but couldn't find a clue.
Here is first 13 lines of my main activity xml file just in case.
<?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">
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="480dp"
app:layout_constraintTop_toTopOf="parent"/>
So, to cut it short, where in android studio can I change the text in toolbar? or appbar? above in the screen without having to create toolbar.xml? (+ align the text in the center and change text color)
Apparantly you don't have toolbar implemented in your xml layout
In Your style file make sure you set it to noActionBar
<style name="Theme.ChatApp" parent="Theme.AppCompat.Light.NoActionBar">
Then you have to add your own toolbar
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/toolbar"
android:background="#color/white"
app:navigationIcon="#drawable/ic_baseline_dashboard_24"
app:title="here you can put your title"/>
In your activity reference your toolbar in your onCreate
setSupportActionBar(findViewById(R.id.toolbar)
PS : If you are using MaterialToolbar please consider add this material library
implementation 'com.google.android.material:material:1.1.0'
I am trying to build Splash Screens the Right Way like this https://medium.com/#ssaurel/create-a-splash-screen-on-android-the-right-way-93d6fb444857 for that i have to place my app icon .png file inside a drawable layer list as bitmap like this
<item
android:drawable="#color/gray"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/logo"/>
</item>
Now how can i resize this bitmap? Thanks...
It is not possible to reset a new resize through XML, and you can confirm this link.
In the code method, there are different and complex methods. If you want to know more, you can look at this question
But if your goal is only to change the size, this can be done through ImageView
if you want full image set to your scree (best for splash screen) that use match_parent to image view , This method is easier and faster than other methods:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_centerInParent="true"
android:src="#drawable/splash_screen" />
</RelativeLayout>
I am building an android web browser and I just wish to know how chrome added text(numbers) to the tab switching button?
Here's an image:
Is it simple text wrapped in a drawable? If yes, then how to do that and increment the number when a new tab is opened?
If no, then what's the way to achieve this?
It's just a TextView. Background of the TextView is a custom drawable(xml).
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12sp"
android:textColor="#000000"
android:text="12"
android:background="#drawable/my_text_drawable"/>
Sample my_text_drawable.xml is like below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:color="#999999" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
Judging from the layout boundaries(enabled from Developers Option), It is a custom view or drawable.
This kind of views or drawables are made to serve special purpose and to have control over your layout. But there are also much more time consuming to make, most of the times.
This is a good starting point for custom views. This video taught me a lot in this regard.
I want to make an activity which has a gradient drawable as background and is going to show 4 panels (RelativeLayout) on top of it's background. now I want to make the 4 panels transparent(e.g. 50%) so that the gradient background could be seen, too. I searched google, but I found only ways to do this with activities not layouts. how to do what I want?
You can create a drawable with shape content. And put your styles in it.
Sample:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#77ffffff"/>
</shape>
Create a file with a name such as sampleBackground.xml in the drawable folder in the res path. And set the
background attribute of your panels to it:
android:background="#drawable/sampleBackground"
You can use alpha attribute for your layout if you want to make it transparent. It can be used in following way:-
<RelativeLayout
android:id="#+id/yourRelativeLayoutID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:alpha="0.7"
android:gravity="bottom">
</RelativeLayout>
set the value of alpha between 0.1 to 1.0. It depends on level of transparency you want.
You can use this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</RelativeLayout>
I was able to get a transparent background on RelativeLayout elements by specifying a background color:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#color/modal_background" >
</RelativeLayout>
And in my colors.xml I created a color called modal_background with an alpha value. This particular example is black (#000000) with an alpha value of CC:
<resources>
<item name="modal_background" type="color">#CC000000</item>
</resources>
I've recently started working on an Android app. I'm not a programming noob, I have been programming in Java since 2009, but this is my first Android app. I have tried following some tutorials on how to set the background colour but it simply isn't working for me. I can't work out what I'm doing wrong.
In my layout directory I have files called: main.xml and view_fact.xml. They both use the linear layout and my LinearLayout Tag is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
And in my "values" directory I the contents of "strings.xml" is:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Facts</string>
<color name="background_grey">#E8E8E8</color>
</resources>
But the background colour is not changing from the default black.
I've also tried adding: "android:theme="#android:style/Theme.Light"" in my AndroidManifest.xml file.
None of this is working, any suggestions?
Thank you.
On my view_fact.xml page it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fact:"
android:padding="10dp"
android:textColor="#080808"
/>
<TextView
android:id="#+id/factData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=""
/>
<Button
android:id="#+id/anotherFactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Another Fact"/>
</LinearLayout>
What's interesting is that the text in "factData" is grey, while the text: "Fact:" in the Text View above is white, and I've tried to change it's colour to red and it doesn't work. It stays white.
Any suggestions? I still haven't managed to get this working; thank you.
If you want to use #color/color_name you have to create a Color.xml file and then it will be accessible in the manner you tried to use it.
Forgive the somewhat obvious questions, but: are you calling setContentView(R.layout.main) in your Activity onCreate? Is there something else in the layout that is filling the LinearLayout and covering the background grey?
Looking at the pastie you linked in the comment I think you need to remove the <?xml version="1.0" encoding="utf-8"?> line at the start. Eclipse won't build a freshly created app with that xml root in a layout..
Although that's strange as the first two examples I've found on the Android Developer site (for example) include it
I'd be interested to see what happens if you remove that line...
In Eclipse if I use
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E8E8E8"
>
then the background colour changes.
If I change that to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#string/background_grey"
>
by adding <string name="background_grey">#E8E8E8</string> to /res/values/strings.xml the background changes.
This also works if I declare it as android:background="#color/background_grey" in the layout and <color name="background_grey">#E8E8E8</color> in strings.xml
and if I setup res/values/colors.xml <= I've used colors.xml (while you've used color.xml) however http://developer.android.com/guide/topics/resources/more-resources.html tells us the filename is arbitrary as you can see by the declaration working in the strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_grey">#E8E8E8</color>
</resources>
and declare the layout as
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_grey"
>
then the background also changes...
If it doesn't behave the same for you then there's something funky going on or you're using a different layout than the simple example above and, well, there's something funky going on in that layout.
Okay, I've tried lots of different methods of trying to set the background colour and none of them are working. And I'm now having problems with text not updating. So I'm going to try remaking it. Using eclipse and ill make a new question if I'm still having issues. Thanks for the suggestions.