Android Studio - Clickable CardViews inside Fragement - android-fragments

I am new to Android Studio,
I have a "Settings Fragment" in which I have multiple CardViews (ID's: "account", "store", "bank", "Language", "User Interface", "support" etc.), however I don't know how to make it possible to click on them to open the respective activities "Account", "Store" etc.
The below code, successfully shows the various CardViews in the "Settings Fragment", however I can't figure out how to add onClickListeners. Can you help? Are there other possibilities?
Thanks alot in advance
package com.example.loginregister;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
public class SettingsFragment extends Fragment {
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_settings, container, false);
}
}

Is it the card view as a whole that should be clickable?
Have you tried the following:
onClick(View v){
}

Related

App crashes on clicking on textView in fragment

I'm creating an app in which there are three tab activity.In tab Three i have a textview name as login text whenever user click on this text a new activity should be opened but app crashes when we click on this.
I have done many research to resolve this problem but i'm unable to resolve at last i'm here for the solution of my problem Please help.
Thanks in advance.
This is my loginActivity.
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class loginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText email_Id;
private EditText password;
private TextView sign_Up;
private Button sign_In;
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
firebaseAuth=FirebaseAuth.getInstance();
// if(firebaseAuth.getCurrentUser()!=null){
// directly start user profile activity
// finish();
// startActivity(new Intent(this,userProfileActivity.class));
//}
progressDialog=new ProgressDialog(this);
email_Id=(EditText)findViewById(R.id.email_id);
password=(EditText)findViewById(R.id.password);
sign_In=(Button)findViewById(R.id.sign_In);
sign_Up=(TextView)findViewById(R.id.sign_up);
sign_In.setOnClickListener(this);
sign_Up.setOnClickListener(this);
}
private void user_Login(){
String email=email_Id.getText().toString().trim();
String pass_word=password.getText().toString().trim();
if(TextUtils.isEmpty(email)){
// email is empty
Toast.makeText(this,"please enter email",Toast.LENGTH_SHORT).show();
return;// to stop the function from executation.
}
if(TextUtils.isEmpty(pass_word)){
// email is empty
Toast.makeText(this,"please enter password",Toast.LENGTH_SHORT).show();
return;
}
// here if everything ok the user will be register
progressDialog.setMessage("Registering User,please wait...");
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(email,pass_word)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()){
// start user profile activity
finish();
startActivity(new Intent(getApplicationContext(),userProfileActivity.class));
}
}
});
}
#Override
public void onClick(View view) {
if (view==sign_In){
// start user profile activity
user_Login();
}
if(view==sign_Up){
// go to registeration page
finish();
startActivity(new Intent(this,FragmentThree.class));
}
}
}
this is declaration
This is my login method
The following line in your code throws the exception:
sign_Up.setOnClickListener(this);
The problem is, that sign_Up is null!
You set sign_Up with: sign_Up=(TextView)findViewById(R.id.sign_up);
Make sure, that setContentView(R.layout.activity_login); (activity_login) is the right one. If, try to clean and rebuild your project and try again.
Update: It is possible, that sign_up should be a button? If, you make a wrong cast!
Change the TextView in this line:
sign_Up=(TextView)findViewById(R.id.sign_up);
to Button, like this:
sign_Up=(Button)findViewById(R.id.sign_up);

How to change TextView in fragment in on create or when a button is called

Almost 10 hours am I searching for an answer. Why my app crashes when i launch it.
What I realy want is a function that when I click OK on Edittext, text will be written on Text view. But first just hat to set text in on create in class that extence fragment.
package youtube.demo.youtubedemo.Fragments.Calculator_list;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import youtube.demo.youtubedemo.R;
public class Calculator_list extends Fragment {
View view;
FragmentManager fragmentManager;
public Calculator_list() {
}
TextView Damage1pise;
EditText Damage1beri;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_calculator_list, container, false);
Damage1beri = (EditText) view.findViewById(R.id.Game1_read_dmg_list_Player1);
Damage1pise = (TextView)view.findViewById(R.id.Game1_writen_dmg_list_Player1);
Damage1pise.setText("123");
return view;
}
public TextView Returnelement ()
{
return Damage1pise;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Damage1pise = (TextView)getView().findViewById(R.id.Game1_writen_dmg_list_Player1);
Damage1pise.setText("1234");
super.onCreate(savedInstanceState);
}
}
What I realy want is a funcction that when I click OK on Edittext, text will be written on Text view
public void OnButton1click(View v)
{
Damage1.settext(v.text)
// This is hard coded, First I want to set text on create then go on whit the code
}
Here are the errors.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: youtube.demo.youtubedemo, PID: 2736
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at youtube.demo.youtubedemo.Fragments.Calculator_list.Calculator_list.onCreate(Calculator_list.java:47)
at android.app.Fragment.performCreate(Fragment.java:2335)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
at android.app.BackStackRecord.setLastIn(BackStackRecord.java:857)
at android.app.BackStackRecord.calculateFragments(BackStackRecord.java:897)
at android.app.BackStackRecord.run(BackStackRecord.java:727)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
On this line of your showed code, both methods, certify yourself you have a blank space between (TextView) and the end of the sentence, like below:
Damage1pise = (TextView) getView().findViewById(R.id.Game1_writen_dmg_list_Player1);
Also, certify yourself you have a textview on your invoked layout file xml with name Game1_writen_dmg_list_Player1, because the error is been raised by a null reference, i.e.: you're invoking a not found element (maybe written with two ts?).

Navigation Drawer and dynamically created fragment conflict

I am using the default Navigation Drawer in android studio and I have a dynamically created fragment and use TabLayout to run it. It will run normally but as I try to load the same fragment from the navigation drawer i run into error. Please see below:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Introduction extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
final ImageView introImage = new ImageView(getActivity());
introImage.setId(R.id.introImageId);
introImage.setImageResource(R.drawable.introduction);
introImage.setAnimation(AnimationUtils.loadAnimation(this.getContext(), android.R.anim.slide_out_right));
introImage.setPadding(100, 100, 100, 100);
final TextView introText = new TextView(getActivity());
introText.setText(R.string.introduction_text);
// introText.setTextAppearance(android.R.style.TextAppearance_Medium);
introText.setTextSize(20);
introText.setPadding(25,25,25,25);
introText.setTextColor(ContextCompat.getColor(this.getContext(), R.color.colorPrimary));
params.addRule(RelativeLayout.BELOW, introImage.getId());
introText.setAnimation(AnimationUtils.loadAnimation(this.getContext(),android.R.anim.slide_in_left));
relativeLayout.addView(introText,params);
relativeLayout.addView(introImage);
return relativeLayout;
}
}
But as I try to load it from Navigation drawer I get an error Error:(115, 62) error: incompatible types: Introduction cannot be converted to Fragment
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fn = getFragmentManager();
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_introduction) {
fn.beginTransaction().replace(R.id.content_frame,new Introduction()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
TabPagerAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabPagerAdapter extends FragmentPagerAdapter {
int tabCount;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.tabCount=numberOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Introduction tab1= new Introduction();
return tab1;
default:
return null;
}
}
I replaced getFragmentManager(); with getSupportFragmentManager(); and its working
// FragmentManager fn = getFragmentManager();
FragmentManager fn = getSupportFragmentManager();

On Android how to call from TextView inside a fragment?

I am trying to call when someone tap or click on a number which in a TextView.
I have added permission in Android AndroidMainfest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
Also I have set the TextView android:clickable="true"
I am very new in Android development so please Help me.
I am sorry for my bad English.
Here is My code AboutDeveloper.java
package com.rupomkhondaker.sonalibank;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class AboutDeveloper extends Fragment {
public AboutDeveloper(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final TextView tv_phone = (TextView)getActivity().findViewById(R.id.devMobile);
tv_phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String phone_no= tv_phone.getText().toString().replaceAll(":", "");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phone_no));
startActivity(callIntent);
}
});
View rootView = inflater.inflate(R.layout.fragment_developer, container, false);
return rootView;
}
}
ERROR LOG
09-20 20:28:05.852 622-622/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.rupomkhondaker.sonalibank.AboutDeveloper.onCreateView(AboutDeveloper.java:22)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Replace your code by this code and tell me what is the output.
public class AboutDeveloper extends Fragment {
public AboutDeveloper(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_developer, container, false);
final TextView tv_phone = (TextView)v.findViewById(R.id.devMobile);
tv_phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try
{
String uri = "tel:"+tv_phone.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
}
catch(Exception e) {
Toast.makeText(getActivity(),"Your call has failed...",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
return v;
}
Since you were getting
java.lang.NullPointerException
at com.rupomkhondaker.sonalibank.AboutDeveloper.onCreateView(AboutDeveloper.java:22)
it meant that it was not able to find the view element.

SwipeRefreshLayout can be used in a fragment inside action bar tab with swipe views for switching between tabs?

I implemented the new SwipeRefreshLayout component in my application and it works well with ListView.
And I also implemented swipe views using action bar tabs, but I can´t get the listfragment of the tab to work with SwipeRefreshLayout.
I have a FragmentActivity that implements ActionBar.TabListener for switching between two fragments and a ViewPager. Every fragment uses SwipeRefreshLayout to swipe-to-refresh the list but when I swipe to refresh the method setOnRefreshListener is null pointer.
I had similar issues until I put the setOnRefreshListener in the correct spot.
public class MyFragment extends Fragment {
private SwipeRefreshLayout swipeLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// onRefresh action here
});
}

Resources