Android checkbox.isChecked is not working - java

I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?

ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938

Related

Can I make clickable text in dynamic text files on android studio java?

I am trying to make a application that will allow people to tag more people. I want to make such that when a person clicks / taps in the name of another person in the TextView, it should go to the person's profile in the application (but not in the web). How can I make it?
Any insight of how to do would be respected!
A short code of how to do would be much respected!
TextView Content Example: I am here in the city with Manoj and he is enjoying this place.
I want to make this such that when a person clicks Manoj it should be gone to the next fragment or next activity. And the data should is to sent to the server, so it is something We cant just use only onClick Listener for the text, as the text is determined and the link is created by public user not me?
The image would help you understand!
First in your java file find your TextView by xml id
TextView tv = (TextView)findViewById(R.Id.textView);
then, you have to set click listner on the textView
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you can use intent to naviaget to another activity
}
});

Espresso RecyclerView inside ViewPager

I am new to Android Espresso library. Trying to test click on RecyclerView item inside the ViewPager. I've searched the Internet but found nothing about ViewPager. So my question is: how to access RecyclerView inside a ViewPager using Espresso to perform click?
Let's suppose you want to tap on buttonClick, which is the id of a button inside your RecyclerView.
I use this code:
onView(allOf(withId(R.id.buttonToClick), isCompletelyDisplayed()))
.perform(click());
This gets the view with this id which is currently displayed and performs a click on it.
Hope this helps.
P.S.: Also, on espresso-contrib there are RecyclerViewActions that maybe you can use: actionOnHolderItem, actionOnItem and actionOnItemAtPosition. You can do something like:
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

Turning an Entire Relative Layout Into a Button

My application has an intro page made up of some text and image elements in a relative layout. I would like to be able to click any part of the screen and have it go to the next activity. Is it possible to use a entire relative layout as a button? If so how would you do this?
You can add android:clickable="true" to the XML for your RelativeLayout and use a standard OnClickListener as you would for a button.
Depending on what you're trying to do (perhaps touching anywhere to dismiss a screen?), you could also look into extending onTouchEvent(MotionEvent event) in your Activity, which would pick up any touches in the entire activity that were not responded to by views.
You can grab the root view as follows and add a click listener to it:
findViewById(android.R.id.content).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make your call to startActivity();
}
});
This should require less maintenance than retrieving a specific layout.
Add android:onClick="myFunction" in the RelativeLayout of the XML file and make the following function in the corresponding Activity file:
public void myFunction(View view)
{
...
}
I think you will have to add android:onClick="myFunction" for all the nested XML tags too which are nested inside the main RelativeLayout.

Adding buttons on xml file other than main

I have two xml files in my application - main.xml and options.xml.
In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that with options.xml: if I write
Button b = (Button)findViewById(R.id.b1);
, b is going to be null. What is the cause of this problem and how do I fix it?
Thank you in advance.
You need to either inflate the options.xml or set it as content view:
setContentView(R.layout.options);
before you can use the views in that layout file.
It sounds like you want to be able to access both layouts so you should do something like this:
View view = LayoutInflater.from(context).inflate(R.layout.options, null);
Button b = (Button) view.findViewById(R.id.b1);

Categories

Resources