I have 2 buttons for increasing and decreasing TextView font size:
btnZoomin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()+1);
}
});
btnZoomout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtview.setTextSize(txtview.getTextSize()-1);
}
});
But are experiencing difficulty, as it will not work.
If anyone is still facing the text size issue. You can use this code:
To increase size: tvText.setTextSize(0, tvText.getTextSize() + 2.0f);
To decrease size tvText.setTextSize(0, tvText.getTextSize() - 2.0f);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_PX,txtview.getTextSize()-1);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_PX,txtview.getTextSize()+1);
How can you see in the DOCS setTextSize needs 2 arguments: the TypedValue and the new size.
Your code is fine, you just have to add the TypedValue :
txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,txtview.getTextSize()-1);
txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,txtview.getTextSize()+1);
Use this text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
Sorry for the previous code, that was wrong but i think this will work for you,
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int counter = 0;
private int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = (TextView) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.button1);
counter = (int) txt.getTextSize();
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), txt.getTextSize() + "",
Toast.LENGTH_LONG).show();
size = counter++;
txt.setTextSize(size);
}
});
}
}
Related
Sorry for my bad English I'm a beginner of android and now I'm stuck.
Now my Question is how can I set the random colors to the background with the OnClickListener. Can you maybe help my with this problem?
I have one class (Kleurenpalet.java)
package com.example.pstek.randomcolor;
import android.graphics.Color;
import java.util.Random;
public class Kleurenpalet{
private static String[] kleur = {
"#39add1", // light blue
"#3079ab", // dark blue
"#3FD52D", // green
"FFFF0000", // red
""};
public int getRandomColor() {
Random rand = new Random();
int color = rand.nextInt(kleur.length);
return Color.parseColor(kleur[color]);
}
}
And I have my main class :
package com.example.pstek.tegeltjeswijsheid;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.solver.widgets.ConstraintWidget;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
int randomColor = new Kleurenpalet().getRandomColor();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
}
});
}
}
in the MainActivity code this :
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
}
}
Perhaps call setBackgroundColor with the initialized color:
layout.setBackgroundColor(randomColor);
Or a different one each time:
layout.setBackgroundColor(new Kleurenpalet().getRandomColor());
I do not understand what you try to do here:
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
Why did you leave empty space there? Moreover, if you parse color in your Kleurenpalet, you must use without ContextCompat. Just set your color like this:
layout.setBackgroundColor(randomColor);
ContextCompat is for parsing color from resources file, for instance:
layout.setBackgroundColor(
ContextCompat.getColor(
getApplicationContext(),
R.color.colorPrimary
)
);
You must put "Random Num" and "layout set Color" in button click event :
randombutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
I'm just a beginner in coding using Android studio and I have a couple of questions in coding a calculator (The calculator is used to only compute carbon emissions so it varies a little bit compared to an actual calculator):
How do I have a clear all button?
How do I add all of the
products that the user has calculated and show its sum? (Kind of like
how those tracking expenses kind of app)
This is my code so far:
package com.carschoolingpromo.scires.carschooling;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button caremissionscheck = findViewById(R.id.checkcar);
caremissionscheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent tocarbonemissions = new Intent(Calculator.this, CarbonEmissions.class);
startActivity(tocarbonemissions);
}
});
Button distancecheck = findViewById(R.id.checkdestination);
distancecheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent checkdistance = new Intent(Calculator.this, DistancesList.class);
startActivity(checkdistance);
}
});
final Button multiply =(Button)findViewById(R.id.multiply);
final EditText num1 =(EditText)findViewById(R.id.carboninput);
final EditText num2 =(EditText)findViewById(R.id.distanceinput);
final TextView ans =(TextView)findViewById(R.id.answer);
final TextView carbontotal=(TextView)findViewById(R.id.sumofcarbon);
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double n1 = Double.parseDouble(num1.getText().toString());
double n2 = Double.parseDouble(num2.getText().toString());
ans.setText(String.valueOf(n1*n2));
}
});
}
}
How do I have a clear all button?
You need to create a new button in the layout file, and get a reference of it in your code, then set a click listener on that reference. Then in the onClick() implementation you will clear your 2 edit texts and 2 text views, something like this:
final Button clearAll =(Button)findViewById(R.id.clear_all);
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1.setText("");
num2.setText("");
ans.setText("");
carbontotal.setText("");
}
});
How do I add all of the products that the user has calculated and show its sum? (Kind of like how those tracking expenses kind of app)
For this you need to use some sort of a way to show a list on your UI, I suggest you look into this tutorial by google to creating lists
am new in android and facing a problem in edit text view need your help. I just want to check EditText before going on next intent. If EditText is filled by at least 1 String then it will go to next intent. May be this is simple for you to implement but this is to difficult for me and I wasted half day on this. Is anyone here which can tell me where is the mistake in this code.
Thank you in advance
package com.example.tricknearn;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Posttittle extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posttittle);
final EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Button postTittleButton = (Button) findViewById(R.id.postTittleButton);
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().length() == 0) {
tittleInput.setError("Please enter some text");
}
}
});
}
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().equals("")){
}else{
startActivity(i);
}
}
}
Try this code instead...
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().isEmpty()){
// Here you can place code while edit text is empty
}else{
startActivity(i);
}
}
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().toString().isEmptry()) {
//edittext is empty
tittleInput.setError("Please enter some text");
} else {
//Edittext is not empty
//start new Activity
}
}
});
And remove postTittleClick method
Replace if (tittleInput.getText().length() == 0) with if (tittleInput.getText().toString.equals(""))
before posting my question , i wanna clarify that is my first post on stackoverflow and let's the story beggin.
as the title said , i'm making my first app on android and i found myself blocked with an issue .
there is 3 button on my app :
button1 : give textview2 "hello world again " and make it VISIBLE
// button2 : make textView2 INVISIBLE
// button3 : make textView1 INVISIBLE
this is the code freom main_activity :
package com.example.ismail.app_test_1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button button_aff;
Button button_hide;
Button button_hide_hw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_aff = (Button) findViewById(R.id.button);
button_hide = (Button) findViewById(R.id.button2);
button_hide_hw = (Button) findViewById(R.id.button3);
button_aff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Show("hello world again");
}
});
button_hide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide();
}
});
button_hide_hw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide_hw();
}
});
}
public void Show(String str)
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.VISIBLE);
text.setText(str);
setContentView(text);
}
public void Hide()
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
public void Hide_hw()
{
TextView text;
text = (TextView) findViewById(R.id.textView);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
}
after getting it on my phone , when i touch any button : "Unfortunatly,app_test_1 has stopped !
can someone help me ?
Edit : i removed the setContentView and it works , thanks a lot guys. if someone has a good tuto that will help me to improve my android programming skills i'm a taker
Do not use setContentView() method outside of the onCreate()
just delete it in show and hide functions. Should work probably
Remove setContentView(text); and it will work. You don't need to call it.
Hi I am developing an Android app using an EditText, and a add en minus button to control it's value.
I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.
Anyone an idea how to repeat the same code for multiple values?
This is my current code:
package com.lars.MyApp;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAppActivity extends Activity {
int currentValue = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText Value = (EditText) findViewById(R.id.Value);
Button addButton = (Button) findViewById(R.id.addButton);
Button minusButton = (Button) findViewById(R.id.minusButton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addValue();
Value.setText("" + currentValue);
}
});
minusButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
minusValue();
Value.setText("" + currentValue);
}
});
}
public void addValue(){
if(currentValue <= 999){
currentValue = currentValue + 1;
}
}
public void minusValue(){
if(currentValue >= 1){
currentValue = currentValue - 1;
}
}
}
You should refactor your OnClickListeners so that they are generic. Probably the easiest way to do this is to change your addValue() method to be addValue(View v), and minusValue() to minusValue(View v). Then, in the layout xml, you add a property android:onClick=addValue or android:onClick=minusValue. You'll have to update the method bodies so that they check the view's id and do the right thing based on that, but you won't have to create and set a whole bunch of OnClickListeners in the onCreate method - you'll get that for free.