Making text bigger and smaller - java

I'm trying to make an application on Android Studio. I have 2 buttons. a plus and a minus. I need to know how to make the text smaller and bigger everytime I click on one of these buttons.
This is my MainActivity.java:
Button Min = (Button)findViewById(R.id.Min);
Min.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(-5);
}
});
This is my .xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Plus"
android:layout_gravity="top|left"
android:text="+"
android:textSize="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Min"
android:layout_gravity="top|right"
android:text="-"
android:textSize="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp"
android:id="#+id/DeText"
android:text="Text"/>
I also have made a screenshot of the page I'm working on. Hopefully this gives some more information about my end product:

You simply set the text size by getting the current size and adding/subtracting the value you want.
Button minButton = (Button)findViewById(R.id.Min);
minButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView t = (TextView)findViewById(R.id.DeText);
t.setTextSize(COMPLEX_UNIT_PX, t.getTextSize() - 5f);
}
});
It would be better to check if the current text size is not too small before reducing the size.

You should use sp instead of dp for text sizes. From documentation, you can use setTextSize method and it includes two arguments.
void setTextSize (int unit,
float size)
So, you can specify the text size after button click as:
Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView text = (TextView)findViewById(R.id.DeText);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,text.getTextSize()-1);
}
});

Related

ScrollView to show layout if expandable layout goes outside the screen

I have several expandable layouts in my Android App. When I click to expand a layout, the layout disapear outside the screen, and I have to manual scroll down to make it visible. How can I make the ScrollView automatically scroll down to make the clicked layout visible?
I tried using scrollView.scrollTo(0, textID.getBottom()); to scroll to the bottom of the layout element, but without luck.
Java:
expandableLayout1 = root.findViewById(R.id.expandable_layout1);
button = (LinearLayout)root.findViewById(R.id.box_header);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if (expandableLayout1.isExpanded()) {
expandableLayout1.collapse();
} else {
expandableLayout1.expand();
scrollView.scrollTo(0, textID.getBottom());
}
}
});
xml:
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/box_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textColor="#ffffff"
android:textSize="16sp"/>
</LinearLayout>
<net.cachapa.expandablelayout.ExpandableLayout
android:id="#+id/expandable_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:el_duration="1000"
app:el_expanded="false"
app:el_parallax="0.5">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:textColor="#ffffff"
android:textSize="12sp"/>
</net.cachapa.expandablelayout.ExpandableLayout>
</LinearLayout>
The problem you are facing is that the ExpandableLayout doesn't have time to finish opening before ScrollView already ends up scrolling to the bottom of it. So what ends up happening is that the ScrollView Scrolls to the bottom of the ExpandableLayout which hasn't fully opened yet. What you need to do is add a delay between the Layout opening and the ScrollView starting it's scroll function. Here is the code you need, I would try adjusting the milliseconds, you could probably drop them down below 1000 but not by much, still it's up to you to troubleshoot it a bit to make it just a tad bit faster and smoother. Also try using smoothScrollTo instead of scrollTo.
expandableLayout1 = root.findViewById(R.id.expandable_layout1);
button = (LinearLayout)root.findViewById(R.id.box_header);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if (expandableLayout1.isExpanded()) {
expandableLayout1.collapse();
} else {
expandableLayout1.expand();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.scrollTo(0, textID.getBottom());
}
});
}
}, 1000 ); // time in milliseconds
}
}
});
And here is the code that I added in it's raw form, might help you understand it a bit easier.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// do something
}
}, 1000 ); // time in milliseconds

Android : Add a listener to an icon in a text field

i am a beginner in application development and i like to ask a question that i tried my best to get an answer in google but i failed.
So,
in my application (in java) i am using too text fields :
<com.google.android.material.textfield.TextInputLayout
android:layout_marginBottom="20dp"
android:id="#+id/start_date_Layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
app:helperText="Start Date"
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_date"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/start_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="dd/mm/yy"
/>
</com.google.android.material.textfield.TextInputLayout>
I added a listener to my end icon like that :
this.startDateLayout = v.findViewById(R.id.start_date_Layout);
this.startDateLayout.setEndIconOnClickListener(this);
My problem is when i try to get the view that the user clicked :
#Override
public void onClick(View v) {
if (v.getParent() == this.startDateLayout){
Toast.makeText(this.context, "click", Toast.LENGTH_LONG).show();
}
}
the toast never appear and the if condition is never true, my question is how can i get the view on witch the user clicked. thanks for reading i hope i was clear as much as possible.
For me only doing following works:
final TextInputLayout textInputLayout = findViewById(R.id.start_date_Layout);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
}
});
As you're explicitly setting listener on End icon only you shouldn't be worried about verifying viewId.
Just use:
startDateLayout = v.findViewById(R.id.start_date_Layout);
startDateLayout.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO something....
}
});
Simple kotlin:
b.dateLabel.setEndIconOnClickListener {
Toast.makeText(requireContext(), "date", Toast.LENGTH_SHORT).show()
}
First what I can notice in the above code, you're trying to listen to click events on your View
A simple way to do so is change your onClick() method to below
public void onClick(View v) {
if (v.getId() == R.id.start_date_Layout){
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
}
}
I believe I could help
If you are using EditText, then try setting an onFocusChangeListner to the editText instead of onClickListner.
startDateLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// Show your code when has focus
} else {
// when it does not have focus
}
}});
The question is simple, You can you a FragmeLayout or LinearLayout, EditText and an ImageView for achieving the solution, it will be similar to the TextInputEditText but you will have much more control over View Hierarchy, let me show you how you can achieve it, for the sake of simplicity as you are a beginner I'll use linear Layout
Create A FrameLayout or LinearLayout and then create an EditText and an ImageView like
XML:
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#DCDFDF"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
<ImageView
android:layout_gravity="center_vertical"
android:id="#+id/imageViewTitle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="#drawable/android"
tools:ignore="ContentDescription" />
<EditText
android:layout_gravity="center_vertical"
android:id="#+id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#DCDFDF"
android:gravity="top"
android:hint="Start Date"
android:paddingTop="10dp"
android:textAlignment="gravity"
android:importantForAutofill="no" />
</LinearLayout>
Java
public class MainActivity extends AppCompatActivity {
EditText editTextTitle;
ImageView imageViewTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTitle = findViewById(R.id.editTextTitle);
imageViewTitle = findViewById(R.id.imageViewTitle);
imageViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your work here
Toast.makeText(MainActivity.this, "ImageView Pressed", Toast.LENGTH_SHORT).show();
}
});
}
}

Button dissapears when run in emulator

As the title suggests. Button is clearly present and visible in Android Studio. I run the emulator and it dissapears.
I have done numerous searches but cant seem to find a case-specific solution. Could you please take a look and see if you can spot something I cannot.
activity_main.xml:
<Button
android:id="#+id/searchBtn"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="154dp"
android:layout_marginTop="581dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="87dp"
android:background="#drawable/buttons"
android:elevation="15dp"
android:text="SEARCH"
android:visibility="visible" />
MainActivity.Java:
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.searchBtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openListActivity();
}
});
}
public void openListActivity() {
Intent intent = new Intent(this, ListActivity.class);
startActivity(intent);
}
The buttons sole purpose at the moment is to open a second activity.
Is there any other potentially relevant code I have missed?
Your margin is too large for the button to render to an immulator or regular device. As you know, the margin is going to put space between your widget and other widgets on the screen. But spacing of that magnitude will force your image to disappear completely. I would reduce my margin and define a more practical layout as your project grows. That should resolve your issue.

how to change textsize in textView by clicking on button

I'm not good at programming and especially at android, I've made 2 buttons one for making text size big, one for making text size small, and also i made condition that when text size is reached to limit size the button will stop increasing or decreasing text size. it didn't work, when i press btnSmall or btnBig nothing happen, I don't know what is missing here, please i need help... here's what I've done
Java file:
private Button btnSmallTxt, btnBigTxt;
private tvAdminPosts;
int txtSize = 14;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
btnSmallTxt = (Button)findViewById(R.id.btnSmall);
btnBigTxt = (Button)findViewById(R.id.btnBig);
btnSmallTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tvAdminPosts.getTextSize() == 15||tvAdminPosts.getTextSize() ==16||tvAdminPosts.getTextSize() ==17|tvAdminPosts.getTextSize() ==18){
txtSize--;
tvAdminPosts.setTextSize(txtSize);
} else {
txtSize += 0;
tvAdminPosts.setTextSize(txtSize);
}
}
});
btnBigTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tvAdminPosts.getTextSize() == 14 ||tvAdminPosts.getTextSize() == 15 || tvAdminPosts.getTextSize() == 16 || tvAdminPosts.getTextSize() == 17){
txtSize++;
tvAdminPosts.setTextSize(txtSize);
} else if (tvAdminPosts.getTextSize() == 18) {
txtSize+= 0;
tvAdminPosts.setTextSize(txtSize);
} else{
txtSize+= 0;
tvAdminPosts.setTextSize(txtSize);
}
}
});
XML file:
in the xml i made the textView textsize 14 sp
to start with 14 as i write in java but i don't
know if i'm in the right way or what ??
<TextView
android:id="#+id/tvAdminPosts"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.06"
android:textColor="#fff600"
android:maxLines="100"
android:textSize="14sp"
android:scrollbars="vertical"
android:maxLength="1000"
android:textStyle="bold" />
<Button
android:id="#+id/btnBig"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/large_txt"
android:textColor="#ffffff"
android:textSize="7pt"
android:textStyle="bold" />
<Button
android:id="#+id/btnSmall"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/small_txt"
android:textColor="#ffffff"
android:textSize="5pt"
android:textStyle="bold" />
I searched a lot in this but i didn't find anything simple like what I've trying to do
I would be grateful if someone just helped me. Sorry for my bad English
According to the documentation, getTextSize() returns the text size in pixels, not sp as you are defining it.
android:textSize="14sp"
Either change your XML attribute to
android:textSize="14px"
so all your code logic works.
Or find a way to translate px value to sp.
Edit: About your second question, when you use setTextSize() with your increased value, it will set the text size in sp. Again, according to the documentation, the unit type of this method, by default, is "sp" or "scaled pixels".
As you are always checking the text size in px, your condition is invalid and, thus, nothing happens.
The solution is specifying the unit type that you want, being pixels, in your case.
So you have to use:
tvAdminPosts.setTextSize(TypedValue.COMPLEX_UNIT_PX, yourSizeValue);

Numbers increasing when button clicked

I have this Button:
<Button
android:id="#+id/button1"
android:layout_width="130dp"
android:layout_height="35dp"
android:layout_alignRight="#+id/editText2"
android:layout_alignTop="#+id/imageView1"
android:layout_marginTop="23dp"
android:text="Increase" />
How can i make it so that whenever the button is clicked, the count in a textview goes up, for example if the number is first 0, and then you click the button it changes to 1?
Attach a listener to the button to handle the click event.
When you click on it, increment a counter variable and update the text of the textview.
Hint: Use String.valueOf(myCounter); to convert the int to a String
You must add a listener to the Button via the Button's setOnClickListener method, passing in an OnClickListener object.
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt(myTextView.getText().toString());
myTextView.setText(String.valueOf(i + 1));
}
});
where myTextView is the TextView with the counter, obtained with a call to findViewById. Be sure to pass a String to setText, otherwise the method overload with the integer parameter will be called, which has a different meaning (the integer represents a resource id).
it is very simple ! use this code :
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increase"
android:onClick="inc"/>
<TextView
android:id="#+id/text"
..../>
and in your activity :
public void inc(View v){
TextView t = (TextView) findViewById(R.id.text);
t.setText(String.valueOf(Integer.parseInt(t.getText)+1));
}

Categories

Resources