How can I add buttons in the dialog of xml? - java

I tried to create a dialog using xml, but I tried it many times and it will crash. I don’t know where the problem is. I ’m sure startConnect() and sendBtn() can be used. How can I create one The dialog of xml? Because I will sort out the interface finally.
This is the error code
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smart.v9, PID: 11678
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smart.v9.TcpClientActivity.tv(TcpClientActivity.java:423)
at com.smart.v9.TcpClientActivity.onClick(TcpClientActivity.java:267)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
TcpClientActivity.java tv():
private void tv(){
final AlertDialog.Builder tvalert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
final View tvview = inflater.inflate(R.layout.dialog_tv, null);
mBtn15.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//startConnect();
//sendBtn(15);
}
});
mBtn16.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(16);
}
});
mBtn17.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(17);
}
});
mBtn18.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(18);
}
});
mBtn19.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(19);
}
});
mBtn20.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(20);
}
});
mBtn21.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(21);
}
});
mBtn22.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(22);
}
});
mBtn23.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(23);
}
});
mBtn24.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(24);
}
});
mBtn25.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(25);
}
});
mBtn26.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(26);
}
});
mBtn27.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(27);
}
});
mBtn28.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(28);
}
});
mBtn29.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(29);
}
});
mBtn30.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(30);
}
});
mBtn31.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(31);
}
});
mBtn32.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(32);
}
});
mBtn33.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(33);
}
});
mBtn34.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startConnect();
sendBtn(34);
}
});
tvalert.setNegativeButton("退出", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
tvalert.setView(tvview);
tvalert.show();
}
dialog_tv.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">
<LinearLayout
android:id="#+id/tv_lin"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="on/off" />
<Button
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="靜音" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="退出" />
<Button
android:id="#+id/button18"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button19"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="#+id/button20"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="快進" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button21"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="後退" />
<Button
android:id="#+id/button22"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="↑" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button23"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="←" />
<Button
android:id="#+id/button24"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="↓" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button25"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="→" />
<Button
android:id="#+id/button26"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="enter" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button27"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="setting" />
<Button
android:id="#+id/button28"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暫停" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button29"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下一項" />
<Button
android:id="#+id/button30"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="上一項" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button31"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="電視" />
<Button
android:id="#+id/button32"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="花園" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button33"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="大門" />
<Button
android:id="#+id/button34"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="室內" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Add a lookup for the View Id
eg:
mBtn15 = tvview.findViewById(R.id.button15);
etc..
hope that may help you

Related

cant set onclick on recylerview(cardview)

MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference reference;
FirebaseRecyclerAdapter<Bookdeets,Bkhomeholder>adapter;
FirebaseRecyclerOptions<Bookdeets> options;
ProgressBar loading;
FloatingActionButton searchbtn;
TextView logout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reference = FirebaseDatabase.getInstance().getReference().child("books");
reference.keepSynced(true);
recyclerView = (RecyclerView) findViewById(R.id.rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
loading=(ProgressBar)findViewById(R.id.loading);
searchbtn=(FloatingActionButton)findViewById(R.id.searchbtn);
logout = (TextView) findViewById(R.id.Logout);
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Logout();
}
});
searchbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Opensearchpage();
}
});
options = new FirebaseRecyclerOptions.Builder<Bookdeets>()
.setQuery(reference, Bookdeets.class).build();
adapter = new FirebaseRecyclerAdapter<Bookdeets, Bkhomeholder>(options) {
#Override
protected void onBindViewHolder(#NonNull Bkhomeholder holder, int position, #NonNull Bookdeets model) {
Picasso.get().load(model.getImage()).into(holder.bookimg, new Callback() {
#Override
public void onSuccess() {
loading.setVisibility(View.GONE);
}
#Override
public void onError(Exception e) {
Toast.makeText(getApplicationContext(), "could not get the image", Toast.LENGTH_LONG).show();
loading.setVisibility(View.GONE);
}
});
holder.title.setText(model.getBookname());
}
#NonNull
#Override
public Bkhomeholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewlay, parent, false);
return new Bkhomeholder(view);
}
};
GridLayoutManager gridLayoutManager=new GridLayoutManager(getApplicationContext(),3);
recyclerView.setLayoutManager(gridLayoutManager);
adapter.startListening();
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
if(adapter!=null)
adapter.startListening();
loading.setVisibility(View.VISIBLE);
}
#Override
protected void onStop() {
super.onStop();
if(adapter!=null)
adapter.stopListening();
}
#Override
protected void onResume() {
super.onResume();
if(adapter!=null)
adapter.startListening();
}
public void Opensearchpage(){
Intent intent=new Intent(this, SearchPage.class);
startActivity(intent);
}
public void Logout() {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
Bkhomeholder.java
public class Bkhomeholder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView bookimg;
public Bkhomeholder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.bkdettitle);
bookimg = itemView.findViewById(R.id.bkdetimg);
}
activity_main.xml
<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"
tools:context=".homepage.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Library App"
android:textAlignment="center"
android:textColor="#E21B1B"
android:textSize="32dp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:clipChildren="true"
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_marginStart="8dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/searchbtn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="345dp"
android:layout_marginTop="480dp"
android:layout_marginEnd="12dp"
android:background="#B3E5FC"
android:clickable="true"
app:srcCompat="#drawable/customicon" />
<ProgressBar
android:id="#+id/loading"
style="?android:attr/progressBarStyle"
android:layout_width="76dp"
android:layout_height="76dp"
android:layout_marginStart="163dp"
android:layout_marginTop="253dp"
android:layout_marginEnd="163dp"
android:layout_marginBottom="253dp"
android:visibility="invisible"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="#+id/Logout"
android:layout_centerHorizontal="true"
android:text="Logout"
android:textAlignment="center"
android:textSize="14sp"
android:layout_marginBottom="8dp"
android:layout_marginTop="565dp"
cardview xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/bookcardview"
android:layout_width="120dp"
android:layout_height="190dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/bkdetlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="ExtraText"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginEnd="4dp"
>
<ImageView
android:id="#+id/bkdetimg"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#2d2d2d"
android:clickable="true"
android:scaleType="fitXY" />
<TextView
android:id="#+id/bkdettitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Book title"
android:textColor="#2d2d2d"
android:textSize="12sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
i tried setting on click on the cardviewholder that is separate xml file which is inflated in my mainactivity oncreate and set the onclick listener under
onBindViewHolder but i got a error regarding null object refference as if the activity could not detect the cardview
You can set OnClickListener on RecyclerView item inside onBindViewHolder like below:
#Override
protected void onBindViewHolder(#NonNull Bkhomeholder holder, int position, #NonNull Bookdeets model) {
....
holder.title.setText(model.getBookname());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do your operation here
}
});
}
Update: Remove android:clickable="true" from cardview layout like below:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/bookcardview"
android:layout_width="120dp"
android:layout_height="190dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">
<LinearLayout
android:id="#+id/bkdetlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="ExtraText"
android:foreground="?android:attr/selectableItemBackground"
android:layout_marginEnd="4dp"
>
<ImageView
android:id="#+id/bkdetimg"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#2d2d2d"
android:scaleType="fitXY" />
<TextView
android:id="#+id/bkdettitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Book title"
android:textColor="#2d2d2d"
android:textSize="12sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>

setOnClickListener not working in fragment Android

I am trying to apply a setOnClickListener on an EditText View in a Fragment and for some reason, It is not working, the code in the listener doesn't execute when I click on the EditText.
The fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
tools:context=".add_payment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/testpay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:inputType="time" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Table:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Amount:"
android:textColor="#000000"
android:textSize="24sp" />
<EditText
android:id="#+id/add_pay_amm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
</LinearLayout>
The fragment code:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_payment_fragment, container, false);
view.findViewById(R.id.add_pay_time).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
return view;
}
Edit text needs focus to call onClick().
Solution 1
request focus when you set listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
}
});
editText.requestFocus(); //request focus
Solution 2
set on touch listener:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction())
Toast.makeText(getContext(), "aaaa", Toast.LENGTH_LONG).show();
return false;
}
});
Solution 3
you can use onClickListener and onFocusChangeListener together:
EditText editText = view.findViewById(R.id.add_pay_time);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToast();//call your method
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showToast();//call your method
}
});
EditText edText = view.findViewById(R.id.add_pay_time);
edText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "aaaa", Toast.LENGTH_LONG).show();
}
});
Use getActivity().getApplicationContext() instead

Setting OnClickListener to an ImageButton

I have tried to set an OnClickListener to my imageButton but it didn't work. I've seen other questions about the topic but none of the ansqers work for me. Am I doing something wrong?
MainActivity.java
public void Button()
{
View.OnClickListener listSet = new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_LONG).show();
}
};
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(listSet);
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.xxxx.xxx.MainActivity"
android:orientation="vertical"
android:weightSum="1"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
>
<WebView
android:id="#+id/webV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
</WebView>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
</RelativeLayout>
Set clickable property of ImageButton to true
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:background="#android:color/background_light"
android:src="#drawable/ic_gear"
tools:background="#android:color/background_light"
tools:ignore="ContentDescription" />
Try this :
ImageButton ib = (ImageButton) ad.findViewById(R.id.imageButton7);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "settings", Toast.LENGTH_SHORT).show();
}
});
try:
boton= (ImageButton) findViewById(R.id.imageButton7);
boton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(this, "settings", Toast.LENGTH_LONG).show();
}
};);

FloatingActionButton Click Event not working

I have a floating action button in the following layout file activity_register :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<FrameLayout
android:paddingTop="70dp"
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<android.support.v7.widget.CardView
android:id="#+id/cv_RegisterAdd"
app:cardBackgroundColor="#009688"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="320dp"
app:cardCornerRadius="6dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:text="REGISTER"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
/>
<LinearLayout
android:layout_marginTop="10dp"
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#f0f7f4"
android:layout_width="match_parent"
android:theme="#style/TextLabel"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="Username"
android:textColor="#f0f7f4"
android:id="#+id/txtCurUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#FFFFFF"
android:theme="#style/TextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:hint="Password"
android:textColor="#f0f7f4"
android:id="#+id/txtNewPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:paddingStart="50dp"
android:paddingEnd="30dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<android.support.design.widget.TextInputLayout
android:textColorHint="#f0f7f4"
android:theme="#style/TextLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f0f7f4"
android:hint="Repeat Password"
android:id="#+id/txtRepeatPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:background="#drawable/selector_bg_edit_2"
android:textCursorDrawable="#drawable/bg_input_cursor_2"
android:paddingBottom="2dp"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:stateListAnimator="#drawable/state_list_animator_z"
android:id="#+id/btn_Register"
android:text="REGISTER"
android:textColor="#009688"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#drawable/register_btnshape"
>
</Button>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fb_Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="#drawable/plus_x"
android:transitionName="loginFab"
android:layout_gravity="center_horizontal|top" />
</FrameLayout>
</RelativeLayout>
In the activity I have set a Click event on the floating action button.
I have been debugging and checking to see if the event is ever fired and the click event is never fired. The RegisterActivity code is as follows:
public class RegisterActivity extends AppCompatActivity
{
#BindView(R.id.fb_Cancel)
FloatingActionButton mClose_fb;
#BindView(R.id.btn_Register)
Button mRegister_btn;
#BindView(R.id.cv_RegisterAdd)
CardView mRegister_cv;
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
getSupportActionBar().hide();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
showEnterAnimation();
}
mClose_fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
animationRevealClose();
}
});
}
public void showEnterAnimation()
{
Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
getWindow().setSharedElementEnterTransition(transition);
transition.addListener(new Transition.TransitionListener() {
#Override
public void onTransitionStart(Transition transition) {
mRegister_cv.setVisibility(View.GONE);
}
#Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
animateRevealShow();
}
#Override
public void onTransitionCancel(Transition transition) {
}
#Override
public void onTransitionPause(Transition transition) {
}
#Override
public void onTransitionResume(Transition transition) {
}
});
}
public void animationRevealClose()
{
Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv,mRegister_cv.getWidth()/2,0, mRegister_cv.getHeight(), mClose_fb.getWidth() / 2);
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter()
{
#Override
public void onAnimationEnd(Animator animation)
{
mRegister_cv.setVisibility(View.INVISIBLE);
super.onAnimationEnd(animation);
mClose_fb.setImageResource(R.drawable.plus);
RegisterActivity.super.onBackPressed();
}
#Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
}
public void animateRevealShow()
{
Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv, mRegister_cv.getWidth()/2,0, mClose_fb.getWidth() / 2, mRegister_cv.getHeight());
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter()
{
#Override
public void onAnimationEnd(Animator animation)
{
super.onAnimationEnd(animation);
}
#Override
public void onAnimationStart(Animator animation)
{
mRegister_cv.setVisibility(View.VISIBLE);
super.onAnimationStart(animation);
}
});
mAnimator.start();
}
#Override
public void onBackPressed() {
animationRevealClose();
}
the method which annotated with #OnClick should be out of onCreate.
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
//--------------------------TODO---------------------//
//----------Set a click event for the floating action button
}
#OnClick({R.id.btn_Login, R.id.fb_Register})
public void onClick(View view)
{
}
#OnClick(R.id.take_photo) void openCameraIntent()
{
openCameraIntent1();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialize();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
}
private void initialize()
{
btnSavePhoto = findViewById(R.id.btn_save);
photoView = findViewById(R.id.imageView);
photoViewUnuse = findViewById(R.id.imageViewUnuse);
previewPhotoText = findViewById(R.id.preview_text);
btnSavePhoto.setOnClickListener(view -> submitDataToServer());
ButterKnife.bind(this);
}

Android addListenerOnButton issue

I'm trying to create an application with 5 buttons on the main page. For some reason the ListenerOnButton is not working and the app will close before showing xml buttons layout.
Here's my MainActivity:
public class MainListActivity extends Activity {
ImageButton news;
ImageButton weather;
ImageButton counter;
ImageButton fakoi;
ImageButton gyalia;
ImageButton uvindex;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
addListenerOnButton();
}
public void addListenerOnButton() {
news = (ImageButton) findViewById(R.id.news);
weather = (ImageButton) findViewById(R.id.weather);
fakoi = (ImageButton) findViewById(R.id.fakoi);
gyalia = (ImageButton) findViewById(R.id.gyalia);
uvindex = (ImageButton) findViewById(R.id.uvindex);
news.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MyOrasisNews.class);
startActivity(intent);
}
});
weather.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
gyalia.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), GyaliaActivity.class);
startActivity(intent);
}
});
fakoi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), FakoiActivity.class);
startActivity(intent);
}
});
uvindex.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent
(getApplicationContext(), UvMainActivity.class);
startActivity(intent);
}
});
}
}
And this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="News"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/news"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/weather"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:singleLine="false"
android:text="Weather"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/weather"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/uvindex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="Uv Index"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/uvindex"
android:layout_centerInParent="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/fakoi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S Fakwn"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sfakon"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weightSum="1"
android:layout_weight="0.5">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="#+id/gyalia"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homebg"
android:gravity="center|bottom"
android:paddingBottom="10dp"
android:text="S gyalion"
android:textSize="18sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sgyalion"
android:layout_centerInParent="true"/>
</RelativeLayout>
</TableRow>
</TableLayout>
</LinearLayout>
Any help will be appreciated.
you defined Buttons in your xml but you are trying to cast them to ImageButton. change type of your ImageButtons to Button.
You try to cast Button to ImageButton.
In your XML file change the Button to ImageButton or in your activity change the ImageButton to Button.

Categories