XML or JAVA code for switching elements in ANDROID - java

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

Related

Change Button color when pressed

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;
}

Adding unique buttons dynmaically

This is what I have right now:
When I click Add a Class, I want another button to appear directly above the 'Add a Class' Button. This newly created button, when clicked on, will switch to an activity that shows all the assignments for that class(new button). I think I have an idea how to do this, but only in its most basic form.
I guess there's really 3 things I'm unsure about:
1) How to add a button at a specific location within an xml file through java?
2) How do I add buttons with unique identifiers?
3) How do I click on the newly created buttons so that a new activity opens which only shows the assignments for that button?
This is how my xml layout currently looks:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/mLlayoutBottomButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="Classes"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/roundedcorners"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:drawableRight="#drawable/rightarrow"
android:gravity="left|center_vertical"
android:text=" Math" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/roundedcorners"
android:textStyle="bold"
android:padding="10dp"
android:text="Add a Class" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
You want to add the button the the linearLayout I guess (id mLlayoutBottomButtons). In your activity you do this:
LinearLayout buttonsLayout = (LinearLayout) findViewById(R.id.mLlayoutBottomButtons);
You will dynamically create buttons, you can give it an id (button.setId(int id)), but it is not necessary. Just create and add it like this
Button newButton = new Button(this);
newButton.setText("New button");
buttonsLayout.addView(newButton, parentView.getChildCount() - 2); //see Edit at end of post
Now we created the button and added it to the layout. As last step you just set an OnClickListener to this button. Inside the onClick-method you can start the activity with an intent and add Extras in order to determine which assignment should be shown in the next Acitivty.
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra("assignmentType", "theNewButtonsAssignments");
startActivity(intent);
in the new Activity call getIntent().getStringExtra("assignmentType") to retrieve which assignment should be shown.
Edit:
You can use addView(View child, int index) for inserting it in a certain position inside the linearLayout.

How to make EditText not focused when creating Activity

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.
At the moment, this is at the top of my onCreate method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.
This is the XML for my EditText:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
This is what it looks like when I bring up my activity:
The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.
What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:
Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.
I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?
You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</RelativeLayout>
May this one helpful ;)
XML code:
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:focusable="false"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
Java code:
EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
set focusable false in xml and set it true via the code
In your main_layout
add this 2 lines:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>
the best solution is here:
https://stackoverflow.com/a/45139132/3172843
the correct and simple solution is to setFocusable false and setFocusableInTouchMode true . so the EditText gain focus only when user touch that EditText
android:focusable="false"
android:focusableInTouchMode="true"
Add this in onCreate()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
or in onCreateView()
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
XML Code
<EditText
android:imeOptions="flagNoExtractUi"
android:focusable="false"
/>
Java Code in onClickListerner
mEdtEnterPhoneNumber.setFocusable(true);
mEdtEnterPhoneNumber.setFocusableInTouchMode(true);
Whenever I'm trying to open Editext after selection item of Spinner, so on that time not able to open keyboard & write down the values in Edit text, but I resolved my issue with this code.
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/titleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="#string/hint_your_full_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thanks!
You can do this programmatically. use editText.setEnabled(false) in your onStart() method of your activity (not in onCreate() - because this is some method for initializing GUI components)
It is possible to use android:focusable="false" to disable it, but if that does not work for some reason, then you can simply put a LinearLayout and it will take the focus without disrupting your layout.
NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents. You should be able to disregard it with no problems.
For example:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="false"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30" >
</EditText>
</LinearLayout>
The easiest way is to add
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
in the activity tag of the Manifest.xml file
1) Its the simplest,open the manifest and put the following code between the activity tag:
android:windowSoftInputMode="stateHidden"
2) Put this attributes in the parent layout
android:focusable="true"
android:focusableInTouchMode="true"
In Manifest , copy and paste the beneath code.
<activity
android:name=".LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
android:windowSoftInputMode="stateAlwaysHidden"
add this line in the activity tag of the Manifest.xml file.when you click on the EditText the keyboard gets into focus.
use android:focusable="false" for focusing disable
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/changePass"
android:layout_centerHorizontal="true"
android:layout_marginTop="167dp"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:maxLength="30"
android:focusable="false" >
</EditText>

How can one set a OnLongClickListener to a Button in a ListView?

So what I am trying to do is, to set an OnLongClickListener to a ListView Button. I managed to put an OnClickListener to it and I also tried to put an OnLongItemClick to my whole ListView, but the problem is, that the OnLongItemClick is never called when I press the certain Button in the Item.
I was able to put the OnClickListener to the Buttons in the XML-File of the ListView-Rows by using android:onClick="myOnClickListener", but this doesn't work with the OnLongClickListener.
I also tried setting the OnLongClickListener to the button in the normal OnClickListener, but that's not what I really want because it just works after a normal click on it.
Is there any way to set the OnLongClickListener to the Buttons programmatically? They have to be unique in each row tho. Btw im not extending ListActivity.
Here is the OnClickListener for the Button:
//I attached this in my XML-File to the Button
public void handlerForPlus(View v)
{
//The LinearLayout where all my Items are in
LinearLayout myItemLayout = (LinearLayout)v.getParent();
//A random EditText in my Layout
editTextCountItem = (EditText) myItemLayout.getChildAt(0);
//The Button i want to add the OnLongClickListener to
buttonItemPlus = (Button)myItemLayout.getChildAt(1);
//Here i set the OnLongClickListener to the button, but I want to do this before it gets clicked normally.
buttonItemPlus.setOnLongClickListener(this);
editTextCountItem.setText((Integer.toString(Integer.parseInt(editTextCountItem.getText().toString()) + 1)));
myItemLayout.refreshDrawableState();
}
Here is my XML-File for the LinearLayout of the ListView-Items:
<?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="wrap_content"
android:weightSum="40" >
<EditText
android:id="#+id/etAnzahl"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:text="0" />
<!-- This is the Button i want to set a OnLongClickListener to. -->
<Button
android:id="#+id/bItemPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="25"
android:onClick="handlerForPlus"
android:text="Item" />
<Button
android:id="#+id/bItemMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="handlerForMinus"
android:text="-" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="10"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="#+id/tvPriceItemSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisS" />
<TextView
android:id="#+id/tvPriceItemLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisL" />
</LinearLayout>
</LinearLayout>
So does anybody know what could solve my Problem?
Best Regards, Alex.
buttonItemPlus.setOnLongClickListener(this); has the effect for entire ListView. What I think you're looking for is buttonItemPlus.setOnItemLongClickListener(this); which has the effect for particular TextView in your ListView.

Java/Android: When I put a ScrollView - I get a soft keyboard. Can't make a good layout

I have a problem with the layout. I want to place one textedit and one button at the bottom of my activity. And then I want a TextView component that fills all free space on my activity's layout.
I have a big text in the bigText component that's why I need to use the ScrollView.
I have a few questions:
I don't understand, why I get a soft keyboard when I place the ScrollView component? But I don't get this keyboard without the scrollview! If I turn off the soft keyboard, like this:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
Then how I can use my EditText?
Could you help me with my layout? The problem is: when I use the ScrollView - it fills all phone's display I even see my button and EditText which should place below the ScrollView (i don't see completely bottom of my layout). How can I fix this?
And that's my fail attempt of layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bigText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search string: " />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center_vertical|center_horizontal" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go >>" />
</LinearLayout>
</LinearLayout>
I'm not sure why adding a ScrollView would make the soft keyboard pop up, but it's easily remedied. Turn it off programmatically like this:
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getApplicationWindowToken();
if (binder != null) {
manager.hideSoftInputFromWindow(binder, 0);
}
where view can be your ScrollView or a TextView or just about anything else
Simply:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

Categories