How to Create Custom ProgressBar - java

Edit:
Question 1) is solved. But I still wasn't able to change to color of the ProgressBar. I tried to use my own theme (see code below).
I want my ProgressBar to look like the image below. I have already created a ProgressBar above my ListView.I’m using a ListView , ProgressBar , and two TextViews in a RelativeLayout.
My Question:
1.) How can I align the TextViews above the ProgressBar in the way shown below?
2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?
MainActivity.xml
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/progressBarText"
android:progress="0"
android:max="100"
android:paddingTop="0dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:theme="#style/progressBarTheme"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"/>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#android:color/holo_green_dark</item>
</style>
<style name="progressBarTheme" parent="#style/Theme.AppCompat">
<item name="colorAccent">#color/myRedColor</item>
</style>
</resources>

1.) How can I align the TextViews above the ProgressBar in the way shown below?
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:id="#+id/progressCircle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/txtvStatusCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/progressCircle"
android:layout_alignParentStart="true"
android:text="Daily Progress"
android:textSize="18sp" />
<TextView
android:id="#+id/txtPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/progressCircle"
android:layout_alignParentEnd="true"
android:text="0 %"
android:textSize="18sp" />
</RelativeLayout>
2.) How can I set the Color of the ProgressBar itself and the Background Color of the ProgressBar?
In your styles.xml:
<style name="progressBarBlue" parent="#style/Theme.AppCompat">
<item name="colorAccent">#color/myBlueColor</item>
</style>
Then set as the ProgressBar theme in the xml:
<ProgressBar
...
android:theme="#style/progressBarBlue" />
About the ProgressBar itself, what ever you choose for the AccentColor in your styles, will be set for the ProgressBar too. So changing the color of:
<item name="colorAccent">#color/colorAccent/item>
Will do the trick.
Output:

try this:
<android.support.constraint.ConstraintLayout
android:id="#+id/uplaodFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/upload"
android:layout_marginTop="10dp"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="46dp"
android:layout_height="65dp"
android:src="#drawable/file_image"
app:layout_constraintEnd_toStartOf="#+id/horizontal_progress_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/notification" />
<TextView
android:id="#+id/notification"
android:layout_width="282dp"
android:layout_height="22dp"
android:layout_marginBottom="2dp"
android:text="File Detail"
app:layout_constraintBottom_toTopOf="#+id/cancel"
app:layout_constraintStart_toStartOf="#+id/horizontal_progress_bar"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ProgressBar
android:id="#+id/horizontal_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="201dp"
android:layout_height="26dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="24dp"
android:indeterminate="false"
android:max="100"
android:minHeight="100dp"
android:progress="2"
android:progressBackgroundTint="#aaaaaa"
app:layout_constraintEnd_toStartOf="#+id/cancel"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/process_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="uploading.. 10%"
android:textAlignment="center"
android:textColor="#4B74FF"
android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.833"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ImageView
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_close_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
this is the java code
ProgressBar progressBar;
process_status = findViewById(R.id.process_state);
progressBar = findViewById(R.id.horizontal_progress_bar);
progressBar.setProgressTintList(ColorStateList.valueOf(Color.parseColor("#4B74FF")));
progressBar.setProgress(currentProgress);
textView.setText("Uploading..."+currentProgress+ "%");

This changed the color of the progressbar.
progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);

Related

I am trying to create custom modal bottom sheet with line on the top, how can I achieve that?

I tried to change the style, but apart from the round corners nothing worked.Here example of bottom sheet https://i.stack.imgur.com/6O92g.jpg.
Can anyone helps? Here what I tried and I got shadow line after I opened modal https://i.stack.imgur.com/uoHNA.jpg
I am trying to create custom modal bottom sheet with line on the top, how can I achieve that?
MainAct
class MainActivity : AppCompatActivity() {
lateinit var btnShowBottomSheet: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnShowBottomSheet = findViewById(R.id.idBtnShowBottomSheet);
btnShowBottomSheet.setOnClickListener {
val dialog = BottomSheetDialog(this)
val view = layoutInflater.inflate(R.layout.bottom_sheet_dialog, null)
val btnClose = view.findViewById<Button>(R.id.idBtnDismiss)
btnClose.setOnClickListener {
dialog.dismiss()
}
dialog.setContentView(view)
dialog.show()
}
}
}
Bottom_sheet_xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_top_corners"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_rectangle"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<!--image view for displaying course image-->
<ImageView
android:id="#+id/idIVCourse"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="#drawable/ic_launcher_background" />
<!--text view for displaying course name-->
<TextView
android:id="#+id/idTVCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="DSA Self Paced Course"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<!--text view for displaying course tracks-->
<TextView
android:id="#+id/idTVCourseTracks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idTVCourseName"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="Course Tracks : 30"
android:textColor="#color/black"
android:textSize="15sp" />
<!--text view for displaying course duration-->
<TextView
android:id="#+id/idTVCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idTVCourseTracks"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="Course Duration : 4 Months"
android:textColor="#color/black"
android:textSize="15sp" />
<!--button for dismissing our dialog-->
<Button
android:id="#+id/idBtnDismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idIVCourse"
android:layout_margin="10dp"
android:text="Dismiss dialog"
android:textAllCaps="false" />
</RelativeLayout>
</RelativeLayout>
Based on the xml and code you have posted you have to make the below changes:
1.Change the xml android:background structure to be like below. The drawable which has rounded corners must be set to the 2nd child of RelativeLayout instead of parent and all other RelativeLayouts must have android:background="#android:color/transparent":
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#android:color/transparent">
<ImageView
android:layout_width="80dp"
android:layout_height="10dp"
android:background="#color/white"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/rounded_top_corners">
<!--image view for displaying course image-->
<ImageView
android:id="#+id/idIVCourse"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="#drawable/ic_launcher_background" />
<!--text view for displaying course name-->
<TextView
android:id="#+id/idTVCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="DSA Self Paced Course"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<!--text view for displaying course tracks-->
<TextView
android:id="#+id/idTVCourseTracks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idTVCourseName"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="Course Tracks : 30"
android:textColor="#color/black"
android:textSize="15sp" />
<!--text view for displaying course duration-->
<TextView
android:id="#+id/idTVCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idTVCourseTracks"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/idIVCourse"
android:layout_toRightOf="#id/idIVCourse"
android:text="Course Duration : 4 Months"
android:textColor="#color/black"
android:textSize="15sp" />
<!--button for dismissing our dialog-->
<Button
android:id="#+id/idBtnDismiss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/idIVCourse"
android:layout_margin="10dp"
android:text="Dismiss dialog"
android:textAllCaps="false" />
</RelativeLayout>
</RelativeLayout>
where #drawable/rounded_top_corners will be:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<corners android:topLeftRadius="30dp" android:topRightRadius="30dp"
android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>
</shape>
2.You have to set a custom theme style for BottomSheetDialog with a Transparent window dialog like below:
val dialog = BottomSheetDialog(this, R.style.MyBottomSheetDialog)
where R.style.MyBottomSheetDialog is your custom BottomSheetDialog style defined in styles.xml like below:
<style name="MyBottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:colorBackground">#android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.3</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
After the above changes you will have a Transparent Window BottomSheetDialog with a line on top like:

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>

Android RadioButton shows only text, not the actual button in device emulator

I have a group of radio buttons displaying fine in my XML design window, but in the emulator (and on my actual device that I installed the app on) it only shows the button text and not the button.
Here is the XML file for this activity:
<?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=".Question1">
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginEnd="25dp"
android:layout_marginRight="25dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="#+id/errorView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="#string/error_msg"
android:textColor="#android:color/holo_red_light"
android:visibility="invisible" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="#string/my_gender_is"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/gender_group"
android:layout_width="match_parent"
android:layout_height="100dp">
<RadioButton
android:id="#+id/radio_male"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="#string/male"
android:textColor="#636363"
android:textSize="18sp" />
<RadioButton
android:id="#+id/radio_female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/female"
android:textColor="#636363"
android:textSize="18sp" />
</RadioGroup>
</LinearLayout>
<Button
android:id="#+id/nextPage"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoeuil"
android:gravity="center"
android:includeFontPadding="false"
android:paddingTop="-9dp"
android:paddingBottom="5dp"
android:text="#string/next"
android:textAlignment="gravity"
android:textAllCaps="false"
android:textSize="26sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- 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:colorBackground">#color/background</item>
</style>
<!-- No title bar theme -->
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:colorBackground">#color/background</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorPrimaryInverse">#color/primary_text_material_dark</item>
</style>
</resources>
It looks fine in the preview but on my device and in the emulator is seems to exclude the radio buttons.
You are using in a wrong way I guess use it as following:
First declare your custom styles in styles.xml file like this:
<style name="MyRaidoButton" parent="Widget.AppCompat.CompoundButton.RadioButton">
<item name="colorAccent">#0091ea</item>
<item name="android:textColorPrimaryDisableOnly">#f44336</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat</item>
</style>
Once the style is done now all you need is to assign that particular style to your radio button or group like the following:
<RadioButton
android:id="#+id/products"
. . .
android:theme="#style/MyRaidoButton"/>
For more info take a look at the following links:
http://www.zoftino.com/android-ui-control-radiobutton
How to customize default theme of radio button in android?

Setting the background using styles

How to set layout drawable background using styles.xml? In the layout I'm using 2 drawable buttons.
When I using this in styles.xml all buttons change the background to what is in styles, but I want to change only the layout background.
<style name="menutheme">
<item name="android:background">#drawable/menu</item>
</style>
My layout:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/menutheme"
android:contentDescription="#null"
tools:context="com.example.user.app.Activity2">
<RelativeLayout
android:layout_width="312dp"
android:layout_height="371dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.955">
<Button
android:id="#+id/entrycity"
android:layout_width="243dp"
android:layout_height="65dp"
android:layout_below="#+id/button3"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:background="#drawable/oval3"
android:fontFamily="#font/ultra"
android:text="#string/wybor2"
android:textColor="#android:color/background_light"
android:textSize="30sp"
tools:layout_editor_absoluteX="59dp"
tools:layout_editor_absoluteY="334dp" />
<Button
android:id="#+id/button3"
android:layout_width="243dp"
android:layout_height="65dp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/entrycity"
android:background="#drawable/oval"
android:fontFamily="#font/ultra"
android:text="#string/wybor"
android:textColor="#android:color/background_light"
android:textSize="30sp"
tools:layout_editor_absoluteX="71dp"
tools:layout_editor_absoluteY="227dp" />
</RelativeLayout
</android.support.constraint.ConstraintLayout>
try WindowBackground instead of background. Like,
<style name="menutheme" parent="AppTheme">
<item name="android:windowBackground">#drawable/menu</item>
</style>
and you need to apply this theme to acivity in manifest. not in layout.

Android: xml selector for button

I'm trying to use an xml selector to change the image of imagebuttons when they are pressed but I've been faceing a few problems. First of all, the images appear with different dimensions on the screen, and secondly the button changes color only when clicked, but not when it is active.
This is the xml of the layout with the imagebuttons:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/coupon_filter_btns"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#a7a7a7"
android:weightSum="7"
>
<ImageButton
android:id="#+id/filter_greece"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:src="#drawable/greece"
android:layout_weight="1"
android:background="#drawable/greece"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
<ImageButton/>
<ImageButton/>
</LinearLayout>
Below is the selector called greece.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/greece_active" android:state_selected="true"></item>
<item android:drawable="#drawable/greece_active" android:state_focused="true"></item>
<item android:drawable="#drawable/greece_active" android:state_pressed="true"></item>
<item android:drawable="#drawable/filter_white_greece"></item>
</selector>
And this is the java code where the button is used:
ImageView filterGreece = (ImageView) kati.findViewById(R.id.filter_greece);
filterGreece.setBackgroundResource(R.drawable.greece);
filterGreece.setOnClickListener(new ViewListeners(this.getContext()).new OneFragmentFilters(OneFragment.this) ) ;
Ok first there Erro in your XML
you Just set drawable as background Not Src
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/coupon_filter_btns"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#a7a7a7"
android:weightSum="7"
>
<ImageButton
android:id="#+id/filter_greece"
android:layout_width="0dp"
android:layout_height="40dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:background="#drawable/greece"
android:paddingTop="4dp"
android:paddingBottom="4dp"
/>
</LinearLayout>
second selector will work fine without
filterGreece.setBackgroundResource(R.drawable.greece);
so remove this Line

Categories

Resources