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);
Related
I'm trying to make a custom NumberPicker to display in a DialogFragment. So far I've succeeded in getting the picker to display in a dialog fragment and getting it to display the custom strings I want it to. I've also disabled the descendantFocusability so the text is not editable. Here is an overview of the questions I have about NumberPicker behaviour, I'll go more in depth after:
How does one 'commit' their selection?
How to return the selected value?
How does one 'commit' their selection?
When the dialog appears, I don't see a clear way to 'select' an option (see image below). Looking at native Android selection dialogs, I often see radiobuttons. Is that the way to go? And am I using the wrong UI component to build this?
How to return the selected value?
This question is tightly knit with the last one, as not knowing how to commit a selection obviously doesn't help here. Right now I use NumberPicker.OnValueChangeListener to see if the value changed, however it never fires. Here's how I structured the code:
class PlatePickerFragment: DialogFragment() {
lateinit var listener: NumberPicker.OnValueChangeListener
//I set up the fragment with onCreateDialog here.
}
And this is the code I use when I create an instance:
val platePicker = PlatePickerFragment()
platePicker.listener = NumberPicker.OnValueChangeListener { numberPicker, i1, i2 ->
//set what to do on value change here.
}
However, this block never gets called.
TL;DR: Am I using the right UI component? If I am, how would I implement this in a way that it works? Why does the NumberPicker not have a cancel/ok section by default (see image of DatePicker below)? Thanks in advance!
Answer to first part :
This is the ideal way of implementing NumberPicker. One thing you can do
is add an OK button to side to catch selection.See screenshot
Code for same :
picker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="OK"
android:textSize="15dp" />
<NumberPicker
android:id="#+id/numberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv"
android:layout_marginTop="10dp" />
</RelativeLayout>
</LinearLayout>
In your activity :
final NumberPicker aNumberPicker = (NumberPicker) dialog.findViewById(R.id.numberPicker);
aNumberPicker.setMaxValue(12);
aNumberPicker.setMinValue(1);
aNumberPicker.setValue(1);
aNumberPicker.setFocusable(true);
aNumberPicker.setFocusableInTouchMode(true);
aNumberPicker.setOnScrollListener(new NumberPicker.OnScrollListener() {
#Override
public void onScrollStateChange(NumberPicker view, int scrollState) {
value = view.getValue();
}
});
aNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
value = newVal;
}
});
TextView ok = (TextView) parent.findViewById(R.id.tv);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// value variable can be used here
}
});
Declare value as global variable.
Answer to the second part of your question :
int hour;
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(final NumberPicker numberPicker, final int i, final int i1) {
hour = Integer.valueOf(numberPicker.getDisplayedValues()[numberPicker.getValue()]);
}
});
On clicking of OK button you will have answer in hour variable.
<TableRow
android:id="#+id/Row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/cell31"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
<TextView
android:id="#+id/cell32"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:background="#drawable/back"
android:onClick="cellClick"
android:clickable="true"/>
.... this is 5x5 row column table. with 25 cells in it.
My .xml file looks like this. I have bunch of rows in LinearLayout contains TableLayouts which contains TextViews as cells. I am making a basic game with them.
I am trying to assign each cell from .xml to TextView array in .java file. But i can't figure out how to make it. I need to change color of each cell based on some algorithm i made so i want to access of cells.
private TextView colorBoard[][];
this is my .java array.
I can check if they are clicked with
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
But i only can assign which cell clicked at moment.In my game color of cells change differently. Not the one you just clicked. Assume you clicked 1x3 and 4x4 may change colors.
I'm new to android and this is my first project so sorry if i'm missing something basic.
I think you should use ListView instead of TextView. In ListView you can use an array for assigning those things where-ever you want to use them.
You can create layout file like this:
<ListView>
android:weight="match_parent"
android:height="match_parent"
android:id="+id/lst_view"
</ListView>
And Your Java Code looks like this
String[] array={"stack","queue","arraylist"};
ListView lst;
//In onCreate function
lst=(ListView)findViewById(R.id.lst_view);
ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,R.layout.what_ever_your_xml,array);
lst.setAdapter(adpt);
//function of listview
//arrayadapter is used for converting string type array into listview
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s;
s=((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
});
If you want to assign values to TextView Array.
Then do it like this in oncreate method after setContentView
colorBoard[0][0] = (TextView) findViewById(R.id.cell11);
colorBoard[0][1] = (TextView) findViewById(R.id.cell12);
You can use findviewbyId to get the TextView from xml to Java.
Suppose if you want to access cell44 when you tap on cell11, then you could do something like this.
public void cellClick(View v) {
TextView cell = (TextView) findViewById(v.getId());
switch (cell.getId()) {
case R.id.cell11:
xloc = 0;
colorBoard[0][0].setBackground()//As you wish
break;
case R.id.cell12:
xloc = 1;
break ;
...rest of cells
Once you have access to cell44 you can set the background color or any other properties to it.
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);
}
});
I have two different buttons in my activity, and an if statement to show them...
My if statement is working well and show the right button, but one of those buttons is not doing anything when I click on it! (Actually, it's not clickable at all!)
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("LOG", "Clicked!"); // Even this log won't work!
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutImportant.setVisibility(View.VISIBLE); // This line is working
}
if (!isNeeded)
{
// buttonNormal is working well!
buttonNormal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutNormal.setVisibility(View.VISIBLE); // And this one is not working too!
}
The problem is, it won't return any error or log, to know what's the problem! It's log I disabled the button... (which I didn't)
I checked if it's for declarations, but everything is declared well...
I checked the XML, there's no problem there either:
<LinearLayout
android:id="#+id/linearLayoutImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/textViewImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="#string/important"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/buttonImportant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/details" />
</LinearLayout>
...
...
<LinearLayout
android:id="#+id/linearLayoutNormal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end"
android:background="#android:drawable/ic_menu_close_clear_cancel" />
<TextView
android:id="#+id/textViewNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="#string/normal"
android:textStyle="bold" />
<Button
android:id="#+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:text="#string/details" />
</LinearLayout>
EDIT:
Here's the declarations:
LinearLayout linearLayoutImportant, linearLayoutNormal;
Button buttonImportant, buttonClose, buttonNormal;
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutImportant = (LinearLayout) findViewById(R.id.linearLayoutImportant );
buttonImportant = (Button) findViewById(R.id.buttonImportant);
buttonClose = (Button) findViewById(R.id.buttonClose);
buttonClose .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
}
});
buttonNormal = (Button) findViewById(R.id.buttonNormal);
linearLayoutNormal = (LinearLayout) findViewById(R.id.linearLayoutNormal);
}
EDIT 2:
Alright guys, maybe I didn't made it clear...
That isNeeded is a variable and I control it from another place...
When I make it false, the second if will get executed very well, and when I make it true, the first if will execute too, but only the button is not clickable!
EDIT 3:
I also checked .setEnabled(true); or .setClickable(true);, still not working... :/
SOLVED:
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
Try without the if statement in order to know if is a validation problem and you're only setting one listener
The boolean variable isNeeded can take only one of the two values, either true or false. Therefore only one of the if conditions will be executed.
Since only one of the condition is true, you are only setting an onClickListener to one of the buttons and thus the other button won't be clickable and the log message within the onClick method won't be visible in the logs.
Hope this answered your question.
All the best :)
set isNeeded = true before
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
then test it again.
Good lucky!
There was an error posting my ans so i have taken a screenshot of it. Please review it for your answer
Trying to get the id of the radio button that is checked in android, this is my XML code,
<RadioButton
android:id="#+id/RadioAuction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:onClick="showAuctionOptions"
android:textColor="#3DCC00"
android:text="#string/RadioButton1" />
<RadioButton
android:id="#+id/RadioBin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#FF0000"
android:text="Buy it now" />
so once it has been clicked it runs showAuctionOptions, this is my java code,
public void showAuctionOptions(View v){
if(findViewById(R.id.v=="RadioAuction")){
//Display start price
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
}
However this doesn't work, does anyone know why? Thanks.
Try
if (v == findViewById(R.id.RadioAuction)){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
If this doesn't cut it either, make sure you've set the on click listeners for those radio buttons.
Probably faster would be:
if (v.getId() == R.id.RadioAuction){
LinearLayout ShowPrice = (LinearLayout)findViewById(R.id.LayoutStartPrice);
ShowPrice.setVisibility(View.VISIBLE);
//Display reserve price
LinearLayout ShowReservePrice = (LinearLayout)findViewById(R.id.LayoutReservePrice);
ShowReservePrice.setVisibility(View.VISIBLE);
}
And that way you can use the R values in a switch statement, if you have a long radio list:
switch (v.getId()) {
case R.id.RadioAuction:
// Do stuff
...
}