I am completely new to android development, my problem is that i had two editText boxes in my layout and self created number button 0-9,enterButton and Clr button. now my problem is to get two inputs from user via these number button in two diffrent editText boxes. Help me out!!!
Here is the code`#Override
public void onClick(View view) {
// button clicked
if (view.getId() == R.id.buttonEnter) {
// enter button
}
} else if (view.getId() == R.id.buttonClr) {
// clear button
} else {
// number button
response.setVisibility(View.INVISIBLE);
// here i want to take two inputs by clicking two buttons and display them
int entered1 = Integer.parseInt(view.getTag().toString());
editTxt1.setText(String.valueOf(entered1));
int entered2 = Integer.parseInt(view.getTag().toString());
editTxt2.setText(String.valueOf(entered2));
}
}`
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:tag="1"
android:text="1"
android:textSize="30sp"
android:textStyle="bold" />`
Ok i get what you are trying to do, but why are you retrieving theTag, which appears to be a String from the view, then parse it to int, just to make it a string again?
besides that, id like to see the Button initialization, so i can understand what exactly the tag is.
Related
Click on the button event.
public void onClick(View view) {
Button result = view.findViewById(view.getId());
textNo[R.id.t1].setText("2");
Toast.makeText(getActivity(),"클릭 : " + result.getText().toString(),Toast.LENGTH_SHORT).show();
}
For example, is there a way to change the value of the first text view by clicking the first button?
There are countless buttons, so I gave resource id to each button and text
Now, for example, only 5 buttons and text views are given.
for (i = 0; i < 5; i++) {
String textId = "t" + (i + 1);
String buttonId = "b" + (i + 1);
textNo[i] = view.findViewById(getResources().getIdentifier(textId, "id", getActivity().getPackageName()));
buttonNo[i] = view.findViewById(getResources().getIdentifier(buttonId, "id", getActivity().getPackageName()));
When I press the button on the same line, I want to increase the number of text views located on it, but I kept trying, but I couldn't figure out how to do it in the fragment.
enter image description here
The outline of the XML code is as follows.
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/zero" />
<Button
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:layout_weight="1"
android:text="#string/Click"
style="?android:attr/buttonBarButtonStyle" />
It's my first time asking this question, so I'm very inexperienced, but if you tell me, I'll try harder.
I'm sorry that I'm not good at English.
b1.setOnClickListener {
t1.text = "tttt"
}
If u have only a few buttons, you can write them in xml. For more, use horizontal RecyclerView.
You should use RecyclerView in this case. Your method isn'n correct
Is there an option to make the MaterialButtonToggleGroup have a required selected button when using app:singleSelection="true"?
When clicking to a different button works fine (all other buttons are deselected), but when you click the same (already selected) button it deselects it itself and I want to remain selected.
My example:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap"
android:layout_height="wrap_content"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="#+id/filterA"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/filterB"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/filterC"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
You can define it in the layout using the app:selectionRequired attribute:
<com.google.android.material.button.MaterialButtonToggleGroup
app:selectionRequired="true"
app:checkedButton="#id/..."
app:singleSelection="true">
You can also use the method setSelectionRequired:
buttonGroup.setSelectionRequired(true);
Note: This requires a minimum of version 1.2.0-alpha03
I got this working with the following:
toggle_group.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (group.checkedButtonId == -1) group.check(checkedId)
}
If you have singleSelection enabled, the conditional will only evaluate to true when the user has clicked on the button which is already checked, making it so no button is checked. When this happens, we just need to check the button they unchecked.
I also came across this issue and I found this is working with the app:singleSelection="true"
String selectedValue = "Male";
genderBtnToggle.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
#Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
MaterialButton btn = genderBtnToggle.findViewById(checkedId);
if (!isChecked && btn.getText().toString().equals(selectedValue)) {
genderBtnToggle.check(checkedId);
}
if (isChecked) {
selectedValue = btn.getText().toString();
}
}
});
I also came across this issue and will be waiting for a permanent fix from google. In the meantime, I did the following to make sure that at least one button is checked.
final MaterialButtonToggleGroup tGroup = view.findViewById(R.id.toggleGroup);
final MaterialButton breast = tGroup.findViewById(R.id.breast);
final MaterialButton bottle = tGroup.findViewById(R.id.bottle);
final MaterialButton solids = tGroup.findViewById(R.id.solids);
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
tGroup.check(v.getId());
}
};
breast.setOnClickListener(onClickListener);
bottle.setOnClickListener(onClickListener);
solids.setOnClickListener(onClickListener);
Hope this helps.
I need to set prompt for my TextView, if String is too long to fit inside TV (i set max length 20) then it displays only part of it with "..." at the end. I want to display prompt with full String when i click on TV. Is it possible? And if yes then how to do it?
Inside activity:
textCompany.setText(testDb.getCompanyName(id));
textCompany.setEllipsize(null);
And XML:
<TextView
android:id="#+id/textCompany"
android:layout_width="match_parent"
android:layout_below="#+id/textId"
android:layout_height="wrap_content"
android:maxLength="20"
android:ellipsize="end"
android:gravity="end"
android:layout_marginEnd="20dp"
android:textSize="17sp"
android:text="verylongstringjusttotestifthisworksandletshopeitwill" />
You can do that with a simple onClickListener. First, you check what the length of the text is, then if it's more than 20 characters, you take the first 20 and add three dots to the end and display that. At the same time, you save the full text to a temporary variable, and you display it when someone clicks your TextView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView textView = findViewById(R.id.textCompany);
String shortMessage;
final String message = textView.getText().toString();
if(message.length() >= 20){
shortMessage = message.substring(0,19)+"...";
textView.setText(shortMessage);
}
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
Note: make sure you make your textView clickable by adding the following to your xml:
android:clickable="true"
The behavior you mentioned is due to
android:ellipsize="end"
It displays the end of text with "Some text..." if it does not fit in width.
You can programmatically change the ellipsize value to none to display the full text on Click.
myTextView.setEllipsize(null);
I have a text view in which the user can enter data at run time using the custom buttons that I have created.
My delete button is able to delete one character at a time but if i hold the button then it stops.
I want the text field to get cleared when i hold the button.
Is there any solution to this....??
Please help me out.
This is my xml,
<RelativeLayout
android:id="#+id/btnClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imgClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img_clear" />
</RelativeLayout>
This is my code,
imgClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getNumber;
if(isFirstNum){
getNumber = txtFirstNumber.getText().toString();
if(getNumber.length() > 0)
txtFirstNumber.setText(getNumber.substring(0, getNumber.length()-1));
} else if(!isFirstNum){
getNumber = txtSecondNumber.getText().toString();
if(getNumber.length() > 0)
txtSecondNumber.setText(getNumber.substring(0, getNumber.length()-1));
}
}
});
You can use the onLongClickListener to check if the delete button is pressed for a long time
imgClear.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
txtFirstNumber.setText("");
return true;
}
});
This will set your txtFirstNumber to blank but the onClickListener will not be called.
Your question is not clear. But as per my understanding you want to clear the text of the text view then in your button click listener just set the text for textview to empty.
eg:
public void onClick(View v){
textview.setText("");
}
I have created an Activity that uses the ViewFlipper to Switch between Different elements. Each element represents an Item in a store. I would like to add a "Buy" Button to each View. I am however not sure how to do this, since all the views use the default layout i have created. I have added the information like the Price of the Item etc Programmatically. So i am uncertain how to add a listener to the button, since they will all refer to the same button in the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/credit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="220dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="104dp" />
<TextView
android:id="#+id/credit_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/credit_button"
android:layout_alignTop="#+id/credit_button"
android:layout_marginRight="22dp"
android:layout_marginTop="21dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="normal" />
<TextView
android:id="#+id/credit_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/credit_button"
android:layout_alignRight="#+id/credit_type"
android:layout_centerVertical="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
As you can see, the Button id is "Credit Button". So now to be able to differentiate between the different store items' buttons what would i have to do?
Note, i am adding store items dynamically as well, so i cant simply create all the views separately using xml.
OK HERE IS THE UPDATED ANSWER. I used everyones responses below to fix teh issue. So Thank you all :)
// PerkView
View PerkView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(PerkView);
Button perkButton = (Button)
PerkView.findViewById(R.id.StoreCatItem);
// TitleView
View TitleView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(TitleView);
Button titleButton = (Button)
TitleView.findViewById(R.id.StoreCatItem);
// ProfileView
View ProfileView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(ProfileView);
Button profileButton = (Button)
ProfileView.findViewById(R.id.StoreCatItem);
I simply Created Multiple Views Programmatically, and then retrieved the buttons from those views afterwards. I then added the listeners to the buttons as follows:
perkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Perks.class);
i.putExtra("player", player);
startActivity(i);
}
});
titleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Titles.class);
i.putExtra("player", player);
startActivity(i);
}
});
Thanks A lot :)
There are two ways you can handle the button click distinctly,
At the time of inflating layout for each store, add the button listener with onClick() method there only.
Or you can assign some uniqueId or flag to each store and then handle button click of store by this uniqueID.
Usually you solve this by following stack:
fragment -has-> list -has-> adapter -has-> list of items
Normally if you present one item per screen and you want to swipe between you should use ViewPager with FragmentStatePagerAdapter (allows removing items from ViewPager if needed).
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
For more items in one screen use ListView with BaseAdapter as it allows better control over item view and ListView will recycle views as you scroll or fling.
http://developer.android.com/guide/topics/ui/layout/listview.html
Set unique tag for each store button, in code you can differentiate with respect to tags.Tag is just piece of information you want set for any view, u can use it.Implement on click listener in your activity and then set that to all buttons, so all buttons click run through same code where you can easily diffrentiate between ur store buttons with respect to tags.
like below how you can set and get tag
// for setting tag
Button button = (Button) findViewById(R.id.button1);
button.setTag("unique_tag");
// get tag and then differentiate with the unique_tg
button.getTag();
You can easily create your button programmatically like so :
Button b = new Button(this); // where this = your context
b.setText("Buy");
// b."other attribute" = "other value";
b.setTag("Awesome blue shirt");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button buy = (Button) v;
String article = b.getTag();
// do some stuff
}
});
container.addView(b);
You can identify the buy button using its tag, as seen here.