Can't set a background attribute twice (image+colour) in Java-Android - java

I've already posted a related question but was partially solved, so here I'll show you the whole code.
The problem is that I can't set a background from a RelativeLayout in white, for example, and set simultaneously, by java code, a background resource (.PNG file) and merge them.
The .PNG image is a prototype of part of the game screen and has transparent space. What I want to obtain is to show this background in white, because of there are details in black that can't be seen because my pre-established background is black (initial theme selected I suppose).
The code below corresponds to the XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/id_act_principal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:keepScreenOn="true"
tools:context=".Principal" >
And the .java file:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
RelativeLayout fondo = (RelativeLayout) findViewById(R.id.id_act_principal);
fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla);
}
The .java file sets the background image correctly, but in the XML file is set to show the white background and it doesn't show it. It keeps being black.
I hope you can help me.

Try this:
Create a drawable named image_with_white_backgroud.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape >
<solid android:color="#android:color/white"/>
</shape>
</item>
<item>
<bitmap
android:src="#drawable/prototipoestructurapantalla"/>
</item>
</layer-list>
In your RelativeLayout replace android:background="#android:color/white" with android:background="#drawable/image_with_white_backgroud"

you can only have one background at a time, try leaving the relativelayout as it is, and insert an ImageView for the png.

Related

Circular button being displayed as a rectangle in a gridlayout

I am trying to display many circular buttons using a recycler view with a gridlayout. However, when I run the application on my emulator the buttons show up as squares. I noticed that when I look at the design view of my button layout file, the constraint layout that stores the button has a transparent rectangle surrounding the button. I'm not sure if my program is automatically filling the constraintview background as well along with the button, but I just wanted to let you know. Here is my onbindviewholder method in my recyclerview that fills the color of my buttons (colorResources is an array within my recycler view that holds the colors I want to fill my button with):
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.button.setBackgroundColor(getResources().getColor(colorResources[position]));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pickedColor=colorResources[position];
}
});
}
Here is my button xml file that is displayed in my recyclerview/gridlayout:
<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="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/button_color_background"
android:id="#+id/colorButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the background xml file for my button referred to in the code above:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="999dp"/>
<solid android:color="#color/purple_700"/>
<stroke android:width="50px" android:color="#636161"/>
</shape>
Please let me know if you need any additional information about my code.
Thanks in advance
Just set width and height of your Button: Like:
android:layout_width="50dp"
android:layout_height="50dp"
Shape should be oval.Use this as drawable instead.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#636161"/>
<solid
android:color="#color/purple_700"/>
</shape>
as long as button is a square this drawable will result in a circle. also you can increase the width in stroke according to your needs.

Android How to get the height equal the width of an component

I want to to have the height of an ImageView equal to it's width. I already read something about using "app:..." in activity.xml.
But, it says that it couldn't find the namespace. After that, I just found a code which didn't worked for me.
Does anybody has ideas or suggestions to fix this issue? If it has to be done programmatically and it would be good if has an example for this using Kotlin.
If you want to have the height of an ImageView equal to it's width,
Programmatically [Dynamic way]:
int widthOfImage = image_view.getLayoutParams().width;
image_view.getLayoutParams().height = widthOfImage ;
refer to this answer
you can do it in Hard-coded way:
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_launcher_background"/>
or you can define background like this image_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#003778" />
<corners android:radius="6dp" />
<size android:width="100dp"
android:height="100dp"/>
</shape>
and add it as background in imageView. [remember it is also hard-coded]
You have to use this custome view inside you xml file.
Example
<YourPackageName.SquareImageView
android:id="#+id/squareImageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher_background"/>
Now you have to bind this view in you code. Like Fragment or Activity whatever you have used.
Exmple (This is code in java you can change it in kotlin) :-
SquareImageView imageView = findViewById(R.id. squareImageview);

Change EditText background color and keep underline

The line underneath the text is a drawable set to the background of the view and when editText.setBackgroundColor(color) is called the following happens internally:
setBackground(new ColorDrawable(color));
This removes the drawable that contained the line and replaces it with the color we gave it.
Is it possible to change the background color of an EditText without having the line under the text disappear?
Setting the EditText inside a layout and changing the layout's background color is not an option.
Try to make drawable some think like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-2dp" android:left="-2dp" android:right="-2dp">
<shape>
<solid android:color="#color/background_color"/>
<stroke android:color="#color/underline_color" android:width="2dp"/>
</shape>
</item>
</layer-list>
The set your EditText background from resources like this:
editText.setBackgroundResource(R.drawable.name_to_xml_file);

Android toolbar replace colorPrimary with a full width background image in Task View or Recent Apps Screen

Hi I have a problem with my toolbar. I'm testing on a lollipop 5.1.1. I have tried:
How to add image in toolbar
This Setting header color of app in overview (recent apps) screen
takes the full width background image and squashes it into the icon.
The toolbar displays the correct background image when i am in the app (on Create or on Resume)
but if I am in on Pause, the default colorPrimary and mipmap icon show as below
I have tried all suggestions on Stackoverflow. I am beginning to think that truly one cannot customise the toolbar in taskView or the recent apps screen.
Does anyone know any unconventional way of having my background image in Task View (recent apps screen) and not the default colorPrimary with the default mipmap icon?
in onCreate, I tried doing the following as well:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
ActivityManager.TaskDescription td = new ActivityManager.TaskDescription("",bm,R.drawable.header);
setTaskDescription(td);
whereby header is a drawable xml file that has myImage in it:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/myImage" />
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
</layer-list>
but get a crash with the warning that the primaryColor must be opaque.

How can i draw a circle that compatible with all android device with fixed radius?

I need to create a program, that have 5 radius buttons. When i'm click on these each button i want to adjust my circle radius.(Circle should have same size in all android phones).
pls help me to find this...
Deducting the screen size before you create a circle and act based on that is a elegant way ,
There are difference sizes of screens in android devices,
so find out the screen size place your co-ordinate values according to the found screen size,
If drawing is only one option for you then the following
thread will be helpful for you to achieve this,
Compatible Canvas draw tip
If not, then button backgounds should be 9 patch image is always better.
Hope , you will find it useful.
You could create a circle-shape in xml and set this as background resource to a button or an imageButton, or you could do your own button-class and override onDraw method. A tutorial with shape is here:
http://dandar3.blogspot.de/2012/10/android-custom-round-buttons.html
or here:
http://yekmer.posterous.com/how-to-make-rounded-buttons-on-android
Instead of using "rectangle" at the shape, You could use "oval".
I donĀ“t know if I understand You right, but making a button that had the same size on every phone is not a good way. Views had to be independent, a view should be created in a way, that it is adjusted to the individual screen size. To do so, use "dp" units in your xml layouts.
Now here is my example:
1.) first create a shape-drawable in the drawable folder:
round_button_oval_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#00ced1" />
</shape>
2.) create a second shape in drawable folder:
rounded_button_oval_shape_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#008b8b" />
</shape>
3.) create a selector in the drawable folder:
rounded_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rounded_button_oval_shape_pressed"
android:state_pressed="true"></item>
<item android:drawable="#drawable/round_button_oval_shape"
android:state_pressed="false"></item>
<item android:drawable="#drawable/round_button_oval_shape"></item>
</selector>
4.)create your main-layout
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textColor="#ff0000" />
<Button
android:id="#+id/rounded_button"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#drawable/rounded_button_selector" />
</LinearLayout>
If you have done this parts, you could anything do with that button in your activity.
RoundedButtonDemo.java
public class RoundedButtonDemo extends Activity {
private Button mRoundedButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRoundedButton = (Button) findViewById(R.id.rounded_button); //initialize your button
mRoundedButton.setOnClickListener(new OnClickListener() { // set Button on click listener
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(RoundedButtonDemo.this, //show a toast when pressing button
R.string.rounded_button_message, Toast.LENGTH_LONG)
.show();
}
});
}
}
The shapes and the selector are needed for showing a pressed behavior of the button. The first shape is a normal button, which is not pressed. the second one is a shape that is pressed. The selector is using the two shapes for showing the pressed state to the user. To get this, set the selector as background to your button in your main.xml .

Categories