Hide/Show TextView when touching button inside a Fragment - java

So I am here asking for your kind help.
I am trying to make a TextView visible and gone by touching a single button.
It did work inside a MainActivityJava but when doing the same inside a Fragment Activity, I am facing many troubles.
It seems that the setOnClickListener does not match with fragment but I do not know how to deal with it...
package com.androidbegin.locumatix;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Tab1Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
return view;
}
Button button = getActivity().findViewById(R.id.buttonTaureauBrute);
TextView textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="#drawable/wallpaper_selection">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
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">
<ImageButton
android:id="#+id/imageButtonTaureauSon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/prononciation_icone" />
<Button
android:id="#+id/buttonTaureauBrute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:text="Prendre le taureau par les cornes"
android:textAlignment="center"
android:onClick="onClick"/>
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:gravity="center"
android:visibility="gone"
android:text="Affronter une difficulté avec détermination"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="#drawable/taureau" />
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exemples :"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NATURE :\nverbe"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="REGISTRE :\nstandard"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NIVEAU :\nB1"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
</LinearLayout>
<Button
android:id="#+id/button14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EXPLICATIONS :" />
<Button
android:id="#+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="USAGES :" />
<Button
android:id="#+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lexique :" />
<Button
android:id="#+id/button12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Traductions :" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>

Try this code
public class Tab1Fragment extends Fragment {
Button button;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
button = getActivity().findViewById(R.id.buttonTaureauBrute);
textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if( view.getVisibility() == View.GONE ) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.GONE);
}
}
};
return view;
}
}

Related

Invoking findViewById on a null object. Which isn't null

So I have a settings fragment which contains, well, settings.
On line 22 I have a findview which finds a textview. It worked wonders until today (Didn't change a thing). Now I imagine it will also fail the rest of the finds but since it crashes on this one, we will never know.
Why is that?
XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="ACCOUNT"
android:textColor="#2b2a29"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/SettingsFragmentAccountSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Account Settings"
android:textColor="#2b2a29"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="THEME"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Dark Mode"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Switch
android:id="#+id/SettingsFragmentDarkModeSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:scaleX="1.25"
android:scaleY="1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="OTHER"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentDarkModeSwitch" />
<TextView
android:id="#+id/SettingsFragmentFAQ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="FAQ"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/r" />
<TextView
android:id="#+id/SettingsFragmentSupport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Support"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentFAQ" />
<TextView
android:id="#+id/SettingsFragmentPolicy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Content Policy & EULA"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentSupport" />
<TextView
android:id="#+id/SettingsFragmentUSerAgreement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="User Agreement"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentPolicy" />
<TextView
android:id="#+id/SettingsFragmentBugReport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Report A Bug"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentUSerAgreement" />
<TextView
android:id="#+id/Build"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="BUILD INFORMATION"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentBugReport" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="1.0.0"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.042"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/Build" />
</androidx.constraintlayout.widget.ConstraintLayout>
The fragment code:
package com.example.create4me;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Settings extends Fragment {
TextView accSettings, FAQ, BugReport, Policy, UserAgg, Support;
Switch darkModeSwitch;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
accSettings = (TextView) getView().findViewById(R.id.SettingsFragmentAccountSettings); <--- This one
FAQ = (TextView) getView().findViewById(R.id.SettingsFragmentFAQ);
BugReport = (TextView) getView().findViewById(R.id.SettingsFragmentBugReport);
Policy = (TextView) getView().findViewById(R.id.SettingsFragmentPolicy);
UserAgg = (TextView) getView().findViewById(R.id.SettingsFragmentUSerAgreement);
Support = (TextView) getView().findViewById(R.id.SettingsFragmentSupport);
darkModeSwitch = (Switch) getView().findViewById(R.id.SettingsFragmentDarkModeSwitch);
accSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), AccountSettings.class);
startActivity(intent);
}
});
BugReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BugReport.class);
startActivity(intent);
}
});
FAQ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Policy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
UserAgg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
darkModeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*
In here, put 0 into MySqli DB if the isChecked value = False.
Put 1 into MySqli DB if the isChecked value = 1.
*/
}
});
return inflater.inflate(R.layout.settings, container, false);
}
void alertDiag(){
new AlertDialog.Builder(getContext())
.setTitle("Well here we are")
.setMessage("Normally, the user by now would've gone into a website explaining the content of the pressed TextView.\n"
+ "Since we do not have a website or a domain, we show this AlertDialog. Sorry for the turnout!")
.setNegativeButton(android.R.string.no, null)
.show();
}
}
You need to do on the first line of onCreateView:
View view = inflater.inflate(R.layout.settings, container, false);
Then do for each of your findViewById:
view.findViewById(R.id.your_view);
You're inflating the view last instead of first.
Then at the end of onCreateView():
return view;
Please try below way to initialize and return in your oncreateview
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.settings, container, false);
//Init all here like
accSettings = (TextView) view. findViewById(R.id.SettingsFragmentAccountSettings)
//and then return like
return view;
}
Hope it may help you

unable to transfer some data to a new page

I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?
My Code:
public void searchView (View view){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SearchFragment searchFragment = new SearchFragment();
fragmentTransaction.replace(R.id.frameLayout, searchFragment).commit();
enterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
city = cityAdd.getEditText().getText().toString().trim();
String url = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+apikey;
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
Log.d("Temperature",(url));
JSONObject temps = response.getJSONObject("main");
String temperatures = temps.getString("temp");
country.setText(city);
temp.setText(temperatures);
Intent intent = new Intent(MainActivity.this, SearchFragment.class);
intent.putExtra("city", country.getText().toString());
intent.putExtra("temp",temp.getText().toString());
startActivity(intent);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Please check the city name", Toast.LENGTH_SHORT).show();
}
}
);
queue.add(request);
}
});
}
Search Fragment.java
package com.nisaefendioglu.weatherapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class SearchFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.search_layout, container,false);
TextView country = viewGroup.findViewById(R.id.country);
TextView temp = viewGroup.findViewById(R.id.temp);
TextView back = viewGroup.findViewById(R.id.back);
return viewGroup;
}
}
XML
Country İtem
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="8dp">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="7dp"
android:layout_marginHorizontal="15dp">
<TextView
android:id="#+id/countryName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Türkiye"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Temperature"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Main 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:hint="Enter City" />
<Button
android:onClick="searchView"
android:id="#+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="300dp"
android:text="Enter"
android:textAllCaps="false"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="#drawable/weather"/>
<RelativeLayout
android:id="#+id/activityRelative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:textColor="#color/black"
android:text="Country Name"
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:text="Temperature"
android:textColor="#color/black"
android:textSize="15dp" ></TextView>
</RelativeLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countries"
tools:listitem="#layout/country_item_layout"
app:layoutManager="LinearLayoutManager"
/>
</FrameLayout>
</LinearLayout>
Search Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="#+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:textColor="#color/black"
android:text=""
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:text=""
android:textColor="#color/black"
android:textSize="15dp" ></TextView>
<TextView
android:onClick="tempBack"
android:id="#+id/back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
android:gravity="center"
android:text="←"
android:textColor="#ff793f"
android:textSize="40dp"></TextView>
</LinearLayout>
Hello, I want to show the data I got from json in my android project in the search layout section, but the data is not visible. Can you help me?
You are retrieving your data in your main activity and sending the data through your intent
intent.putExtra("city", country.getText().toString()); intent.putExtra("temp",temp.getText().toString());
However, you are not fetching the data that you sent through intent in your fragment.
You have to fetch them according to the key values by which you sent.
In your onCreateView() in your fragment, you can fetch each data you sent through your intent in the form:
String city = getArguments().getString("city");

onClickListener is not working in Recycler Adapter class

I am using Recycler view to show the list but setOnclicklistener is not woking in Recylerview Adapter class . Below is my Adapter class code & xml code.
i m just simply implementing setOnClickListener to adapter same setOnClickListener was working one day ago and
same issue i met with few days ago but i made anothers adapter and it did the trick but again today i met same problem
though i googled but did get the answer and i m posting here question to what could be the reason i meet often where i m making mistake and keep this in mind for further codes.
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.chat.Activities.Statedata;
import com.example.chat.Models.StateCases;
import com.example.chat.R;
import com.example.chat.databinding.StateDataItemBinding;
import java.util.ArrayList;
public class StatelistAdapter extends RecyclerView.Adapter<StatelistAdapter.StatelistHolder> {
Context context;
ArrayList<StateCases> stateCases;
public StatelistAdapter(Context context, ArrayList<StateCases> stateCases) {
this.context = context;
this.stateCases = stateCases;
}
#NonNull
#Override
public StatelistHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.state_data_item, parent, false);
return new StatelistHolder(view);
}
#Override
public void onBindViewHolder(#NonNull StatelistHolder holder, int position) {
StateCases scase = stateCases.get(position);
holder.binding.states.setText(scase.getStatename());
holder.binding.sconfirmed.setText(scase.getTodaycases());
holder.binding.stotay.setText(scase.getTotalcases());
holder.binding.todayreovered.setText(scase.getTodayrecovered());
holder.binding.srecovered.setText(scase.getRecovered());
holder.binding.sdeaths.setText(scase.getDeaths());
holder.binding.todaydeath.setText(scase.getTodaydeaths());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, Statedata.class);
intent.putExtra("totalcase", scase.getTotalcases());
intent.putExtra("todaycase", scase.getTodaycases());
intent.putExtra("statename", scase.getStatename());
intent.putExtra("todayrecovered", scase.getTodayrecovered());
intent.putExtra("totalrecovered", scase.getRecovered());
intent.putExtra("todaydeaths", scase.getTodaydeaths());
intent.putExtra("totaldeaths", scase.getDeaths());
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return stateCases.size();
}
public class StatelistHolder extends RecyclerView.ViewHolder {
StateDataItemBinding binding;
public StatelistHolder(#NonNull View itemView) {
super(itemView);
binding = StateDataItemBinding.bind(itemView);
}
}
}
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"
android:stretchColumns="1,2,3,4,5"
tools:context=".Activities.Statewiselist">
<com.romainpiel.shimmer.ShimmerTextView
android:id="#+id/lastime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Loading..." />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lastime"
android:layout_marginTop="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#318797"
android:stretchColumns="1,2,3,4,5">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/textView25"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="States"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Today Cases"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latest Reco."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Recovered"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latest deat."
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginEnd="5dp"
android:text="Deaths"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<com.cooltechworks.views.shimmer.ShimmerRecyclerView
android:id="#+id/strecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:shimmer_demo_angle="20"
app:shimmer_demo_child_count="10"
app:shimmer_demo_grid_child_count="2"
app:shimmer_demo_layout="#layout/statelistitem"
app:shimmer_demo_layout_manager_type="linear_vertical"
tools:listitem="#layout/state_data_item">
</com.cooltechworks.views.shimmer.ShimmerRecyclerView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Thanks in Advance.
In this case the reason may be in StateDataItemBinding.
Is your root view in state_data_item.xml is clickable?

EditText autoscroll doesn't work after showing dialog

I'm developing an app for users to share their books. To achieve this, I'm getting different data about the book from the user. The problem is, horizontal autoscrolling of all EditTexts are working until Dialog has been shown and dismissed. Once a dialog has been dismissed on the fragment, horizontal autoscrolling of the EditText's on this layout won't work.
I added:
android:focusable="true"
android:focusableInTouchMode="true"
to parent layout of the edittext but doesn't work.
Also to make sure that autoscrolling is enabled I've added:
android:scrollHorizontally="true"
But none of above helped.
Here is XML code of the fragment layout.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical"
android:paddingStart="#dimen/dp25"
android:paddingEnd="#dimen/dp25"
tools:context=".UI.Fragments.SharePostFragments.Fragment1.OverViewFragment">
<LinearLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:id="#+id/edit_text_name_of_book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:fontFamily="#font/segoe_ui_semi_bold"
android:hint="#string/name_of_book"
android:inputType="text"
android:maxLength="50"
android:maxLines="1"
android:scrollHorizontally="true"
android:textAlignment="textStart"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font22" />
<TextView
android:id="#+id/text_view_number_of_characters"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="#dimen/dp10"
android:fontFamily="#font/segoe_ui_light"
android:text="#string/_0_50"
android:textColor="#color/colorAccent"
android:textSize="#dimen/font16"
tools:ignore="RtlSymmetry" />
<EditText
android:id="#+id/edit_text_name_of_author"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp30"
android:background="#drawable/round_text_box_gray"
android:fontFamily="#font/segoe_ui_regular"
android:hint="#string/name_of_writer"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<RelativeLayout
android:id="#+id/constraint_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:fontFamily="#font/segoe_ui_regular"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/price_of_book"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/azn_sign"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font16" />
<EditText
android:id="#+id/edit_text_price_of_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/textView2"
android:background="#color/white"
android:clickable="false"
android:hint="#string/_0_0"
android:inputType="text|numberDecimal"
android:maxLength="6"
android:singleLine="true"
android:textColorHint="#android:color/black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/constraint_layout_conditions_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/book_condition_placeholder"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_conditions"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear_layout_conditions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/text_view_new"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/_new"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/normal"
android:textColor="#color/dark_gray_text_color" />
<TextView
android:id="#+id/text_view_old"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingTop="#dimen/dp10"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:text="#string/old"
android:textColor="#color/dark_gray_text_color" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/constraint_layout_languages_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:background="#drawable/round_text_box_border_gray"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="match_parent"
android:layout_height="#dimen/textBoxHeight"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:gravity="center_vertical"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:text="#string/language"
android:textColor="#color/colorPrimaryDark" />
<ImageButton
android:id="#+id/image_button_languages"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="#dimen/dp10"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_spinner" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_languages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/dp20"
android:paddingEnd="#dimen/dp20"
android:paddingBottom="#dimen/dp10"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Fragment Java code:
package org.kitapp.UI.Fragments.SharePostFragments.Fragment1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import org.kitapp.R;
import org.kitapp.UI.Dialogs.ConditionDialog.ConditionOfBookDialog;
import org.kitapp.UI.Dialogs.LanguageDialog.LanguageDialog;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
public class OverViewFragment extends Fragment implements OverViewContractor.View {
private final String TAG = OverViewFragment.class.getSimpleName();
private Context mContext;
private OverViewCallback mListener;
private OverViewPresenter mPresenter;
#BindView(R.id.constraint_layout_conditions_root)
LinearLayout conditionRoot;
#BindView(R.id.constraint_layout_languages_root)
LinearLayout languagesRoot;
#BindView(R.id.linear_layout_conditions)
LinearLayout conditions;
#BindView(R.id.root_layout)
LinearLayout rootLayout;
#BindView(R.id.text_view_number_of_characters)
TextView numberOfChars;
#BindView(R.id.edit_text_name_of_book)
EditText nameOfBook;
public interface OverViewCallback {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_over_view, container, false);
ButterKnife.bind(this, view);
new OverViewPresenter(this);
//rootLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
return view;
}
////// CONTRACTOR METHODS //////
#Override
public void setPresenter(OverViewPresenter presenter) {
this.mPresenter = presenter;
}
////// LISTENERS //////
#Override
#OnClick(R.id.image_button_conditions)
public void showConds() {
ConditionOfBookDialog conditionOfBookDialog = new ConditionOfBookDialog(mContext);
conditionOfBookDialog.show();
}
#Override
#OnClick(R.id.image_button_languages)
public void showLangs() {
LanguageDialog languageDialog = new LanguageDialog(mContext);
languageDialog.show();
}
#Override
#SuppressLint("SetTextI18n")
#OnTextChanged(R.id.edit_text_name_of_book)
public void onNameOfBookChange() {
int len = nameOfBook.getText().toString().trim().length();
numberOfChars.setText(len + "/50");
if (len > 50) {
nameOfBook.setText(nameOfBook.getText().toString().substring(0, 50));
nameOfBook.setSelection(nameOfBook.getText().length());
}
}
////// FRAGMENT METHODS //////
#Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
if (context instanceof OverViewCallback) {
mListener = (OverViewCallback) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OverViewCallback");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
UI Design and Dialog
In the image above, there are EditTexts Name Of Book and Name of Author. Before showing a dialog, both of them work like a charm. When writing something, the cursor automatically scrolls to the end. Once Dialog has been shown and dismissed, none of them works properly, and the cursor stays at the end of the EditText but still the characters are being written continues in the hidden part.
In this UI it cannot be seen because of the issue, but I've written.
Roses are red, violets are blue, Stackoverflow I love u
But only Roses are red, violets are blue, Stac can be seen.
Weird buggy layout UI image
Please, help me to solve this problem. Thanks in advance.
As a solution, I don't know the underlying reason, but the problem solved once I removed Butterknife library from my project.

How to open a browser through a Relative Layout

I have tried following 2 different links with no success.
https://stackoverflow.com/questions/3536165/how-can-imageview-link-to-web-page
and
https://stackoverflow.com/questions/3505788/how-to-open-default-browser/3505804#3505804
I do apologize if this is a simple fix but i'm that nooby and getting frustrated because according to those examples it should work.
Here is my Xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="#color/grey"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5sp" >
<ImageView
android:id="#+id/logo_about"
android:layout_width="85dp"
android:layout_height="105dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.11"
android:src="#drawable/web_icon" />
</LinearLayout>
<TextView
android:id="#+id/tv_content_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="#color/grey"
android:gravity="center_horizontal"
android:padding="5sp"
android:text="#string/info_about_us"
android:textColor="#color/black"
android:textSize="15sp" />
<!-- Contact Us -->
<RelativeLayout
android:id="#+id/contact"
android:clickable="true"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/img_icon"
android:clickable="true"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_email" />
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/img_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator"
android:layout_toRightOf="#+id/img_icon"
android:singleLine="true"
android:text="#string/title_contact_us"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name"
android:layout_alignRight="#+id/tv_name"
android:layout_below="#+id/tv_name"
android:singleLine="true"
android:text="#string/info_contact_us"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:id="#+id/devider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Facebook -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/facebook_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_facebook" />
<TextView
android:id="#+id/tv_name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/facebook_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/facebook_icon"
android:layout_toRightOf="#+id/facebook_icon"
android:singleLine="true"
android:text="#string/title_facebook"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name1"
android:layout_alignRight="#+id/tv_name1"
android:layout_below="#+id/tv_name1"
android:singleLine="true"
android:text="#string/info_facebook"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Google Plus -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/google_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_googleplus" />
<TextView
android:id="#+id/tv_name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/google_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/google_icon"
android:layout_toRightOf="#+id/google_icon"
android:singleLine="true"
android:text="#string/title_google_plus"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name2"
android:layout_alignRight="#+id/tv_name2"
android:layout_below="#+id/tv_name2"
android:singleLine="true"
android:text="#string/info_google_plus"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Website -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/website_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_website" />
<TextView
android:id="#+id/tv_name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/website_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator3"
android:layout_toRightOf="#+id/website_icon"
android:singleLine="true"
android:text="#string/title_website"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name3"
android:layout_alignRight="#+id/tv_name3"
android:layout_below="#+id/tv_name3"
android:singleLine="true"
android:text="#string/info_website"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Rate Us -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/rate_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_rate_me" />
<TextView
android:id="#+id/tv_name4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rate_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator4"
android:layout_toRightOf="#+id/rate_icon"
android:singleLine="true"
android:text="#string/title_rate_us"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name4"
android:layout_alignRight="#+id/tv_name4"
android:layout_below="#+id/tv_name4"
android:singleLine="true"
android:text="#string/info_rate_us"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- More Apps -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/apps_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_more_app" />
<TextView
android:id="#+id/tv_name5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/apps_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator5"
android:layout_toRightOf="#+id/apps_icon"
android:singleLine="true"
android:text="#string/title_more_app"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name5"
android:layout_alignRight="#+id/tv_name5"
android:layout_below="#+id/tv_name5"
android:singleLine="true"
android:text="#string/info_more_app"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
</LinearLayout>
Now my Fragment.java
package com.wny.wecare.fragment;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
return mRootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
}
}
do this rather bro
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
if(v == img)
Context.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://infinitycodeservices.com"))); //context stands 4 ur activity
}
Edit 1: i change the intent to Intent this was the reason for the earlier variable error..
so retry & seee
let me know if it works.. im feeling i left something.
According to my understanding, your public void onClick(View v) not called you just declare but don't set Listener,then when you set listener to ImageView on onClick() that never call too.
I think you should setListener() in onCreateView() by using anonymous inner class like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
return mRootView;
}
or If you still use implement View.OnClickListener() use this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
Hope this helps
After Hours of reading i finally solved this issue.
Thanks to this link Correct way to call onClickListener on fragments
Thank You all for your help, very appreciated.
Here is my full AboutUsFragment.Java
package com.wny.wecare.fragment;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.wny.wecare.MainActivity;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
ImageView Contact;
ImageView Facebook;
ImageView Google;
ImageView Website;
ImageView Rate;
ImageView Apps;
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_about_us, null);
Contact = (ImageView) mRoot.findViewById(R.id.contact_icon);
Contact.setOnClickListener(this);
Facebook = (ImageView) mRoot.findViewById(R.id.facebook_icon);
Facebook.setOnClickListener(this);
Google = (ImageView) mRoot.findViewById(R.id.google_icon);
Google.setOnClickListener(this);
Website = (ImageView) mRoot.findViewById(R.id.website_icon);
Website.setOnClickListener(this);
Rate = (ImageView) mRoot.findViewById(R.id.rate_icon);
Rate.setOnClickListener(this);
Apps = (ImageView) mRoot.findViewById(R.id.apps_icon);
Apps.setOnClickListener(this);
return mRoot;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.contact_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:infinitycodeservices#gmail.com")));
break;
case R.id.facebook_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/infinitycodeservices")));
break;
case R.id.google_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/105796163192090141980/")));
break;
case R.id.website_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
break;
case R.id.rate_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
case R.id.apps_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Intent intent;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String cameback="CameBack";
intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("Comingback", cameback);
startActivity(intent);
return true;
}
return false;
}
}
You are in a Fragment so I think you should invoke startActivity() method using:
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));

Categories

Resources