Text size not decreasing - android

I'm working on rendering a text on textview. I'm trying to add two buttons to zoom in and out the text inside text view and the code goes like this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter_view);
String chapter;
chapter=getIntent().getExtras().getString("ChapterName");
final StringBuffer buffer = new StringBuffer();
int id= getResources().getIdentifier(chapter,"raw",getPackageName());
try{
DataInputStream dataIO= new DataInputStream(getResources().openRawResource(id));
String strLine= null;
while((strLine = dataIO.readLine())!=null){
buffer.append(strLine);
buffer.append("\n");
}
dataIO.close();
}catch(Exception e){
}
final TextView tv=(TextView) findViewById(R.id.chapter);
tv.setText(buffer.toString());
Button zoomIn = (Button) findViewById(R.id.zoomin);
zoomIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize+0.25));
}
});
Button zoomOut = (Button) findViewById(R.id.zoomout);
zoomOut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textSize = tv.getTextSize();
tv.setTextSize((float) (textSize-0.25));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chapter_view, menu);
return true;
}
}
But the problem I'm getting is the even upon clicking zoom out button, it still increasing the font size of the text. Please help me out in this. Also once I close one chapter and opens another, the text size will reset to its default value. Are there any solutions regarding this. I'm thinking of using the namevalue pair for this solution.

The problem is that the unit passed to setTextSize(float size) is in scaled pixels, whereas getTextSize() reports in pixels. Try using setTextSize(int unit, float size) instead, setting unit to TypedValue.COMPLEX_UNIT_PX.

I've tried a sample at my machine. Check the code below. You have to place the variable at the right place with the scope.
public class MainActivity extends Activity {
float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.textView1);
textSize = tv.getTextSize();
Button btnPlus = (Button)findViewById(R.id.button1);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSizeP", String.valueOf(textSize));
textSize = (float) (textSize+0.25);
tv.setTextSize(textSize);
Log.v("TextSizeP", String.valueOf(textSize));
}
});
Button btnMinus = (Button)findViewById(R.id.button2);
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.textView1);
Log.v("TextSize", String.valueOf(textSize));
textSize = (float) (textSize-0.25);
tv.setTextSize(textSize);
Log.v("TextSize", String.valueOf((float) (textSize-0.25)));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
For example I have my textSize variable declared at the class level. And the textSize variable is increased and decreased when the relevant button is clicked.
This works fine for me.

As for the size that returns to its default , you will have to use shared preferences to save the value of the last text size .
As for the text size , why don't you increment the value one by one , for instance tv.setTextSize(textsize++) when zoomOut is clicked
or tv.setTextSize(textsize--) when zoomIN is clicked

public class MainActivity extends Activity {
TextView tv;
Button in,out;
static float textSize;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,30);
textSize = tv.getTextSize();
in = (Button) findViewById(R.id.zoomin);
out = (Button) findViewById(R.id.zoomout);
in.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize + 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
out.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textSize = (float)(textSize - 2);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Related

How I can change state of widget switch from check to uncheck?

I want to recieve data from thingspeak and change automatically state of switch from checked to unchecked or vice versa.
This is my code, but it doesn't work, just can set text but can't change state.
Please help me!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
myswitch1 = (Switch) findViewById(R.id.switch1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
postdata2();
}
});
t.start();
now_da1.setText(get_da1);
if(get_dk1 == "1") {
myswitch1.setChecked(False);
}
else if (get_dk1 =="0") {
myswitch1.setChecked(True);
}
}
});
myswitch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
t1 = "1";
} else {
t1 = "0";
}
}
});
public void postdata2() {
HttpRequest mReq = new HttpRequest();
get_t1 = mReq.sendGet("https://thingspeak.com/channels/106453/field/1/last");
get_da1 = get_t1.substring(0, 2);
get_dk1 = get_t1.substring(3, 3);
Log.d(myGet, get_dk1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
use this myswitch1.setChecked(false); to uncheck and myswitch1.setChecked(true); to make it checked.
Happy Coding :)
When comparing the value of two Strings always use the equals() method which compares the values instead of using == which compares the object references.
For example:
String first = "1";
String second = "1";
first == second will return false because they are not referencing to the same object. However, first.equals(second) will return true because the value of the two Strings are identical.
You can try replacing the conditions with this:
if (get_dk1.equals("1")) {
myswitch1.setChecked(false);
} else if (get_dk1.equals("0")) {
myswitch1.setChecked(true);
}

Datepicker to parse date

What i would like is when i select a date it filters the rss feed to display the roadworks on the day that is selected. Only for now i start an intent to take me to the next activity which displays all the roadworks and not the ones on the day selected. How would i go about this?
public class ChooseDate extends Activity {
RelativeLayout rl;
Button pick;
DatePicker date;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_picker);
rl = (RelativeLayout) findViewById(R.id.rl);
pick = (Button) findViewById(R.id.button1);
date = new DatePicker (ChooseDate.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
date.setLayoutParams(params);
rl.addView(date);
pick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Date is : " +
date.getDayOfMonth() + "-"+(date.getMonth()+1) +
"-"+ date.getYear(), Toast.LENGTH_SHORT).show();
startActivity(new Intent("com.mobilerwts.robert.MainActivityOther"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If I understand this right, you want to pass the date from the first activity to the second. If you are going to do that, you need to include it in the Intent. Basically, you have to include the data in the intent. See How do I pass data between Activities in Android application? for how to do that.

TextView won't clear for some Reason, Android

I hope this isn't a stupid question. I'm having some trouble clearing a TextView. I've looked around and everyone keeps saying use: textView.setText(""); in onCreate but doesn't seem to work for some reason. Basically, my app just accepts a number from an editText then runs the Fibonacci sequence (when a button is clicked) and displays the result in a textView. Well, the sequence displays fine but I want the textview to clear every time I click the button - so far it just keeps adding more text to what's already there.
Am I placing textView.setText(""); in the wrong location? Or am I just missing some other concept? (I also tried placing it from my OnClick - that didn't work either).
Here is my code:
public class MainActivity extends Activity {
// primary widgets
private EditText editText;
private TextView textView;
private Button button1;
static ArrayList<Integer> fibList = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
//Attempt to clear TextView
textView.setText("");
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;
// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));
// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);
}
}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);
}
});
}
// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If you want the TextView to clear on each button click then the .setText must go in you onClick. The reason you would put the .setText in your onCreate is to clear the text as soon as your activity is created, but you do not have anything to clear just yet since your button has not yet been pushed so setText will do nothing. Also, since your onCreate will only run once for your activity, it will never go back to the setText again. Try the following:
public class MainActivity extends Activity {
// primary widgets
private EditText editText;
private TextView textView;
private Button button1;
static ArrayList<Integer> fibList = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
textView = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(""); //Clear the TextView
fibList.clear(); //Clear your array list before adding new elements
String input = editText.getText().toString();
int number = Integer.parseInt(input);
int tmp = 0;
// confirm input
if (number < 20) {
Toast.makeText(getApplicationContext(),
"You entered: " + number, Toast.LENGTH_LONG).show();
for (int i = 0; i <= number; i++) {
fibList.add(fib(i));
// sum even numbers
if (fib(i) % 2 == 0) {
tmp += fib(i);
}
}
} else {
Toast.makeText(getApplicationContext(),
"Number is too Large: " + number, Toast.LENGTH_LONG)
.show();
}
String array = fibList.toString();
textView.setText(array);
}
});
}
// run fibonacci sequence
public static int fib(int n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
you will need to clear textView on Button click event before adding new results to it.do it as:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(""); //<<<<<< clear TextView on Button Click
.....
The problem is more likely in fibList that is not being cleared

how to hide the context of a text field and show it when click button

how do i hide the context of the text field and show it when i click
the button
i do not want to hide the whole text filed just the context i wrote inside it
and show ot when i click a button
*
this is a small code*
package com.example.nonachan;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
char a;
char b;
char c;
int i = 0;
char buf;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText t =(EditText)findViewById(R.id.t1);
ImageButton n = (ImageButton)findViewById(R.id.b1);
n.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a = 'a';
t.setText(t.getText().toString() + a);
// t.setVisibility(View.INVISIBLE);
}
});
ImageButton a = (ImageButton)findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b = 'b' ;
t.setText(t.getText().toString() + b);
// t.setVisibility(View.INVISIBLE);
i++;
}
});
ImageButton m = (ImageButton)findViewById(R.id.b4);
m.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c = 'c' ;
t.setText(t.getText().toString() + c);
//t.setVisibility(View.INVISIBLE);
i++;
}
});
Button l = (Button)findViewById(R.id.b3);
l.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// t.setVisibility(View.VISIBLE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Assuming you have misspelled contents as context in your question (looks very likely)
instead of trying to hide the contents of the EditText, just save it to a variable and set the text of the EditText to empty. Then in your button click just set the text back to the contents in your local variable
Eg
String hiddenText = null;
EditText text = (EditText)findViewById(R.id.t1);
ImageButton hide = (ImageButton)findViewById(R.id.b1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// save and hide
hiddenText = text.getText();
text.setText("");
}
});
ImageButton unhide = (ImageButton)findViewById(R.id.b2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// unhide the text and 'clear' hiddenText
if (hiddenText != null) {
text.setText(hiddenText);
hiddenText = null;
}
}
});
There are several ways of doing it. It depends how you want to do it. These are just some suggestions off the top of my head, you can even use them all together.
You can put any entered text in a separate string and then if invisible set the TextView to show an empty string, if visible set the TextView to the saved string.
You can use the visibility tag.
You can set the text color to the background color.
You can create/remove the TextView on visible/invisible states.
You can replace the text with symbols for secure inputs if you want (for each character in edittext display an asterisk or something) and then change that back to plain text.
There are a hundred and one ways of doing it, if you search around on previously answered questions you will see several different methods.

Android - How to increase EditText value?

I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}

Categories

Resources