I'm using the standard Android RatingBar. It seems to behave quite weird. For some reason I can't select only full stars. It always shows a little bit more or less. Also the .setNumStars property doesn't seem to work. I'm using the following code:
RatingBar ratingbar = new RatingBar(this);
ratingbar.setLayoutParams(lp);
ratingbar.setVisibility(View.VISIBLE);
ratingbar.setNumStars(5);
ratingbar.setStepSize(1);
ratingbar.setRating(1);
linearLayoutReviews.addView(ratingbar);
When I select 1 star it selects one star:
When I select 2 stars:
When I select 3 stars:
When I select 4 stars:
And when I select 5 stars:
So for some reason it shows more than 5 stars, and there will always be a remainder of the next star selected
EDIT
When I set .setNumStars to 6 I can select them one by one, but than ofcourse, I have six
EDIT
This is the xml of the LinearLayout
<LinearLayout
android:id="#+id/linearLayoutReviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5"></LinearLayout>
You have added RatingBar multiple times LinearLayout that's why its showing previous RatingBar in your LinearLayout .
So remove all current RatingBar from LinearLayout first and then add new RatingBar.
I have added one line in your code see below: it may help you:
RatingBar ratingbar = new RatingBar(this);
ratingbar.setLayoutParams(lp);
ratingbar.setVisibility(View.VISIBLE);
ratingbar.setNumStars(5);
ratingbar.setStepSize(1);
ratingbar.setRating(1);
//In your code add the lines given below
linearLayoutReviews.removeAllViews();
linearLayoutReviews.addView(ratingbar);
I have fixed it by making a new linearlayout and placing it below the old linearlayout. I added the ratingbar to the new linearlayout
Related
My question is about Android/Java.
My main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:ems="5"
android:layout_height="wrap_content"
android:id="#+id/mainEditText1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/mainButton1"/>
</LinearLayout>
My MainActivity.java:
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
e.setError("SO - Please enter a correct E-Mail!!!");
}
});
My picture:
As you can see, my problem is the following: The error pop-up is larger than the width of the EditText.
But I want the error pop-up to be the same width or less than the EditText (containing several lines).
How can achieve this? How can I show the error pop-up correctly and not as my image shows?
Change your LinearLayout to a ConstraintLayout. My guess is its adding the error message to the next available place in the layout. If using LinearLayout in horizontal mode it will put it to the right of the button.
OR
Another good answer is to wrap your EditText in a TextInputLayout and set the error of the TextInputLayout and not the error of the EditText.
I got that from here
How to fix EditText error possition in LinearLayout
Although the answer has no votes i do believe its the right one.
val rating = jsonObject.getInt("rating")
for instance, a rating of 4, to be displayed as 4 stars in my view.
I am looking for ways to display it programatically as image views or to use Ratingsbar
Use RatingBar, in your view:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In your Activity:
Use synthetic Just type the id of your RatingBar and it'll get your RatingBar
import kotlinx.android.synthetic.main.your_activity.*
val rating = jsonObject.getInt("rating")
ratingBar.numStars = rating
or just old school:
val rBar: RatingBar = findViewById(R.id.ratingBar)
rBar.numStars = rating
Edit: As you wanted to know about resizing:
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="5"
android:rating="3.5"
android:scaleX=".5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp" />
You can use scaleX & scaleY to resize accordingly (chose a balanced value of both) the base value is 1 for both. (.5,.5 will make it half of the original size.)
Here's a question about resizing: How can I decrease the size of Ratingbar?
I am trying to set a custom view in an ActionBar. I am using the support.v7 ActionBar - min API is 9. For some odd reason, I cannot get the custom view to appear in the ActionBar. Am I missing something painfully obvious? Any help appreciated.
Here is the layout I am inflating:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flsfjkelsjek"
android:background="#color/black"
android:textColor="#color/white"
/>
...Very simple, just a TextView for testing.
Here is the code to set this view as my custom ActionBar view:
TextView customView = (TextView) myInflater.inflate(R.layout.my_test_textview, null);
myActionBar.setDisplayShowTitleEnabled(false);
myActionBar.setDisplayHomeAsUpEnabled(true);
myActionBar.setDisplayShowCustomEnabled(true);
myActionBar.setCustomView(customView, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Again, very simple and straightforward. I have tried many different varations on this, such as:
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_SHOW_TITLE);
and have even tried that line along with all of the above. I've also tried with a TextView just created on the fly, rather than inflating one.
The custom view simply doesn't appear. When I check the value of
myActionBar.getCustomView()
it returns my TextView. However, the width of that TextView is 0. Does anyone have any idea why the CustomView is not showing up?
I have the following layout which displays an image and a text string:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/number_icon"
android:layout_height="#dimen/number_icon"
android:scaleType="fitXY"
android:layout_marginTop="#dimen/letter_icon_margin_top" />
<Button
android:id="#+id/btnNumber"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="3"
android:text=""
android:gravity="center"
android:textSize="#dimen/word_size_text"
android:textColor="#000000"
android:textStyle="bold"
android:background="#drawable/borderaction" />
</LinearLayout>
The ivIcon is displaying the images based on user input. It worked great when displaying one image per letter. But how do i fix the above layout so it displays the number of images based on user selection.
#dimen is used to change the view size based on the screen.
For example this is what is looking like when I use it for letter:
For the number, if the user chooses 5 the display should be:
If the user chooses 1 the display should be:
The code that is setting the image for the letter is:
ivLetterIcon = (ImageView) findViewById(R.id.ivIcon);
ivLetterIcon.setImageResource(R.drawable.apple);
How would I accomplish the number images?
Would it be best to have a blank layout without the images and add the ImageView at runtim depending on the number and have it centered? This way the images(s) are centered to the parent view layout?
Well I got an idea maybe you can try this out.
Try using GridView for displaying the number of apples by dynamically changing the column numbers and row numbers depending upon the screen.
For ex, if gridView is the GridView and the entered number is 11, you can do this.
gridView.setNumberOfColumns(5);
gridView.setNumberOfRows(3);
so 5x3 can accomodate 15 elements which can fit in 11 easily.
Then inflate the custom view containing the Apple into the columns.
Make the layout_height for the ImageView a fixed height
--edit--
int dp = TheFixedSize;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
Imageview.setHeight(px);
I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound