OnClick in a CardView <include> layout - java

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.

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 update divider to the right place in a RecyclerView?

I wrote the following layout:
<?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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
<TextView
android:id="#+id/full_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/avatar"
android:layout_toEndOf="#id/avatar"
android:textSize="20sp"
android:textColor="#color/blackColor"
android:layout_centerVertical="true" />
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/avatar"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>
It's the item layout of the RecyclerView. Basicly it creates the following layout:
[avatar] [name]
---------------------
[avatar] [name]
---------------------
[avatar] [name]
The Java code:
public void onBindViewHolder(ViewHolder holder, int position) {
// Code
// Set avatar
// Set name
holder.fullname.setText((CharSequence) name);
// Set visible/invisible divider
if (position < getItemCount() - 1) {
holder.viewDivider.setVisibility(View.VISIBLE);
} else {
holder.viewDivider.setVisibility(View.GONE);
}
}
The problem is that the divider is being set on top of the ImageView and the TextView and not on the bottom of each block. I think that I understand why it happens - the divider is already there so when I set ImageView and TextView dynamically, the divider is not being rendered to the right place. Or maybe it's related to android:layout_centerVertical? How should I fix it?
Also, In the activity which sets the adapter I call:
holder.users.setLayoutManager(new LinearLayoutManager(this.activity));
Is it ok that I call LinearLayoutManager when the layout (of the item) is RelativeLayout?
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/avatar"
android:layout_centerVertical="true" <-- REMOVE THIS LINE :)
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
Maybe you should enclose your image and textview in a layout preferably in a relative layout and then take a view and below that layout like this :
<?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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rlLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" />
<TextView
android:id="#+id/full_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/avatar"
android:layout_toEndOf="#id/avatar"
android:textSize="20sp"
android:textColor="#000000"
android:text="Test String"
android:layout_centerVertical="true" />
</RelativeLayout>
<!-- Divider -->
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_below="#+id/rlLayout"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" />
</RelativeLayout>

Elevation on circular Image not woking Android

the elevation is not working even after adding padding to the view, here is the Relative layout, what else I should add for a shadow in the image view.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
I use the code below to add shadow to any View that looks circular. Works on Lollipop and above.
myCircularImageView.setOutlineProvider(new ViewOutlineProvider() {
#Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
myCircularImageView.setClipToOutline(true);
I don't encourage using CardView because it makes your layout more complex.
you can use cardview as parent layout of relative layout & give the elevation to cardview. It will give shadow to your imageview. Hope it will work for you.
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardCornerRadius="5dp"
card_view:cardBackgroundColor="#color/colortransperent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Easy way is to use textview in a card view and then add shadow to it using
android:elevation="", see this on how to use CardView.
Best is to use CardView and add the RelativeLayout inside the CardView and fill it with match_parent.
Then use 'elevation' attribute of CardView
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/tvContinueWith"
android:layout_marginTop="#dimen/_10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgFb"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:elevation="#dimen/_5dp"
android:src="#drawable/fb"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgGoogle"
android:layout_toRightOf="#+id/imgFb"
android:layout_marginLeft="#dimen/_5dp"
android:layout_centerInParent="true"
android:src="#drawable/google"
android:elevation="#dimen/_5dp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Dependency for CardView
implementation 'com.android.support:cardview-v7:28.0.0'

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