I'm trying to set a relative layout's background with,
relativeLayout1.setBackgroundColor(0x00000000);
My program keeps crashing though. Here's the logcat.
Code:
RelativeLayout window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
That's the only stuff apart from the regular code setContentView(R.layout.something); and super.onCreate(savedInstanceState);
Entire code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invisible);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);
}
From the logcat attached, I can say that most likely your window pointer is null at the time you are trying to set background color. It can be caused by different types of problems:
Your something.xml layout does NOT contain element with android:id="#+id/window" attribute
Your something.xml layout DOES contain element with android:id="#+id/window" attribute, but this element is not RelativeLayout
Your project resource data got messed up. Try to do Project->Clean to rebuild resources
Is there is your R.layout.something a layout with the id R.id.window ?
Maybe you've missed something?
You can use this instead
relative layout.setBackgroundDrawable(get resources().get drawable(R.drawable.bg);
Or you could define it in XML
android: background="#drawable/bg"
This was another of Eclipse's moments. It didn't compile my code, so I was executing the old code. Restarted Eclipse and my app works fine now.
Related
I am using a Translucent AppCompatActivity, and in the layout of that activity I am trying to use an ImageView. I am putting the src of the image by using the srcCompat tag. I do the see the image on the design view of the Xml file, but not able to see that being rendered on the phone screen.
Thanking in advanced.
First try using the Layout Inspector tool to verify that your ImageView has at least its width and height well defined. Go to Android Studio->Tools->Layout Inspector and analyze your layout.
https://developer.android.com/studio/debug/layout-inspector?hl=en
You need to add vectorDrawables.useSupportLibrary = true inside your Gradle file if it not there. Browse to this https://developer.android.com/guide/topics/graphics/vector-drawable-resources you can find more data here. If it is not resolved by adding this, you can use android:src attribute instead of srcCompact
I have just begun learning Android App development. I have Android Studio 1.4. In my layout folder I have two XML files (content_main.xml and activity_main.xml). I use online tutorials to learn but they have only activity_main.xml in them. So what i want to know is what are the functions that should be used in these respective files. can i just use activity_main.xml and just let the other be ? and vice versa.
The modern Android approach is based on Fragments, which are, in a way, "small Activities", which you can put in Activities, gaining lots of flexibility.
Therefore, activity_main.xml is simply the Activity layout containing a container (FrameLayout most probably) and content_main.xml is the layout for a Fragment put into this container somewhere within MainActivity.java. You should study the code in there to understand it better :)
As I know, there must be include statement in your activity_main.xml file as follows :
<include layout="#layout/content_main" />
that means it is calling the content_main.xml which has actual elements to be hold.
There will be no problem if you cut and paste all the content of content_main.xml file and paste it in activity_main.xml file in place of include statement(tag).
You can delete your content_main.xml after doing as above.
In your activity setContentView() statement should be look like as below :
setContentView(R.layout.activity_main);
According to new design pattern in android studio activity_main.xml will determine how the look of the main activity should be. And on the other hand content_main.xml will determine the contents in the activity_main.xml. That is content_main.xml will contain the textview, edittext, button etc component. And the content_main.xml will be included by the activity_main.xml .
So we can think of content_main.xml just like partial in HTML.
activity_main.xml will contain your activity global design, and
content_main.xml the contents.
What is the role of content_main.xml in android studio 1.4?
So it seems the content_main.xml is a part of a new design pattern introduced in Android Studio 1.4.
For the moment, to get along with the tutorials you can find pick the 'empty activity' when creating a new project. It will not contain the content_main.xml.
As mentioned before, the layout file used for your activity is set with setContentView(R.layout.activity_main); in the onCreate function of the activity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have not seen Android Studio creating two layout files for one activity. Perhaps the content_main.xml was generated for a previous activity, wasn't it?
Anyway, it doesn't matter what is the name the layout file. Choose one and go for it. Just remember to set the right one in your Activity:
#Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.your_layout_here);
}
use that one which is set in Activity class. i mean set to setContentView(). or please provide your code if you want more description.
i downloaded zxing library and installed it on my device. now i tried to make a simple app the extends zxing library. in this simple app I created a new layout for it and when i tried to setmycontentview to be this layout, eclipse highlits it with red and when i move the mouse pointer on the highlighted layout, eclipse suggests the following:
1-create the field "activity_firstactivity" in type layout
OR
2-create constant "activity_firstactivity" in type layout
now, my question is:why eclipse can not reach my xml file for this layout, i created the layout inside the layout folder but eclipse can't recognise it.
UPDATE:
even when i press ctrl+space the layout name is not listed
Java Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstactivity);
probably Eclipse is trying to find the layout in the android package. Try to access your layout through the full name of your package:
your.package.name.R.layout.activity_firstactivity
I have a weird problem. While running my Android application, I receive Exception:
java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.RelativeLayout
the code, where I try to get relativeLayout
v = inflater.inflate(R.layout.home, null);
RelativeLayout btn = (RelativeLayout) v.findViewById(R.id.my_button);
in xml, I've got this:
<RelativeLayout
android:id="#+id/my_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_selector"/>
While debugging, I've noticed that in v there is Button with mID equals to R.id.my_button.
Although, when I change id in xml and code to anything else, I receive NullPointerException.
Any ideas why this is happen ?
EDIT:
Maybe it will be helpful, if I add that in older version of that file there was Button, but was replaced with RelativeLayout. I'm using GIT. Project was cleaned many times and eclipse was restarted also.
Delete the R.Java file and once recreated run the app it will work fine.
The problem was that I've made one more layout file that I forgot about and it was for the same Activity byt different configuration. Problem solved.
I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.