Trying to create a BMI Calculator I already declared a variable for weight and height, but still I'm getting an error of "variable "weight" might not have been initialized.
public class MainActivity extends AppCompatActivity {
private EditText eheight, eweight;
private Button computeb;
private TextView output, category;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eheight = (EditText) findViewById(R.id.eheight);
eweight = (EditText) findViewById(R.id.eweight);
output = (TextView) findViewById(R.id.output);
category = (TextView) findViewById(R.id.category);
computeb = (Button) findViewById(R.id.computeb);
computeb.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
float weight, height, BMI;
if (checkInputLength())
return;
weight = Float.parseFloat(weight.getText().toString());
height = Float.parseFloat(height.getText().toString());
computeBMI(weight, height);
BMI = weight / (height * height);
}
});
}
Change
weight = Float.parseFloat(weight.getText().toString());
height = Float.parseFloat(height.getText().toString());
to
weight = Float.parseFloat(eweight.getText().toString());
height = Float.parseFloat(eheight.getText().toString());
weight and height are just float variables.If you want to parse data from edittext then you have to get the data from the edittexts and store them in the corresponding variables.
weight.getText()? weight is a float type value which is not initialized here. I think you want to use eweight. And the same with height.
weight = Float.parseFloat(eweight.getText().toString());
height = Float.parseFloat(eheight.getText().toString());
just set it 0.
float weight = 0;
change this:
weight = Float.parseFloat(weight.getText().toString());
to:
weight = Float.parseFloat(eweight.getText().toString());
Related
I am trying to set the layout width programatically .
ViewTreeObserver vtoRecyclerView = mMainLayout.getViewTreeObserver();
vtoRecyclerView.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mMainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
ViewGroup.LayoutParams lpView = mMainLayout.getLayoutParams();
lpView.width = ViewGroup.LayoutParams.MATCH_PARENT-20;
mMainLayout.requestLayout();
}
});
but mMainLayout.getLayoutParams() is returning width as -1 .so when I set width as match_parent - 20 it becomes -21. I want to set the width as match_parent - 20.
What is wrong with the approach.
The root problem with your code here, is because you set the width with:
lpView.width = ViewGroup.LayoutParams.MATCH_PARENT-20;
while ViewGroup.LayoutParams.MATCH_PARENT is a constant.
You can find the constant on ViewGroup class
public static final int MATCH_PARENT = -1;
So that is obvious, because -1 - 20 is -21
What you can do here is, you can change the line to
lpView.width = mMainLayout.getWidth()-20;
try this.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
ViewTreeObserver vtoRecyclerView = mMainLayout.getViewTreeObserver();
vtoRecyclerView.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mMainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
ViewGroup.LayoutParams lpView = mMainLayout.getLayoutParams();
lpView.width = width -20;
mMainLayout.requestLayout();
}
});
Try this.
ViewTreeObserver vtoRecyclerView = mMainLayout.getViewTreeObserver();
vtoRecyclerView.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(mMainLayout.getWidth > 10 && mMainLayout.getHeight() > 10) // you can modify this value.
{
mMainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
ViewGroup.LayoutParams lpView = mMainLayout.getLayoutParams();
lpView.width = ViewGroup.LayoutParams.MATCH_PARENT-20; // This seems to be wrong;
ViewParent parent = mMainLayout.getParent();
if(parent != null) {
lpView.width = (View)parent.getWidth();
}
mMainLayout.requestLayout();
}
}
});
I had the same problem while setting width as match_parent, I had resolved this by getting the phone screen width as follows:
Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int ActualWidth=width-(`margin you set for view`);
Hope it will help :)
As ViewGroup.LayoutParams.MATCH_PARENT constant predefined numeric value is -1 so you are getting -21 as result. and what you want can be achieved by defining margin to a layout rather than using that approach.
I need to get current text color form TextView and then assign this value to TextView.setTextColor(). But I get a large int -1979711488138, how can I get a color from it?
Integer intColor = -1979711488138;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
or
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
Suppose you want to set color from textView1 to textView then you can do like this:
textView.setTextColor(textView1.getCurrentTextColor());
public class MainActivity extends Activity
{
private TextView txtViewIpLable,textView1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
textView1.setTextColor(txtViewIpLable.getTextColors());
}
private void init()
{
// TODO Auto-generated method stub
txtViewIpLable = (TextView) findViewById(R.id.txtViewIpLable);
textView1 = (TextView) findViewById(R.id.textView1);
}
}
You cannot fit the number into an int, I get -1979711488 which is #8A000000 i.e. black with 138 alpha. You can get all parts of the color like this:
int color = getCurrentTextColor();
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
It's really strange why the default text color is not a solid color but rather black with an alpha value as that is more expensive for the system.
I am trying to edit the width of a TextView before it is getting loaded on the screen based on some score value. The requirement is like, based on the relative score value the text view should have width.
I have written the below code in onCreate() method of the activity as below,
{{{
TextView graph = (TextView) findViewById(R.id.predictionScoreGraph);
graph.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int score = 60;
UserData data = UserData.getInstance();
score = data.overallScore();
TextView graph = (TextView) findViewById(R.id.predictionScoreGraph);
int grossWidth = graph.getWidth();
Log.d(TAG, "Existing width of the text view is "
+ grossWidth);
LayoutParams existing = graph.getLayoutParams();
int fillWidth = (grossWidth * score) / 100;
Log.d(TAG, "Modified width is " + fillWidth);
graph.setWidth(fillWidth);
existing.width = fillWidth;
graph.setLayoutParams(existing);
graph.setBackgroundColor(getResources().getColor(
android.R.color.holo_green_light));
graph.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
}}}
It works like a Gem!! But when I want to do it for more than one TextView objects, it is not changing the width of the TextView object.
Can somebody please help me on how to change width of multiple TextView objects?
Thanks,
Rather than using a GlobalLayoutListener, try overriding the onMeasure method of the Activity.
I have an EditText, a Button and a TextView. On clicking the button, textview shows the text written in edittext. Is it possible to find the size of textview occupied depending upon text. i.e. If It has three characters "abc", what is width now, if it has 5 characters like "abcde" , then what is the width ?
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text,0,text.length(),bounds);
int height = bounds.height();
int width = bounds.width();
or
textView.setText("bla");
textView.measure(0, 0);
textView.getMeasuredWidth();
textView.getMeasuredHeight();
Please try this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView edit = (TextView) findViewById(R.id.edit);
edit.setTextSize(20);
edit.setText("Hello, world");
edit.measure(0, 0);
int width = edit.getMeasuredWidth();
Log.w("width", width.toString());
}
Before you get width, you have to measure the view / label / text edit.
Please let me know if this is not working.
TextView txt = new TextView(mContext);
txt.setText("Some Text)";
int height = txt.getLineCount() * txt.getLineHeight();
int width = txt.getWidth();
Try this way,hope this will help you to solve your problem.
yourTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int width = yourTextView.getMeasuredWidth();
int height = yourTextView.getMeasuredHeight();
}
});
please tell me width in??? do you want ?
TextView method getWidth() gives you width of your view, in pixels
TextView textView = (TextView)findViewById(R.id.textview);
textView.getWidth(); //width of your view, in pixels
Im creating a app that takes in a string and divides it into words and reprints them while checking if there is a '#' in front of the words. If there is a '#' the color of that word is changed. The problem i am having is the String gets cut if the original String is too long.
any help?
public class MainActivity extends Activity {
String[] parts;
LinearLayout.LayoutParams layoutParams ;
int size;
LinearLayout L;
String s ="This is the test String that is divided #Testing ";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
L=(LinearLayout)findViewById(R.id.ll);
layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
parts = s.split(" ");
size = parts.length;
for(int i=0; i<size;i++)
{
TextView valueTV = new TextView(this);
String d= parts[i] + " ";
valueTV.setText(d);
// valueTV.setLayoutParams(layoutParams);
if(d.charAt(0)=='#')
{
valueTV.setTextColor(Color.CYAN);
}
L.addView(valueTV,layoutParams);
}
}
}
It is because you are having linear layout
and it takes horizontal orientation.
It will display the text views upto your phone edge and after that your data will not be visible.
You need to check the device display width and manually add another layout if the textView's width exceeds the display width.
Refer this link
It will help you resolve your problem.