When user clicks a button weird text appears in inputbox - java

Here is my xml code for a sample button.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="25dp"
android:layout_weight="0.166"
android:text="1"
android:onClick="ProcessInput" />
Now in mainActivity.java i wrote this code
public void ProcessInput(View v)
{
Button btn = (Button) v;
inputText.append(btn.toString());
}
The output is shown in the screenshot

You simply convert to string not getting value of button so change from
inputText.append(btn.toString());
to
inputText.append(btn.getText().toString());

You need to use .getText() to get button text. btn.toString() will return id of btn.
Try this
inputText.append(btn.getText().toString());

Related

Alternating editText text color onButtonClick

I am currently trying to create a highlighter function for my app where on a button being clicked by the user the program checks to see if the editText TextColor is black, default, or Yellow, highlighted, then switches between the two.
I am using an onClickListener if Statement:
hB0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getTextColors().equals(Color.BLACK)){
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
}
});
I have set the textColor of the EditText to Color.Black in the XML file and I have looked for similar problems with little to show for it! Thank you for your time.
Edit: The problem is when I press the button the text stays the same colour and doesn't switch between the two.
Edit2: To take it further, when I comment out the if statement and make the button simply change the text colour to Color.YELLOW it works perfectly. I just want to be able to switch the colour BACK to black.
Edit 3: The XML Code for the highlighter button and the textView I want to change the text color in.
<Button
android:id="#+id/highLighter0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:minWidth="4dp"
android:minHeight="10dp"
android:text="H"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="#+id/slot0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#+id/slot0"
app:layout_constraintTop_toTopOf="#+id/slot0" />
<EditText
android:id="#+id/slot0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="center"
android:hint="6 AM"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine"
android:lines="4"
android:maxLines="4"
android:minLines="1"
android:rotationX="0"
android:textColor="#color/black"
app:layout_constraintEnd_toStartOf="#+id/highLighter0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoppingListButton"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="370dp" />
EditText #id=slot0 is ed1 in the java code.
You need to change your code with below code:
if (ed1.getCurrentTextColor() == Color.BLACK) {
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}

How to create a clickable Edittext?

I know a there is a lot of similar questions out there, but none of them work for me. I want to make a EditText that acts like a Button. I want it to be not editable (and no keyboard should popup). However, when I set an onClickListener(), the EditText doesn't react to my onClick() event. How should I do this correctly?
In my xml I disabled the focusableInTouchMode and I set the clickable to be true. My resulting EditText is not editable, but it doesn't response to my onClickListener().
My xml of the EditText:
<EditText
android:id="#+id/taskviewer_date"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textColorHint="#color/colorShadow"
android:inputType="none"
android:hint="No Due Date"/>
My Code:
EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("taskviewer", "OnClickListener called !");
}
}
);
Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. Make FrameLayout's visibility Gone when you want your edittext to be editable
Use of EditText's setOnClickListener will work if you set setTextIsSelectable to true.
In the XML:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
In the appropriate fragment or the activity:
mTextInpuEditText.setEnabled(true);
mTextInpuEditText.setTextIsSelectable(true);
mTextInpuEditText.setFocusable(false);
mTextInpuEditText.setFocusableInTouchMode(false);
mTextInpuEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Edit Text is clicked");
}
});

Android - How to check for button click in a custom alert dialog box?

I've got two buttons: Share, Continue. Both of them are created in a new XML file because I wanted to keep my application with a nice flat Windows 8/10 looking GUI.
I am able to display the dialog message, but the problem I am facing is, how can I check which button was clicked by the user: Share or Continue? I can't set up the onClickListener for them because this alert dialog has been created in a new file, thus, it crashes the app if I try to do so.
Here's the XML code for the buttons:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/share_button"
android:layout_marginRight="5dp"
android:text="SHARE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="#drawable/button_blue" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/continue_button"
android:layout_marginLeft="5dp"
android:text="CONTINUE"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:background="#drawable/button_green" />
And the java code where I display this as an alert dialog:
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
d.show();
You can call it by your dialog d i.e :
Dialog d = new Dialog(MainActivity.this);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.dialog);
Button button = (Button) d.findViewById(R.id.share_button);
Button button2 = (Button) d.findViewById(R.id.continue_button)
d.show();
And then you can make a normal onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//STUFF
}
});
Hope it helps ;)

How do I use a custom icon at a PinItButton of their SDK?

I want to use a custom icon for my pinIt button. I keep getting the old layout overwritten when I run my app while in the preview the icon that I prefer is shown.
<com.pinterest.pinit.PinItButton
android:id="#+id/car_info_comment_pintrest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:background="#drawable/ic_comment_pintrest"
android:drawableTop="#drawable/ic_comment_pintrest"
android:text="100"
android:textColor="#fff" />
I've fixed it with a hacky solution. I've added another button and called the OnClickEvent of the PinIt button and made that buttons visibility: gone.
holder.mPinterestButton = ((PinItButton) holder.content.findViewById(R.id.pin_it));
holder.mPinterestButton.setImageUrl("http://placekitten.com/400/300");
holder.mPinterestButton.setUrl("http://placekitten.com"); // optional
holder.mPinterestButton.setDescription("A place kitten!"); // optional
holder.pinterestButton = ((Button) holder.content.findViewById(R.id.car_info_comment_pinterest));
holder.pinterestButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.mPinterestButton.performClick();
}
});
XML:
<Button
android:id="#+id/car_info_comment_pinterest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_comment_pinterest"
android:background="#null"
android:text="100"
android:textColor="#fff" />
<com.pinterest.pinit.PinItButton
android:id="#+id/pin_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>

Android, Button don't working at all

This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.

Categories