activity_main.xml
activity_main.xml for cardview means this displays the card.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:id="@+id/c1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lin_nameage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="#000000"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:gravity="start"
android:textSize="15dp"/>
<!-- to set edittext value -->
<TextView
android:id="@+id/tvet_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/tvName"
android:textSize="15dp"/>
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="age:"
android:textColor="#000000"
android:layout_marginLeft="172dp"
android:layout_toRightOf="@+id/tvet_name"
android:textSize="15dp"/>
<!-- to set edittext value -->
<TextView
android:id="@+id/tvet_age"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginLeft="200dp"
android:layout_toRightOf="@+id/tvet_name"
android:textSize="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lin_nameage">
<TextView
android:id="@+id/tvrel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relationship:"
android:textColor="#000000"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:gravity="start"
android:textSize="15dp"/>
<!-- to set edittext value -->
<TextView
android:id="@+id/tvet_rel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/tvName"
android:textSize="15dp"/>
<TextView
android:id="@+id/tv_occupation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Occupation:"
android:textColor="#000000"
android:layout_marginLeft="80dp"
android:layout_toRightOf="@+id/tvet_name"
android:textSize="15dp"/>
<!-- to set edittext value -->
<TextView
android:id="@+id/tvet_occu"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginLeft="200dp"
android:layout_toRightOf="@+id/tvet_name"
android:textSize="15dp"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
first.xml :
first.xml is the layout that has to open when I click on cardview.
<?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"
android:paddingBottom="3dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/lin_act_name"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/actname"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Name"
android:inputType="number">
<EditText
android:id="@+id/act_name"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/actage"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="age">
<EditText
android:id="@+id/act_age"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/lin_relationship"
android:layout_height="45dp"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:layout_below="@+id/lin_act_name">
<android.support.design.widget.TextInputLayout
android:id="@+id/actrelation"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:inputType="number"
android:hint="Relation">
<EditText
android:id="@+id/act_relation"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/actoccupation"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:hint="Occupation">
<EditText
android:id="@+id/act_occupation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/okbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="20dp"
android:layout_below="@+id/lin_relationship"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements
OnClickListener {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button = (Button) findViewById(R.id.buttonShowCustomDialog);
// add button listener
}
@Override
public void onClick(View view) {
{
if (view ==) {
Details();
}
}
public void Details() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.first);
dialog.setTitle("Enter Details");
// set the custom dialog components - text, image and button
EditText act_name = (EditText) dialog.findViewById(R.id.act_name);
EditText act_age = (EditText) dialog.findViewById(R.id.act_age);
EditText act_relation = (EditText) dialog.findViewById(R.id.act_relation);
EditText act_occupation = (EditText) dialog.findViewById(R.id.act_occupation);
//text.setText("Android custom dialog example!");
//ImageView image = (ImageView) dialog.findViewById(R.id.image);
// image.setImageResource(R.drawable.ic_launcher);
Button ok_btn = (Button) dialog.findViewById(R.id.okbtn);
// if button is clicked, close the custom dialog
ok_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
}
when I click on cardview, dialog box should open. How to write Onclicklistener to cardview. I am not using any recyclerview. I got struck with this. How to write Onclick to cardview. Any help would be appreciated.