i want to add an intent for an imageview onclick which links to a weburl. below u can see the xml file of home fragment page and java file of home fragment.kindly help me with this
HomeFragment.xml
<ImageView
android:id="#+id/imageView2"
android:layout_width="80dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_marginRight="15dp"
android:src="#drawable/scial" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="130dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:src="#drawable/log" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:src="#drawable/line" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="180dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView3"
android:src="#drawable/banner" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="400dp"
android:layout_height="300dp"
android:layout_below="#+id/imageView4"
android:layout_centerHorizontal="true"
android:src="#drawable/mainn" />
</RelativeLayout>
HomeFragment.java
package com.tabs;
import com.tabs.R;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
Add a intent to one of the imageview's with the required address of webpage. Try this:
image.setOnClickListener(new View.onClickListener(){
String url = "http://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(i);
});
Have you tried adding inside your onCreateView?
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.your_weburl.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(i);
}
});
You should use image button; i.e: ib_playPause
XML
ImageButton
android:id="#+id/ib_play"
android:src="#drawable/ic_play"
JAVA:
private ImageButton play;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
play = (ImageButton) rootView .findViewById(R.id.ib_play);
return rootView;
}
//for onclick
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
play.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri
.parse("https://play.google.com/store");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
});
}
Related
I am currently trying to link up my xml textviews to my Profile Fragment. However, I get a null object reference error on the line
nameAge = getView().findViewById(R.id.nameAge);
Here is the Fragment.java
package com.example.helloworld2;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.os.Bundle;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
public class ProfileFragment extends Fragment{
private TextView nameAge;
private TextView occupation;
private TextView description;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_profile, null);
nameAge = getView().findViewById(R.id.nameAge);
occupation = getView().findViewById(R.id.occupation);
description = getView().findViewById(R.id.description);
return view;
}
}
Here is the xml file
<?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:orientation="vertical">
<ImageView
android:id="#+id/profile_image"
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:src="#drawable/nero" />
<TextView
android:id="#+id/nameAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/occupation"
android:textSize="40dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/description"
android:paddingTop="40dp"
android:textSize="30dp"/>
</LinearLayout>
Thank you
do this in onViewCreated() not in onCreateView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, null);
return view;;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
nameAge =getActivity().findViewById(R.id.nameAge);
occupation = getActivity().findViewById(R.id.occupation);
description =getActivity().findViewById(R.id.description);
}
Red text came out a little small. Here is what it says:
Need to get the background color of the dialog so I can set the background of the "more info" textview to that same color. Need to do this programmatically because color of the dialog is different depending on API level.
Java code:
public class CityDetailDialog extends DialogFragment {
private SharedPreferences sharedPreferences;
private City mCity;
private LinearLayout mCityContainerLayout;
private ImageView mImageFlag;
private TextView mTextCity;
private ImageView mImageCity;
private ImageButton mButtonGoogleMaps;
private ImageButton mButtonWikipedia;
private TextView mMoreInfoTextView;
public CityDetailDialog() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
sharedPreferences = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
return inflater.inflate(R.layout.dialog_city_detail, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCity = (City) getArguments().getSerializable("city");
mCityContainerLayout = (LinearLayout) view.findViewById(R.id.container_city_dialog);
mImageFlag = (ImageView) view.findViewById(R.id.flag_image);
mTextCity = (TextView) view.findViewById(R.id.city_name);
mImageCity = (ImageView) view.findViewById(R.id.city_image);
mButtonGoogleMaps = (ImageButton) view.findViewById(R.id.button_google_maps);
mButtonWikipedia = (ImageButton) view.findViewById(R.id.button_wikipedia);
mMoreInfoTextView = (TextView) view.findViewById(R.id.more_info_label);
mImageFlag.setImageResource(ResourceByNameRetriever.getDrawableResourceByName(mCity.
getCountryName(), getActivity()));
mTextCity.setText(ResourceByNameRetriever.getStringResourceByName(mCity.getCityName(),
getActivity()));
Picasso.with(getActivity()).load(mCity.getImageUrl()).into(mImageCity);
mButtonGoogleMaps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mapsIntent = new Intent(Intent.ACTION_VIEW);
mapsIntent.setData(Uri.parse("geo:" + mCity.getCoordinates()));
Intent chooser = Intent.createChooser(mapsIntent, getString(R.string.launch_maps));
startActivity(chooser);
}
});
mButtonWikipedia.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent wikiIntent = new Intent(Intent.ACTION_VIEW);
wikiIntent.setData(Uri.parse(mCity.getWikiUrl()));
Intent chooser = Intent.createChooser(wikiIntent, getString(R.string.launch_browser));
startActivity(chooser);
}
});
}
}
Based on the code in your xml file on github following changes need to be made to achieve the desired effect :
<LinearLayout
android:id="#+id/container_buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:layout_below="#+id/relativeLayout">
.
.
.
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
<View
android:id="#+id/horizontal_divider1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_centerInParent="true"
android:background="#color/gray"
android:layout_toLeftOf="#+id/more_info_label"
android:layout_toStartOf="#+id/more_info_label"/>
<TextView
android:id="#+id/more_info_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="5dp"
android:text="#string/more_info"
android:textSize="18dp" />
<View
android:id="#+id/horizontal_divider2"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_centerInParent="true"
android:background="#color/gray"
android:layout_toRightOf="#+id/more_info_label"
android:layout_toEndOf="#+id/more_info_label"/>
</RelativeLayout>
I want to call the LocationActivity when the user click on the button and get the location information and set it into my button.
xml file
<Button
android:id="#+id/locationTxt"
android:text="#string/setLocation"
android:textStyle="bold"
android:layout_weight="0.5"
android:layout_width="8dp"
android:layout_height="40dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:editable="false"
android:gravity="center"
android:background="#drawable/btn_shape" />
Java file
public class AddTask extends android.app.Fragment {
Button btnLocation;
public AddTask()
{
// Constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_task, container, false);
btnLocation = (Button) v.findViewById(R.id.locationTxt);
btnLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I am very new to this.
How do I click a text and get it to open up an image which is saved in the app?
This is what I have so far, but I am having errors, I am using Fragments and I do not know if I am doing it the right way.
Thanks!
This is what I have now in Fragment_6.java :
package com.rufflez.swipeytabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment_6 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_6, container, false);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://androidcookbook.com/seam/resource/graphicImage/escapedn.png";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
}
in fragment_6.xml :
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:clickable="true"
android:text="#string/procedures1"
/>
Change to
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v =inflater.inflate(R.layout.fragment_6, container, false);
// inflate the layout
TextView tv = (TextView) v.findViewById(R.id.textView1);
// initialize textview using inflated view object
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://androidcookbook.com/seam/resource/graphicImage/escapedn.png";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
reuturn v; // return view
}
Hi Im a newbie in android, can some help me implement a click on my ImageView to open new activity. I tried several codes but the app crashes on launch. Here is my code on fragments
Fragment1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:fontFamily="sans-serif-light"
android:paddingBottom="15dp"
android:paddingTop="8dp" >
<ImageView
android:id="#+id/selectone"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#drawable/fashion"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/selecttwo"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#+id/selectone"
android:layout_marginTop="8dp"
android:background="#drawable/lingerie"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/titlelabel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/selectone"
android:background="#80000000"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="Fashion"
android:textColor="#fefefe"
android:textSize="28sp"
android:textStyle="italic" />
<TextView
android:id="#+id/titlelabel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/selecttwo"
android:background="#80000000"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="Lingerie"
android:textColor="#fefefe"
android:textSize="28sp"
android:textStyle="italic" />
</RelativeLayout>
Fragment1.java
package com.androidbegin.sidemenututorial;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment1 extends SherlockFragment {
ImageView fashionImg;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the button view
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
//set a onclick listener for when the button gets clicked
fashionImg.setOnClickListener(new OnClickListener() {
//Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(), CarouselActivity.class);
startActivity(mainIntent);
}
});
}
}
As shown below add code into onActivityCreated() instead of onCreate()
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// get the button view
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
// set a onclick listener for when the button gets clicked
fashionImg.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
CarouselActivity.class);
startActivity(mainIntent);
}
});
}
try getApplicationContext() instead of getActivity()
You need to write your Fragment1 class like this :
public class Fragment1 extends SherlockFragment {
ImageView fashionImg;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
//get the button view
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
return rootView;
}
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(), CarouselActivity.class);
startActivity(mainIntent);
}
}
You should change this by:
fashionImg = (ImageView) getView().findViewById(R.id.selectone);
to
fashionImg = (ImageView) getActivity().findViewById(R.id.selectone);
First, replace android:onClick="onClick" in the ImageView with something like android:onClick="onClickTest". Then in the code write something like:
public void onClickTest(View v){
//The MainActivity being the starting point
Intent int = new Intent(MainActivity.this,
CarouselActivity.class);
MainActivity.this.startActivity(mainIntent);
}
add the listener in onCreateView. Refer Fragment lifecycle, the problem is onCreate is called before onCreateView so your fragment's view is not added to the parent container view by that time.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
//get the button view
fashionImg = (ImageView) rootView.findViewById(R.id.selectone);
//set a onclick listener for when the button gets clicked
fashionImg.setOnClickListener(new OnClickListener() {
//Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(), CarouselActivity.class);
startActivity(mainIntent);
}
});
return rootView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Try the following, first place an ImageView like this
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"/>
Then place all other ImageView or images over this.
In your 'Fragment1' class use OnTouchListener.
image1 = (ImageView) findViewById(R.id.imageView1);
image1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Intent mainIntent =new Intent(Fragment1.this,CarouselActivity.class);
stratActivity(mainIntent);
return true;
}
});