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

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>

Related

OnClick in a CardView <include> layout

I have a xml that contains some CardView <include>, I want to set onClick in the CardView includes to open an Activity in my project. I tried to set the onClick in the CardView layout as you will see in the code below, it opens normally but triggers an error sometimes and the application stops. How can I set the onClick in this include?
This is the <include> in my xml
<include layout="#layout/view_caderno"
android:id="#+id/view_caderno" />
and here is the CardView
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="15dp"
android:layout_margin="5dp"
android:onClick="Caderno"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_margin="2dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pic1"
android:layout_width="100dp"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:padding="5dp"
android:src="#drawable/note"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pic1"
android:maxLines="3"
android:text="Diário"
android:padding="10dp"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:maxLines="3"
android:padding="5dp"
android:drawableRight="#drawable/baby2"
android:text="#string/diário"
android:textColor="#666"
android:textSize="14dp" />
</RelativeLayout>
You have to make OnClickListener programmatically :
CardView yourCardView = (CardView) findViewById(R.id.view_caderno);
yourCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do what you want on click event
}
});
Check this answer - stackoverflow.com/a/37790333/1333022
Dynamically you can add layout in your cardview.

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

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.

Android: creating two linearlayout in single relative layout with on click button

In my single screen android project(using only one XML file) when i press button1 then linerlayout1 is open and when i press button2 then linearlayout2 is open.my button1 and button2 is placed in linearlayout3.can it works??if yes then how??
thanks in advance.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout1" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button2" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout2" />
<RadioButton
android:id="#+id/radioButton14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout2" />
</LinearLayout>
</RelativeLayout>
Yes, it's very simple:
Use the android:Visibility XML tag initially, and when each respective button is pressed, you'd obtain your reference to the LinearLayouts, and set their visibility to VISIBLE or GONE in code.
Note: you want to use GONE rather than INVISIBILE as INVISIBILE will still take up screen space, so there'll be a large chunk of blank space where it was before, whereas I assume you want them in place of each other
in the layout xml of linear layout 1 and 2, use
android:visibility="gone"
From code
LinearLayout l1 = (LinearLayout) findViewById(R.id.linearLayout2);
you can use the following to make it visible
l1.setVisibility(View.VISIBLE);
and to hide it
l1. setVisibility(View.GONE):
You can play around with the "setVisibility" property of your layout.
In the "onCreate()" method you can do something like this
LinearLayout layout1 = (LinearLayout) view.findViewById(R.id.linearLayout1);
layout1.setVisibility(View.GONE);
And then on your button press
layout1.setVisibility(View.VISIBLE);

Android Dynamically center the buttons when inflating layout

I need to center layout dynamically, but the problem is I use inflate method to add the layout. And I want buttons to be center.
I tried adding xml attributes like gravity, layout_gravity. But it didn't work.
And I tried to add parameters, but since I inflate xml I can't add parameters. Because I can't use addRule method.
Then I tried to make every element center by adding gravity attribute to root layout, it didn't work out either.
Here is the my code.
actvity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="335dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/buttons_layout"
android:layout_below="#+id/surfaceView1"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
three_buttons_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="29dp"
android:text="Button 1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="29dp"
android:text="Button 2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
And here is my java code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
parent = (ViewGroup)findViewById(R.id.buttons_layout);
captureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
camera.takePicture(null, null, photoCallback);
captureButton.setVisibility(View.GONE);
inflater.inflate(R.layout.three_buttons_layout, parent, true);
}
});
I want Button1, Button2, and Button3 to be center.
A horizontal LinearLayout will always left-align its subviews. You're going to have to use a RelativeLayout in order to get the positioning you want. I'd suggest giving button1 the android:layout_alignParentLeft attribute, button2 the android:layout_centerHorizontal attribute and button3 the android:layout_alignParentRight attribute, like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="29dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button 3" />
</RelativeLayout>
(I'm just editing your code in the comment editor, so there might be a syntax error in there somewhere, but you get the idea.)
Also, you should be able to set any attribute you want on those buttons after inflating the view. Just call findViewById(R.id.button1) and it should return you the view you want (as long as its after the inflater.inflate call). On that note, you've given all your buttons the same id, which probably isn't a good idea.
Hope that helps! Let me know if it's still not working.
Edit: I just realized you're going to need that RelativeLayout to have android:layout_width="match_parent". I've edited the above xml accordingly.
//try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
//your can remove this below line from parent to make it as center horizontally
android:layout_gravity="center"

Categories