I have added a imagebutton dynamically to my Tablerow, now i want to define the style for the image button to a style that i have previously defined. I have tried the setTheme method that doesnot seem to work
Thanks for any help
k after a lot of searching this is what i found and it works perfectly.
1) if you want to create a button/imagebutton/anyView at runtime with a predefined style the you can do so just follow the steps below
2a) Create a new xml file in res/layout and put that one element you want to create for example I wanted to create a minusButton. This is minusb.xml in res/layout
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/MinusButton"/>
2b) Then add the following lines to your java activity code where you would like to create it
ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
tr.addView(bt);
Related
I know this question has been asked a lot (see: How to customize a Spinner in Android,
How to change layout of spinner in Android, How to change spinner text size and text color?, How to change spinner text color)
All the answers suggest to create a custom_spinner.xml file to accomplish this task. This file must be something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#color/#FFFFFF" />
However, no one says where to locate this file. In this answer https://stackoverflow.com/a/37859442/5616178 says this file must be in Drawable folder, but when I do that, an "Element TextView must be declared" error is raised by Android Studio. When I locate it in the layout folder, the R.layout won't resolve it either.
Thank you for your answers.
EDIT
I was able to solve the problem. As many of you said, the file must be located inside the res/layout since it's a layout resource. At first, my code looked like this:
citiesSpinner.setAdapter(
new ArrayAdapter<String>(SignUp.this,
android.R.layout.custom_spinner,
cityNames)
);
When I declared the adapter outside the constructor, i.e.:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SignUp.this,
R.layout.custom_spinner,
cityNames);
citiesSpinner.setAdapter(adapter);
The R class was able to resolve the layout. I don't know why that happens, it would be useful if someone can explain it. Thank you again for your answers!
However, no one says where to locate this file.
That is a layout resource. It would go in res/layout/ by default.
When I locate it in the layout folder, the R.layout won't resolve it either.
Then you have some other problem. For example, perhaps there is a bug in your layout resource that is preventing the R class from being regenerated.
Your resource file must be in the layout folder
Your android:id="#android:id/text1" need to be android:id="#+id/text1"
Just declare your array like the links above recommended :
ArrayAdapter<String> adapter = new ArrayAdapter<String>this,R.layout.custom_spinner,yourArray);
I built a layout like this in XML say "block.xml"
Now, i want to create an XML like this
so that i can access each block as an array. Can somebody tell me how can i use block.xml as a template to generate my new xml file to be put as a UI part in android. Thank You
Just to be sure , i want to use table layout i dont know how to procees.
In your layout xml you may use the include tag and re-use your block.xml
as:
<include
android:id="#+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/block" />
And in your java code you can access views from block.xml as
View firstBlock = findViewById( R.id.block1 );
View blockButton = firstBlock.findViewById( R.id.someButtonId );
You should inflate and add the sub layouts programmatically.
This link has example code:
Android using layouts as a template for creating multiple layout instances
I have the following menu xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_select_appshare"
android:showAsAction="always"
android:title="#string/action_select_appshare"
android:actionViewClass="android.widget.Spinner" />
</menu>
Because a Spinner seem to require the definition of the android:spinnerMode attribute from the get-go, and there doesn't seem to be a proper way to set it later on after the Spinner object is created. I was wondering if there's a way to specify additional attributes (such as android:spinnerMode in my e.g.) for an android:actionViewClass within the menu XML?
Thanks!
Finally found it:
Related links: 1, 2 and 3
Create a new res/layout file. Put the spinner in it, with all the properties you need/want. Note that the Spinner should be the root view.
In your menu file containing the actionClassProvider=spinner, remove actionClassProvider, and replace it with actionLayout="#layout/my_spinner"
All Done
I hope this helps somebody!
well you could solve it in 2 ways: create a custom spinner and put what you want there, or, get the actionView when inflating this menuItem, and then do what you want with it.
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.
I am need a logic to change the xml background programatically instead of giving the backgroung color from xml file. here i used setContentView(R.layout.main); to show the xml file in application. But i need to change the background color of main.xml file in java code.
Following is the example that shows how can you make it. Here i have taken LinearLayout as the root node of xml file. In java code i have shown that how to set white background. I think this code will help you.
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
-----
-----
</LinearLayout>
java code
LinearLayout root=(LinearLayout)findViewById(R.id.root);
root.setBackgroundColor(Color.WHITE);
It can also possible to set color using Hex code as shown below,
root.setBackgroundColor(Color.parseColor("#ffffff"));
Even you can set drawable also,
root.setBackgroundResource(R.drawable.bg_image);
Here is how you can do that,
mLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
mLayout.setBackgroundColor(Color.BLUE);
Here mLayout is your parent RelativeLayout.
get the instance of the root layout of your xml and set it's background color from your code as you wish as many times you want but do all this in your UI thread..
GradientDrawable sd = (GradientDrawable)this.getResources().getDrawable(R.drawable.roundbordertry);
sd.setColor(Color.rgb(5, 5,5)); newHeadView.setBackgroundDrawable(sd);
//////////////////////////////////////////////////////////
roundbordertry is the xml file.
newHeadView is a linearlayout.