Change Button color when pressed - java

I've found other answers to this but they where 2 and 6 years old so I didn't know if that was the best option.
I'm making an app that converts a simple integer in hex, oct and binary.
The user can choose in which of the three options the integer must be converted to by three buttons located at the top of the Fragment.
The effect that I'm searching for is that if the user presses one of the three Buttons, the one that has been clicked remains of a certain color.
This is my XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingRight="16dp"
android:paddingLeft="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp">
<Button
android:id="#+id/hex_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="#string/hex_button"/>
<Button
android:id="#+id/binary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/hex_button"
android:text="#string/binary_button"/>
<Button
android:id="#+id/oct_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/binary_button"
android:text="#string/oct_button"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/converted_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:inputType="number"
android:text="#string/convert_button"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<EditText
android:id="#+id/int_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/integer_edit_text_hint"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/converted_number_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
Do I have to make changes in the Java code or just in the XML to achieve this?

You will need to change color of button widget for onClick() method

You can use two background drawables : one for active and other for inactive.
Add ClickListeners on all the buttons and toggle the backgrounds using setBackgroundResource(); method.

You will have to set an onClickListener for the buttons. In this listener, you check which button invoked the event that triggered the listener, and you change the color of this button there.
So you will have to do it in the java code. A listener basically looks like this:
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Change Button Color here, and do other stuff
Button button = (Button) findViewById(v.getId());
button.setBackgroundColor(int color);
}
});

This use in xml activity like for button :-
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/binary_button"
android:text="#string/oct_button"
android:onClick="changeColor"
/>
Now in java class use like....
private boolean isChangeColor=true;
public void changeColor(View v)
{
if(isChangeColor)
v.setBackgroundColor(Color.BLACK);
else
v.setBackgroundColor(Color.WHITE);//use your original color here
isChangeColor=!isChangeColor;
}

Related

AndroidStudio EditText can't be edited (cursor disappear)

I'm having a weird problem i've never encountered.
I'm building a layout for an activity. My only issue is, i can't edit the edittexts in any way. I've tried both with the emulator and the real device. To be more specific, when i click on the edittext, the cursor appears for a very short moment, then disappears. The keyboard shows up but get stuck and doesn't work. In the java code only enable / disable the the editext :
#Override
public void onClick(View v) {
if(bottlename.isEnabled()){
//bottlenumber.setEnabled(false);
ArrayList<Bottle> bottle = BottleView.getBottlelist();
bottle.set(Integer.parseInt(bottlenumber.getText().toString())-1, new Bottle(bottlename.getText().toString(),Integer.parseInt(bottlenumber.getText().toString()), R.drawable.vodka));
bottlename.setEnabled(false);
}else{
bottlename.setEnabled(true);
//bottlenumber.setEnabled(true);
}
}
});
Any idea what could be the issue?
I've already tried many solutions from other posts, with no luck.
Attaching the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/bottleimage"
android:layout_width="150dp"
android:layout_height="160dp" />
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/bottlename"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:text="Bottle"
android:textAlignment="center"
android:inputType="text"
android:textSize="36sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bottlenumber"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:inputType="number"
android:text="X"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<ImageButton
android:id="#+id/editbottle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="center"
android:src="#android:drawable/ic_menu_edit" />
</LinearLayout>
</RelativeLayout>
Is all your EditText fields disabled (android:enabled="false") in your layout intentionally?
the behavior it's a bit weird, I suggest to move the action of reading the value of the editText to a button, so you will have to change the text of the button like this :
When EditTexts enabled ==> "Send"
When EditTexts disabled ==> "Enable"

How to Multichoice Options That Appears When Clicking Single Chocie in Android

I want something like this. There could be passive multichoice buttons below all of the single choices. It doesn't matter. Is that possible?
Single and Multiple Choice Dialog
You can control visibility of every view with view.setVisibility(View.Visible or View.Gone). Set click listener on single choice button and use that method to show container layout with radio buttons.
chkIos.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (v.isChecked()) {
multiChoiceView.setVisibility(View.VISIBLE);
} else {
multiChoiceView.setVisibility(View.GONE);
}
});
To build a layout like on your picture you can combine RadioGroup with LinearLayout under each RadioButton, in this way:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:orientation="vertical">
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>

Creating nested Linear layout dynamically with weights to have multiple buttons on same line

I'm trying to create a toggle button that will display more buttons and some TextView like shown below.
With the toggle button ON it will display the TextViews and 3 extra buttons, once switched off it will close those, Rearranging the toggle buttons below it as shown.
The part I specifically need help with is creating the second linear layout and setting weightings for each of my buttons?
Public void toggleswtich(){
layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
Button enterJob = new Button(Reports.this);
layout.setLayoutParams();
// if (button.isActivated()) {
enterJob.setText("Open");
layout.setWeightSum(1);
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
// }
// else {
enterJob.setText("Clsoe");
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
//}
}
As requested this is my xml, however there isnt much to show as everything is created with code in run time once a Job is saved.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">
<TextView
android:textSize="40dp"
android:layout_width="wrap_content"
android:text="Username"
android:layout_height="wrap_content" />
<EditText
android:textSize="40dp"
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:nestedScrollingEnabled="false">
<LinearLayout
android:id="#+id/ReportsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:textSize="40dp"
android:id="#+id/bEmptyJN"
android:text="Enter Job number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<requestFocus
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here's what it looks like with jobs, picture the jobs as toggle buttons
Let me know if you'd like anymore information

XML or JAVA code for switching elements in ANDROID

Very new to programming here. I am trying to program an activity for Android in .xml and .java and I'm trying to figure out if it is possible to switch one element for another by pressing a button.
The screen I am working on will have an option to search through a list of items and the option to add items to this list. I would like the search bar and any adding options to be hidden by default, with the option to show either of them by pressing the according button. Press the search options button and the search bar should appear, press the adding options button and the search bar should be hidden and in its place should show adding options instead.
Is this possible in .xml or .java and if so what kind of elements or methods should I be looking into to achieve this? Below you will find the code that I have so far in .xml, where the editText1 should be the search bar and which should be replaced with adding options if imageButton2 is pressed.
Many thanks in advance.
<?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:background="#drawable/bgmenu"
android:orientation="vertical" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:scaleType="fitStart"
android:src="#drawable/bproductsbanner"
android:paddingStart="5dp"
android:paddingTop="15dp"
android:id="#+id/ibProductsBanner"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bgminibutton" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:background="#00000000"
android:src="#drawable/bssearch" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="#+id/imageButton1"
android:background="#00000000"
android:src="#drawable/bsadd" />
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Search.." />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Product List"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
android:gravity="center"/>
</LinearLayout>
Suppose you have defined the search bar in your xml as an EditText
<EditText android:id="#+id/search" ...
Then in your activity (java) you get a reference to it by doing
View search = findViewById(R.id.search);
and then when clicking the respective button you can play with the visibility attribute
search.setVisibility(View.VISIBLE); // View.GONE
You have to set the visibility as gone in your xml to your search bar:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Search.."
android:visibility="gone"/>
And then you have to change visibility when you click the button:
ImageButton ib = (ImageButton) findViewById(R.id.imageButton1);
final EditText search = (EditText) findViewById(R.id.editText1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search.setVisibility(View.VISIBLE);
}
});
P.s. I'm supposing that your search bar is the EditText with id editText1 and your option button is the ImageButton with id imageButton1

Android/Java: EditText focus moves whole layout, and only ScrollView should be

This phase is supposed to take all basic information and add so-called "Generals", as many as the user wants, but when I click on en "Enter your phone number" EditText, or any of the generals' EditTexts, and the virtual keyboard appears, the whole layout gets pulled up so that the focused EditText can be visible.
Problem is that I need to have the top ribbon containing "Confirm" button available at all occasions. I'm aware of the fact the user can fix that by pressing the Back button, but that'd be extremely unpractical. I need to know how to keep the ribbon up there after everything else gets pulled up by the mentioned EditTexts.
Screenshot of the layout before mentioned EditTexts are tapped:
https://www.mediafire.com/convkey/2da9/4pa1balkda4y4d46g.jpg
Screenshot of the layout after mentioned EditTexts are tapped:
https://www.mediafire.com/convkey/3f9e/so8qq8vud6m3h996g.jpg
Here is layout's XML. "topRibbonLL" LinearLayout should always keep its place at the top, while everything else should be pulled up by tapping the mentioned EditTexts.
<?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" >
<LinearLayout
android:id="#+id/topRibbonLL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/firstDataTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="Step 3: First data"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Confirm"
android:onClick="newFileStep3ConfirmPressed" />
</LinearLayout>
<CheckBox
android:id="#+id/shareableCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other people can share my file" />
<TextView
android:id="#+id/shareableDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:text="Description of the shareableCheckBox effect."
android:textAppearance="?android:attr/textAppearanceSmall" />
<ScrollView
android:id="#+id/dataScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/dataFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" >
</EditText>
<EditText
android:id="#+id/dataFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:focusable="true"
android:focusableInTouchMode="true" />
<EditText
android:id="#+id/dataAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress"
android:focusable="true"
android:focusableInTouchMode="true" />
<EditText
android:id="#+id/dataPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Generals:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/generalsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/newGeneralButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add new general"
android:onClick="newGeneralButtonPressed" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Necessary EditTexts are re-set-up after the layout is opened, so that they'd have grey text etc.
private void setupNewCardEditText(final EditText editText, final String text) {
editText.setTextColor(R.integer.tempTextColor);
editText.setText(text);
editText.setGravity(Gravity.CENTER);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (editText.getTextColors().getDefaultColor() == R.integer.tempTextColor) {
editText.setTextColor(0xFF000000);
editText.setText("");
}
} else {
if (editText.getText().toString().equals("")) {
editText.setTextColor(R.integer.tempTextColor);
editText.setText(text);
}
}
}
});
}
I solved my problem. I used low-priority threading with a while loop to re-position the ribbon whenever it's not on top and visible. I also had to change the root layout to RelativeLayout to I could have control of the coordinates.

Categories