I am creating a RecyclerView on a fragment, and there is a button in one of the viewHolder(cell). But on the emulator, nothing happened after I clicked on that button. I set break points in the code for onClickListener, it doesn't even jump into the breakpoint.
Here is how I implement the button in the layout file:
<Button
android:id="#+id/add_to_list_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Here is my code in the adapter:
Button mAddButton
mAddButton = (Button) itemView.findViewById(R.id.add_to_list_button);
mAddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test button","testinbtn");
AddContactToList(GlobalPosition);
}
});
private static void AddContactToList(int pos) {
String list_id = mUnsubscribedList.get(pos - 2 - mSubscribedList.size()).getList_id();
apiInstance.AddContactToTheList(contact.getContactID(), list_id);
}
I had checked the resource ID, the resource ID is correct. I had cleaned the project, rebuild the project, quite and relaunch Android Studio.
Related
I want to navigate two buttons to the same class. In one XML, I have two buttons, both should work the same way. How can I achieve this?
Define two button with different id and point to same function in onClick event
<Button
android:id="#+id/button1"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:onClick="executeSameBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
And then implement your logic here:
public void executeSameBehavior(View view) {
// Your logic
}
You can simply assign one clickListener interface to both setOnClickListeners
Button a = findViewById(R.id.a);
Button b = findViewById(R.id.b);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Something for both
}
};
a.setOnClickListener(listener);
b.setOnClickListener(listener);
In my app I have a button (Add) that takes the entered name, creates a TextView with that name and places the TextView in a LinearLayout. You should be able to create how many subjects (school subjects) you want, so the properties would need to be set in the Java code (correct me if I'm wrong). I have managed make it create the TextViews, but they have default settings. How can I make them like the first TextView? I haven't found any solution on this one nor on the Android Developers website.
Here's the XML for the LinearLayout and the first TextView:
<LinearLayout
android:id="#+id/subjectLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#+id/entryEditText"
app:layout_constraintEnd_toStartOf="#+id/divider"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<TextView
android:id="#+id/subjectMATHTextView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Math"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="#+id/entryEditText"
app:layout_constraintEnd_toStartOf="#+id/divider"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.259" />
</LinearLayout>
And the Java Code:
final ProgressBar subjectMATHProgressBar = (ProgressBar) findViewById(R.id.subjectMATHProgressBar);
final EditText entryEditText = (EditText) findViewById(R.id.entryEditText);
Button progressBtn = (Button) findViewById(R.id.progressBtn);
final Button addSubjectButton = (Button) findViewById(R.id.addSubjectBtn);
// For Progress Button
progressBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if (!entryEditText.getText().toString().matches("")) {
int currentProgress = subjectMATHProgressBar.getProgress();
int toSet = currentProgress + Integer.parseInt(entryEditText.getText().toString());
subjectMATHProgressBar.setProgress(toSet);
}
} catch (Exception e){
Toast.makeText(getApplication().getBaseContext(), "Enter a number", Toast.LENGTH_SHORT).show();
}
}
});
final LinearLayout subjectLayout = (LinearLayout) findViewById(R.id.subjectLayout);
LinearLayout progressLayout= (LinearLayout) findViewById(R.id.progressLayout);
// For Add Subject Button
addSubjectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// Important code:
TextView newTextView = new TextView(getApplication().getBaseContext());
newTextView.setText(entryEditText.getText().toString());
subjectLayout.addView(newTextView);
} catch (Exception e){
Toast.makeText(getApplication().getBaseContext(), "Enter a valid name", Toast.LENGTH_SHORT).show();
}
}
});
Thanks in advance!
That is not how it works, if i get it right u need to make a list of subjects?
For this purpose there is a special view - RecyclerView or ListView. It allows you to populate your layout with a list of data. Each data entry is responsible for different item in list.
Here you can find how it works and to use it:
https://developer.android.com/guide/topics/ui/layout/recyclerview
My app crashes after I pressed a button.
My code:
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_print_trans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:onClick="OnClickPrintSimpleApiTest"
android:text="PRINT"
android:textColor="#FFFFFF" />
and:
public void OnClickPrintSimpleApiTest(View view) {
final Button BTN_print = (Button) findViewById(R.id.btn_print_trans);
BTN_print.setBackgroundColor(Color.GREEN);
BTN_print.setEnabled(false);
}
Because the OP defined the onClick method OnClickPrintSimpleApiTest as an attribute in their xml layout file as:
android:onClick="OnClickPrintSimpleApiTest"
They do not need to get a reference to the Button using findViewById().
The Button view is passed to the OnClickPrintSimpleApiTest() method as the parameter "view". Therefore, simply do this:
public void OnClickPrintSimpleApiTest(View view) {
Button BTN_print = (Button) view
BTN_print.setBackgroundColor(Color.GREEN);
BTN_print.setEnabled(false);
}
I have a set of image view that I want to change when it is clicked. The first view is set by default, and I want the second one to appear when the first one is clicked. I also want the reverse to occur when the second one is visible. How can I do this? (Example: Default is Img 1 default, img 2 hidden. On click, img 2 shows, img 1 is hidden. On click again, reverse occurs).
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_off_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp" />
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_alarm_on_75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_marginTop="134dp" />
Use the Following Code:
ImageView img1=(ImageView)findViewById(R.id.img1);
ImageView img2=(ImageView)findViewById(R.id.img2);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img1.setVisibility(View.GONE);
img2.setVisibility(View.VISIBLE);
}
});
img2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img2.setVisibility(View.GONE);
img1.setVisibility(View.VISIBLE);
}
});
in both of button onclick you must check to see if other button is not visible then set it visible and if it is visible then do not do any thing. you can check visibility of buttons with this code
if(mybutton.getVisibility()==View.VISIBLE)
{
//set visibiliy of mybutton
}
I have an application with an input text where the users have to insert an information and a button "+" beside to input text.
I would like to make my form dynamic in a way that when a user pushes on "+" button appears dynamically another text input and another "+" button beside this one, the process is repeated in the same way.
I created and xml file, sample_content:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/attempt"
android:layout_alignParentBottom="true"
android:text="TextView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="36dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:text="+" />
<EditText
android:layout_width="229dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/addKey"
android:background="#drawable/inputtext_corner"
android:ems="10"
android:textSize="18sp" >
<requestFocus />
</EditText>
</RelativeLayout>
and in my Activity, AddDeviceActivity I put:
inflater = LayoutInflater.from(AddDeviceActivity.this);
Button addKey = (Button) findViewById(R.id.addKey);
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});
But this solution doesn't work because when I add the first input text and the first button, I don't know how to make the second button work in my AddDeviceActivity dynamicly
Just wondering whether you can do this:
Have your activity implement OnClickListener and add this method to your activity:
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, canvas, false);
canvas.addView(childView);
((Button)childView.findViewById(R.id.addKey)).setOnClickListener(AddDeviceActivity.this);
}
And then change your initial code to use
addKey.setOnClickListener(this);
instead of an anonymous inner class.
I haven't tested this, but don't see why it wouldn't work.
check out this, pass null instead of canvas object in inflate() method
addKey.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final RelativeLayout canvas = (RelativeLayout) AddDeviceActivity.this.findViewById(R.id.my_canvas);
final View childView = inflater.inflate(R.layout.sample_component, null, false);
// TODO: Look up the 5 different signatures of the addView method,
// and pick that best fits your needs
canvas.addView(childView);
}
});