Common class for elements in include layout - android

I have a layout toolbar which I use in every pages using in xml.
There are some buttons in this layout and I want to create a class which takes care of the button clicks of the buttons in toolbar.xml . Is this possible? If yes how?
This is my code of the xml file:
<?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/rl"
android:layout_width="match_parent"
android:layout_height="56dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffa500"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="visible" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignBottom="#+id/toolbar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#drawable/round_edges"
android:ems="10"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textPersonName"
android:paddingLeft="10dp"
android:visibility="invisible"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
<ImageButton
android:id="#+id/button2"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toLeftOf="#+id/button1"
android:layout_toStartOf="#+id/button1"
android:background="#android:color/transparent"
android:src="#mipmap/cart" />
<ImageButton
android:id="#+id/button1"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:src="#mipmap/contact" />
<ImageButton
android:id="#+id/button3"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toLeftOf="#+id/button2"
android:layout_toStartOf="#+id/button2"
android:background="#android:color/transparent"
android:src="#mipmap/search" />
<ImageButton
android:id="#+id/button4"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:background="#android:color/transparent"
android:src="#mipmap/phone" />
<ImageButton
android:id="#+id/button0"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>

Of course you can, but for every action you must pass the context from the activity/fragment which are calling that action.
Example of class for handling editText and button1 from your layout(other elements you can add yourself)
public class TestClass {
public void getEditText(Context context, View view){
EditText et = (EditText) view.findViewById(R.id.editText);
Toast.makeText(context,"Here I handle editText!",Toast.LENGTH_SHORT).show();
}
public void setImageButton(final Context context, View view){
ImageButton imageButton = (ImageButton) view.findViewById(R.id.button1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something
Toast.makeText(context,"button1 clicked!",Toast.LENGTH_SHORT).show();
}
});
}
}
You call this methods from your activity like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestClass tc = new TestClass();
tc.getEditText(this,findViewById(android.R.id.content));
tc.setImageButton(this,findViewById(android.R.id.content));
}

Related

How start New Activity when Click on Title Of HEADER_VIEW in RecyclerView To display items in IT

enter image description here
In Activty one i have RecyclerView with to View :
HEADER_VIEW
ITEM_VIEW
Header_view.xml
<?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="?attr/actionBarSize"
android:background="#FFF7F7F7">
<TextView
android:id="#+id/tv_group_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="#F50057"
android:textSize="20sp" />
<TextView
android:id="#+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:text="More..." />
</RelativeLayout>
Item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:src="#mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/imageView"
android:background="#f5fff2">
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="name"
android:textAlignment="center"
android:textColor="#FF20C200"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/new_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/old_price"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/old_price"
android:text="480.00 DA"
android:textColor="#FF65C2FD"
android:textStyle="bold" />
<TextView
android:id="#+id/old_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/tv_name"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginStart="5dp"
android:text="250.00 DA"
android:textAlignment="center"
android:textColor="#F50057" />
<ImageButton
android:id="#+id/overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/new_price"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:srcCompat="#drawable/ic_more_vert_black_24dp" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I created the Adapter and it Work Fine .
My problem is :
I need when I click on more TextView In HEADER_VIEW an New Activity Start
with Specific Itemes Of Stection.
My try :
In Adapter Exactly in onBindViewHolder
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//Context context = mContextWeakReference.get();
if (mContext == null) {
return;
}
if (SECTION_VIEW == getItemViewType(position)) {
final SectionViewHolder sectionViewHolder =(SectionViewHolder)holder;
GroupTitleModel sectionItem = ((GroupTitleModel) mUsersAndSectionList.get(position));
sectionViewHolder.title.setText(sectionItem.title);
sectionViewHolder.more.setText(sectionItem.moreSection);
sectionViewHolder.more.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
// I TRY TO DO IT HERE BUT NO THINGS
}
} );
return;
}
My first thought is that you are bringing yourself into trouble by doing it this way.
You separate the Header view and its Content view, so the problem appear when the Header has no idea which content to continue to next step. If you don't want to refactor than it's ok, there will be a workaround. But my suggestion is that you combine the header and the content into 1 ViewHolder, keep the title, more text, and items inside 1 Model class too. They need to go with each other so they know how to handle with interaction

android studio zoom image in + horizontal scrolling

I am working on an android project, and for that I have a human model image:
I would like to zoom right in, so that you can only see one human model e.g. the middle one to start with, then have 2 arrows on either side of the image, and click left arrow to see the left human model, and right to see the right human model.
This is my attempt, but doesn't work
layout activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_home_page"
tools:context="com.example.lahirufernando.skinsensor.HomePage"
android:background="#e0e0e0">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textViewBottom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/peace" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#drawable/loginbox" />
<Button
android:id="#+id/ibLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/left_arrow" />
<Button
android:id="#+id/ibRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/questionBtn"
android:layout_alignStart="#+id/questionBtn"
android:background="#drawable/right_arrow" />
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:fadingEdgeLength="100dp"
android:layout_toRightOf="#id/ibLeft"
android:layout_toLeftOf="#id/ibRight"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/humanbody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:background="#null" />
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/textViewBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:textColor="#FFFF"
android:text="Tip: You can zoom in and turn your device!"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/questionBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/bodyPart"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="35dp"
android:clickable="true"
app:backgroundTint="#FFFF"
app:fabSize="mini"
app:srcCompat="#drawable/question" />
<TextView
android:id="#+id/bodyPart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Click a Body Region"
android:textColor="#000000"
android:textSize="20dp" />
activity:
private final String TAG = getClass().getSimpleName();
HorizontalScrollView hSV;
Button ibLeft,ibRight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
FloatingActionButton homeBtn = (FloatingActionButton) findViewById(R.id.homeButton);
FloatingActionButton questionButton = (FloatingActionButton) findViewById(R.id.questionBtn);
hSV = (HorizontalScrollView) findViewById(R.id.hsv);
ibLeft=(Button) findViewById(R.id.ibLeft);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() - 10, (int)hSV.getScrollY());
}
});
ibRight=(Button) findViewById(R.id.ibRight);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() + 10, (int)hSV.getScrollY());
}
});
// Add image
ImageView image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.human);

Click TextView inside linear layout inside ScrollLayout

this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked

How to create custom dialog box with two button in android?

How to generate custom dialog box in android like this,
I want just like this.
How to generate it. please give me suggestion.
i have used below code for dialog box, what is the problem in my code?
I have not identify it. please share me any Idea.
<?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:background="#android:color/transparent">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video" />
</LinearLayout>
</RelativeLayout>
Please share me any Idea.
Thanks.
Simple, 1st need to create an newcustom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="250dp"
android:layout_gravity="center"
android:layout_marginLeft="55dp"
android:layout_marginRight="55dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="165dp"
android:layout_gravity="center"
android:layout_marginEnd="60dp"
android:layout_marginStart="60dp"
app:cardCornerRadius="8dp"
app:cardElevation="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="Quit Earning?"
android:textColor="#android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. quit Earning?"
android:textSize="18dp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmNo"
android:layout_marginRight="45dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="No"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/frmOk"
android:layout_marginLeft="50dp"
android:layout_marginTop="75dp">
<android.support.design.widget.FloatingActionButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#android:color/transparent"
app:backgroundTint="#color/fab1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="6dp"
android:text="Ok"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
</FrameLayout>
Then, in java file (in activity) paste this code
public class ViewDialog {
public void showDialog(Activity activity) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.newcustom_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
FrameLayout mDialogNo = dialog.findViewById(R.id.frmNo);
mDialogNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
FrameLayout mDialogOk = dialog.findViewById(R.id.frmOk);
mDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
dialog.show();
}
}
Finally you can call it wherever you want.
ViewDialog alert = new ViewDialog();
alert.showDialog(CustomDialogActivity.this);
Inside your dailog.xml
<?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:background="#android:color/white">
<RelativeLayout
android:id="#+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/btn_white"
android:paddingBottom="#dimen/thirty_dp"
android:paddingLeft="#dimen/ten_dp"
android:paddingRight="#dimen/ten_dp"
android:paddingTop="#dimen/ten_dp">
<TextView
android:id="#+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="#dimen/ten_dp"
android:text="Quit LEarning?"
android:textSize="#dimen/twenty_sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="#dimen/ten_dp"
android:layout_marginRight="#dimen/ten_dp"
android:layout_marginTop="#dimen/twenty_dp"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
<Button
android:id="#+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
</LinearLayout>
</RelativeLayout>
open dimens.xml and add code mentioned below
<dimen name="thirty_dp">30dp</dimen>
<dimen name="ten_dp">10dp</dimen>
<dimen name="twenty_sp">20sp</dimen>
<dimen name="twenty_dp">20dp</dimen>
<dimen name="sixteen_sp">16sp</dimen>
open drawable and create btn_white.xml add code mentioned blow
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp"></corners>
<solid android:color="#android:color/white"></solid>
</shape>
open mainactivity.java and add the code mentioned below
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dailog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
Button videoButton = (Button) dialog.findViewById(R.id.btn_video);
// if decline button is clicked, close the custom dialog
videoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="#+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="#android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
You can change your button by using
android:src=#drawable/image
You have to extends Dialog and implements OnClickListener
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
Call Dialog
CustomDialogClass cdd=new CustomDialogClass(Activity.this);
cdd.show();
Maybe try this method
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="#color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="10dp"
app:contentPadding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_all"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorButtonNormal"
android:text="Tebrikler!"
android:layout_gravity="center_horizontal"
android:textSize="36sp"
android:padding="8dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnDialogCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Çıkış"
android:textColor="#FFF"
android:layout_marginRight="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
<Button
android:id="#+id/btnDialogOk"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Devam"
android:textColor="#FFF"
android:layout_marginLeft="5dp"
android:background="#drawable/dialog_button_background"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
main.java
Dialog dialog = new Dialog(context, R.style.CustomDialog);
LayoutInflater layoutInflater = LayoutInflater.from(context);
CardView cardView = (CardView) layoutInflater.inflate(R.layout.dialog, null);
dialog.setContentView(cardView);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialogBtnCancel();
private void dialogBtnCancel(){
mBtnDialogCancel = dialog.findViewById(R.id.btnDialogCancel);
mBtnDialogOk = dialog.findViewById(R.id.btnDialogOk);
mBtnDialogCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
mBtnDialogOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
});
}

Clicking the button of the recyclerview item makes buttons of other recyclerview item respond too

I have created a recyclerview usin cardview and added button in each of the items of the recyclerview. I have already integrated the button click event . Now, when I click the button of one of the recyclerview item it responds normally and the button of other items responds too at the same time as if they are also clicked.what is the problem? how can it be solved?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:cardCornerRadius="1dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true">
<ImageView
android:id="#+id/icon_vendor1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/ic_direction" />
<TextView
android:id="#+id/vendorName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon_vendor1"
android:text="Kozzaja Infotech Pvt Ltd"
android:textSize="20sp" />
<RatingBar
android:id="#+id/rating"
style="#style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vendorName1"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/icon_vendor1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/vendorName1"
android:layout_marginBottom="3dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button4"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="10dp"
android:src="#drawable/ic_call_black_36dp" />
<ImageButton
android:id="#+id/button3"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="20dp"
android:src="#drawable/ic_messenger_outline_black_36dp" />
<ImageButton
android:id="#+id/button2"
android:layout_width="38dp"
android:layout_height="match_parent"
android:background="#drawable/circle_button"
android:src="#drawable/ic_directions_black_24dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/layout"
android:background="#drawable/circle_button"
android:tag="off"
android:text="+"
android:textSize="30sp" />
<View
android:layout_width="11dp"
android:layout_height="38dp"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my adapter's code.
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(workers.get(position).getName());
holder.rating.setRating(workers.get(position).getRatings());
Picasso.with(context).load(workers.get(position).getImageLogo()).fit().into(holder.imageView);
holder.plus.setTag("off");
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/// button click event
if (holder.plus.getTag().toString().equals("off")) {
holder.plus.setTag("on");
holder.plus.setText("-");
//show the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_to_right));
holder.layout.setVisibility(View.VISIBLE);
} else if (holder.plus.getTag().toString().equals("on")) {
holder.plus.setTag("off");
holder.plus.setText("+");
//hide the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_to_left));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//disappear the button after 500ms
holder.layout.setVisibility(View.GONE);
}
}, 500);
}
}
});
}

Categories

Resources