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")));
Related
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
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");
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?
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;
}
}
I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}