On click of a button , a check mark icon should be displayed on the leftmost corner of the button, when reclicked on the same button , the check mark icon should disppear. Could some on help me out in this case?
While this is already answered, here's an alternative solution: add a unicode checkmark symbol. There are two of them: \u2713 and \u2714. Just add them to your strings:
<string name="button_label_on">\u2713 on</string>
<string name="button_label_off">off</string>
Of course, you can put this directly into your layout code, too:
<Button
...
android:text="\u2713 on"
/>
You can add an ImageView (lets say tick.png) with visibility Gone, at the left of the Button. And set its visibilty. Here is the code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/iv_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="#drawable/tick"/>
<Button
android:id="#+id/btn_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"/>
</LinearLayout>
Now, on Button click event you set its visibilty:
Button btn_tick = (Button)findViewById(R.id.btn_tick);
btn_tick.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ImageView iv_tick = (ImageView)findViewById(R.id.iv_tick);
int visibility = iv_tick.getVisibility();
if(visibility == View.VISIBLE)
{
iv_tick.setVisibility(View.GONE);
}
else
{
iv_tick.setVisibility(View.VISIBLE);
}
}
});
Checkout the CheckBox widget.
Related
I am creating a set of radio options where the radio buttons are removed, and I would like to know which option is selected.
Here is the code
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="#+id/languages"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<RadioButton
android:id="#+id/english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:text="#string/langtext_english"
android:textSize="16dp"
android:checked="true"
android:button="#null" />
<RadioButton
android:id="#+id/bahasaIndonesia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/langtext_indonesian"
android:textSize="16dp"
android:button="hide"/>
</RadioGroup>
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button. How to do this?
When the option is touched or clicked, I would like to show a Toast.
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
langChosen = (RadioGroup) rootView.findViewById(R.id.languages);
langChosen.setOnClickListener(new RadioGroup.OnClickListener() {
#Override
public void onClick(View v) {
Toast abc = new Toast(getContext());
abc.makeText(getContext(), v.toString(),Toast.LENGTH_LONG);
}
You mentioned
I don't want to show the radio button, but I would like to know which option is selected. onCheckedChangedListener doesn't work as there is no button.
I would not say that is true. The RadioGroup still registers changes of the checked radio button. The android:button attribute of RadioButtonjust represents the button you see next to the text/content of the radio button. You still can register to click events of the radio button element itself.
Could it be that you just forgot to call show() on your toast, so you think it's not working?
I supply a tested solution for you here. Let me know if it works for you too.
RadioGroup group = (RadioGroup)this.findViewById(R.id.languages);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton b = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(MainActivity.this, b.getText(), Toast.LENGTH_SHORT).show();
}
});
Note that MainActivity.this must be replaced with your corresponding context.
I would try the OnClick method with the two buttons, then the method is called, use the ischecked() method to detremine which of the buttons is checked.
I am new in android programming. I have made an app which is fill in the blanks type app. Until the answer-confirm button is clicked, the next and the previous Button should be disabled. If it is clicked and answer is checked, then next and previous Button to get enabled. please help!!!!!!!!
If you want to disable a button in xml use this code
<Button
android:text="Next"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
for enabling the button when we click on previous then in onClick function(previous) add this code
next.setVisibility(View.VISIBLE);
next is the button
something like that (maybe its not the best way, you can play with it and make it better)
protected void onCreate(Bundle savedInstanceState) {
...
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enableNextButton();
}
});
...
}
private void enableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_active);
nextButton.setClickable(true);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goNext();
}
});
}
private void disableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_inactive);
nextButton.setClickable(false);
}
and in your xml the buttons should be something like
<Button
android:id="#+id/first_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_active"
android:layout_margin="#dimen/generic_margin"
android:text="#string/first_button_text"
android:textColor="#color/white"/>
<Button
android:id="#+id/next_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_inactive"
android:clickable="false"
android:layout_margin="#dimen/generic_margin"
android:text="#string/next_button_text"
android:textColor="#color/white"/>
this way you start with an active button and an inactive button, when the first is pressed you can "activate" the next button
Please use punctuations and uppercases but anyway.
You can check here for more infos about buttons : http://developer.android.com/reference/android/widget/Button.html
Otherwise, the method setVisible() allow you to make visibile or not a button of your layout. Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
EDIT
Oh sorry ! So you can use setEnabled().
In every activity in my app, the users are able to press two button at the same time.
How can I prevent they to do this?
you can disable the multi touch in the screen...which can allow only
single touch by
place this line button viewGroup layout
android:splitMotionEvents="false"
<LinearLayout
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:splitMotionEvents="false" >
<Button
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbars="none" >
</Button>
<Button
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbars="none" >
</Button>
</LinearLayout>
You can try like this :
findViewById(R.id.buttonX).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewGroup group = (ViewGroup)findViewById(R.id.container);
for (View touchable : group.getTouchables()) {
if (touchable != view && touchable.isPressed()) {
Log.d("...", "skip");
}
}
}
});
Disable all other buttons on click listener of all buttons
On First button click, you can do this for particular seconds by running one thread
second_btn.setEnabled(false); or second_btn.setClickable(false);
If the buttons are one near other you can make a radiogroup that do your job without any work:D. If not you can use a synk boolean variable and play witt all the cases.
A radio group should do the magic. As the user will be able to select any one option(i mean the button at a time).
You could try something like this for every single button:
boolean buttonClicked = false;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(!buttonClicked) {
buttonClicked = true;
...
...
...
buttonClicked = false;
}
}
});
To disable press two button at the same time multi-touch on your app by using this approach in your theme - it works fine!
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:splitMotionEvents">false</item>
<item name="android:windowEnableSplitTouch">false</item>
</style>
I have a TextView with an onClickListener().
When I click on the TextView it blinks.
How to disable this effect?
Here is the XML
<TextView
android:id="#+id/descriptionText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:ellipsize="end"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:maxLines="3"
/>
I tried to remove android:ellipsize and android:maxLines tags - no effect.
And here is the code:
accountDescriptionTextView = (TextView) v.findViewById(R.id.descriptionText);
accountDescriptionTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (descriptionOpened)
{
// accountDescriptionTextView.setEllipsize(TextUtils.TruncateAt.END);
// accountDescriptionTextView.setMaxLines(3);
descriptionOpened = false;
}
else
{
// accountDescriptionTextView.setEllipsize(null);
// accountDescriptionTextView.setMaxLines(100);
descriptionOpened = true;
}
}
});
I need to have the commented functionality, but even when this lines are commented I still see how the textview blinks.
The text just disapears when i place my finger on the screen and apears when I take the finger away.
Android uses a selector to give different colors to widgets' pressed state.
If you don't want that behavior, you can use solid colors for android:textColor and android:textColorHighlight.
Check the TextView doc.
I am creating a user form in android. I want to display an edit text box on click of a button. below that button, while simultaneously the contents originally present below that button to move more down.
How can this be done?
If you just want to "display an edit text box on click of a button" why don't you just..
Keep the EditText in your XML layout file for that activity below the Button where you want it..
XML set it's
android:visibility = "gone"
and making instance of that
EditText et=(EditText)findViewById(R.id.thatEditText);
in activity...in your button click event set
et.setVisibility(View.VISIBLE);
Define the view in your layout, then in code, show and hide it with
myView.setVisibility(View.VISIBLE) and myView.setVisibility(View.GONE).
//xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
<EditText
android:id="#+id/edtbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//Activity
//oncreate
editText.setVisibility(View.GONE);
btn..setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setVisibility(View.VISIBLE);
}
});
If your layout is relative then addView(yourView, index) doesn't work. Suppose you want to add view after some other control and reference to that control.
e.g.
<RelativeLayout android:id="#+id/templayout">
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
and you want to add edit text control after text View then on button click :
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.templayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.title);
EditText yourEditText = new EditText(this);
relativeLayout.addView(yourEditText, params);
Define your EditText in your xml and hide it. On button click, change its visibility to View.Visible.
YourEditText.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
YourEditText.setVisibility(View.VISIBLE);
}
});