I'm trying to create a rating bar programmatically.
If I use XML everything works perfectly
<RelativeLayout 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:id="#+id/derp">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:layout_centerHorizontal="true"
/>
And the result is a normal, perfectly functional rating bar with red stars ( my accent color).
But if I try and do it programmatically with this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout l = new LinearLayout(getApplicationContext());
l.setOrientation(LinearLayout.VERTICAL);
l.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
RatingBar ratingBar = new RatingBar(getApplicationContext());
ratingBar.setMax(5);
ratingBar.setStepSize(1);
ratingBar.setNumStars(0);
l.addView(ratingBar);
((RelativeLayout)findViewById(R.id.derp)).addView(l);
}
The result is completely different. The background of the unselected star is invisible, therefore it looks like this.
Prog emprty
The rating bar is there, if you start interacting with it it displays correctly as such
Prog interacting
and as soon as I remove my finger it gets back to an invisible unselected star, but the star that I've selected remain correctly highlighted.
Also the star color is different than the xml one. The rating bar context constructor uses what seems to be the default style though.
This has stared happening all of a sudden in my main app and I can't really figure out why.
I tried to create an empty test app and the problem persist.
Does anybody know what could be the cause?
The applicationContext should usually not be used to inflate a View because your view would then be inflated with the system's Theme, not the theme defined in your application.
Try using your activity context.
RatingBar ratingBar = new RatingBar(MainActivity.this);
To learn more about the different context's capabilities there is a very good article written by Dave Smith
Related
wonder if anyone has came across this problem before. I have an app which detects faces and places touchable squares around the faces, all in a RelativeLayout. When touched I want to add some text into the View which is all working nicely, but when I go to simply add a background to the TextView it just does nothing. I've tried a standard background setBackgroundColor(Color.WHITE); instead of the background I really want to use (setBackgroundResource(R.drawable.nametag);) and still nothing.
TextView nameLabelView = new TextView(activity);
nameLabelView.setText(fullname);
nameLabelView.setTextColor(Color.BLACK);
nameLabelView.setBackgroundColor(Color.WHITE); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
facePreviewLayout.addView(nameLabelView);
Strange one, hope it's obvious to someone out there, thanks in advance!
I have tested your code and it works ok for me. It is necessary to provide more information so I can help more with it. The code below works fine.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView nameLabelView = new TextView(getApplicationContext());
nameLabelView.setText("Test");
nameLabelView.setTextColor(Color.WHITE);
nameLabelView.setBackgroundColor(Color.BLACK); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
FrameLayout selectedFaceView = (FrameLayout)findViewById(R.id.frame_layout);
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
RelativeLayout facePreviewLayout = (RelativeLayout)findViewById(R.id.relative_layout);
facePreviewLayout.addView(nameLabelView);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00FF00" >
</FrameLayout>
</RelativeLayout>
Thank you janzoner for your test really appreciate it, good to know I wasn't going mad. I've found a way to work around it though, I have no idea why this method works and the setup I had before didn't, perhaps I'd been staring at it too long and missed something somewhere else.
Basically beforehand I was adding the name label TextView into the main RelativeLayout (whose children were the actual image and all the face Views etc).
Now I have moved the name label TextView into its own RelativeLayout which sits around the face, and that's magically done the trick. This is a cleaner way of doing things anyway as you can't iterate through child Views of Views but you can children of RelativeLayout's and I will need this later!
Phew!
I am attempting to render a background of a programmatically created set of buttons onto a main layout which has an orange background. I could just adjust the table row background to match the orange but I am trying do this with a single variable change should I need to change the color of the background later on in the code.
The problem begins when I select an option in the app that creates new buttons for the user and the background of the supporting table row for those buttons renders with a white background and does not show the orange main background behind it. The buttons render fine but the OS doesn't seem to take into account the background color/transparent command in the xml file or the android color. I don't use both of these, though I have tried that and had no success. I use either/or in an attempt to get the background to comply with transparent. I must be missing something as the only call from the source file is to overlay the below layout onto another tablerow that is the main layout for the app. It simply adds buttons to the layout when a user selects a certain task.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newTagTableRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color = "#00000000" <----Either this or below line
android:background="#android:color/transparent" <----Either this or above line
>
<Button
android:id="#+id/newTagButton"
android:layout_width="#dimen/tagButtonWidth"
android:layout_height="wrap_content" />
<Button
android:id="#+id/newEditButton"
android:layout_width="#dimen/editButtonWidth"
android:layout_height="wrap_content"
android:text="#string/edit" />
Here is what the activity is doing regarding the table row pasted above:
private void makeTagGUI(String tag, int index){
//get a reference to the LayoutInflator service
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate new_tag_view.xml to create new tag and edit Buttons
View newTagView = inflater.inflate(R.layout.new_tag_view, null);
//get newTagButton, set its text and register its listener
Button newTagButton = (Button) newTagView.findViewById(R.id.newTagButton);
newTagButton.setText(tag);
newTagButton.setOnClickListener(queryButtonListener);
//get newEditButton and register its listener
Button newEditButton = (Button) newTagView.findViewById(R.id.newEditButton);
newEditButton.setOnClickListener(editButtonListener);
//add new tag and edit buttons to queryTableLayout
queryTableLayout.addView(newTagView, index);
}// end method makeTagGUI
As I understand it, the inflator object is just pointed to the tablerrow xml and it follows the setup outlined in the xml.
I want to set some view over action bar that will display Tutorial text (Like click here and send email...). Is this possible? I ask because i know that action bar uses the top space on layout, and a fragment or activity uses remaining space.
My second question is how to display all action items on action bar. I use ActionBarSherlock library and i see that i have room for one more action item, but it's not displaying on action bar. I set in xml ifRoom option on item...
Thanks!!!
There are multiple ways to achieve a tutorial-like overlay. Probably the easiest one is to use specially prepared Dialog window with transparent background and without dim behind.
Using custom Dialog for tutorial overlay
First of all we have to prepare content for the Dialog. In this example there will be one TextView inside RelativeLayout which is the most useful layout here.
Content of info_overlay.xml file:
<?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">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/darker_gray"
android:padding="3dp"
android:text="TextView"
android:textColor="#android:color/white" />
</RelativeLayout>
Now, we can use this layout to create our Dialog:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog overlayInfo = new Dialog(MainActivity.this);
// Making sure there's no title.
overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Making dialog content transparent.
overlayInfo.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
// Removing window dim normally visible when dialog are shown.
overlayInfo.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// Setting position of content, relative to window.
WindowManager.LayoutParams params = overlayInfo.getWindow().getAttributes();
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 100;
params.y = 20;
// If user taps anywhere on the screen, dialog will be cancelled.
overlayInfo.setCancelable(true);
// Setting the content using prepared XML layout file.
overlayInfo.setContentView(R.layout.info_overlay);
overlayInfo.show();
}
Result
Below is the screenshot of the above solution working. Note the TextView over ActionBar.
A few notes about solution
If you'll have a dedicated button to dismiss tutorial you can probably use setCancelable(false) to avoid accidental closing of tutorial.
This solution works with any theme with any action bar solution (either OS-provided, Android Support Library or ActionBar Sherlock)
Other solutions/helpers
Take a look at Showcase View library as it focuses on creating tutorial-like screens in easy way. I'm not sure however that it can easily overlay actionbars.
I have created a custom title bar as shown in this example
http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/
"A custom title bar" - half way down.
On some activities I would like to place a button on the right hand side of the titlebar (same as facebook app). I have attempted to add a button to the view as follows, but it doesn't appear.
Custom title bar is displayed as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.maintabhost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.headerbar_include);
Attempting to add button as follows. The button will eventually be an ImageButton and aligned to right of custom titlebar-if I get it working. (just realised I've too many layoutparams now, but this isnt affecting the button display)
LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);
Button searchButton = new Button(this);
searchButton.setText("info");
LayoutParams layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
searchButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(searchButton, layoutParams);
layout.invalidate();
I could just create another custom titlebar with the button already embedded, but a dynamic solution would be better.
Cheers
First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)
Starting with the xml file, add an id attribute:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<!-- Stuff -->
</LinearLayout>
and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):
LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
layout.addView(new Button(this));
and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):
Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.
Here's one approach to make sure the button(s) remain in the title bar:
How to Create Custom Window Title in Android
Essentially, wrap the android Activity class, and then extend from that new class.
I would like to show progress bar (in form of rotating circle or something similar) when loading image from remote location.
I don't want standard ProgressBar that gets shown in the middle of the screen. I would like to have it in the middle of the imageView (or Layout that holds ImageView). Is there easy way to do this? For example, do I have option to attach progressBar to some View?
I was reading about FrameAnimation. I think I will do that, but first I want to make sure that I am not reinventing the wheel.
Thanks.
Do you mean like a spinner ?
Yes you can do that:
heres a sample code:
<!--Grey Spinner-->
<ProgressBar
android:id="#android:id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!--Black Spinner-->
<ProgressBar
android:id="#android:id/progress"
style="?android:attr/progressBarStyleSmallInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Here are some other style attributes you can use, just replace above style attributes with one of these, e.g. style=?android:attr/<one of the attribute from below list>
progressBarStyleSmallTitle
progressBarStyleLarge
progressBarStyleLargeInverse
progressBarStyleHorizontal
progressBarStyleSmallTitle
You must also note that if you are downloading image from internet do not use UIThread. And you can also add progress bar in the title bar.
Add spinner to title bar with following code(call request.. in onCreate()):
requestWindowFeature(Window.FEATURE_PROGRESS);
//calling setContentView() after requesting
setContentView(R.layout.main);
setProgressBarVisibility(true);
//call setProgressBarVisibility(false); to turn it off
Hope this helps.
Cheers!
you can attach a ProgressBar into ImageView layout holder,
RelativeLayout
-> ImageView
->ProgressBar
then you can set layout align params to the progress bar to show it where you want.
you can do this in a xml layout and inflating later programatically, so, when you start/stop the image download process you can set the ProgressBar visibility on/off