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
}
Related
I want to set an onClickListener on a button, but I am facing a problem.
I can't get the context I need after the button is clicked.
I tried by view but didn't work, it doesn't work but also doesn't throw an error.
The xml file in fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="go to test" />
</RelativeLayout>
Java fragment code :
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.util.Objects;
public class Homefragment extends android.support.v4.app.Fragment {
Button test;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
test = (Button) view.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Clicked go to test");
Toast.makeText(getContext(), "clicked", Toast.LENGTH_SHORT).show();
Intent gotest;
gotest = new Intent(getActivity().getApplicationContext(),testing.class);
startActivity(gotest);
}
});
return inflater.inflate(R.layout.fragment_home,null);
}
}
My logcat gives no error and I use print command when the button is clicked but print does not work, like if the button is not clicked.
Use this return your view.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
test = (Button) view.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Clicked go to test");
Toast.makeText(getContext(), "clicked", Toast.LENGTH_SHORT).show();
Intent gotest;
gotest = new Intent(getActivity().getApplicationContext(),testing.class);
startActivity(gotest);
}
});
return view;
}
I Have a Fragment, and i want to add elements (textview, button) dynamically when i click on Floating Action Button.
Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.xyz, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// new elements on click
// new elements on click
}
});
return view;
}
try this xml code for Fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="10dp"
app:srcCompat="#android:drawable/ic_dialog_email"/>
</FrameLayout>
And java code for Fragment
package com.example.androiddeveloper.fragmentdynamic;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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.LinearLayout;
/**
* A simple {#link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
private LinearLayout linearLayout = null;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank2, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Create your Controls(UI widget, Button,TextView) and add into layout
Button btn = new Button(getActivity());
btn.setText("Manual Add");
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(btn);
}
});
}
}
You could do it like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.xyz, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View view = YouFragmentClass.this.getView(); // returns base view of the fragment
if (view != null&&(view instanceof ViewGroup)){
//set the properties for button
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("Button");
ViewGroup viewGroup = (ViewGroup) view;
viewGroup.addView(btn);
}
}
});
return view;
}
I havent tested it but it should do the job
I am trying to read in contents of w_number_edit_text(a textbox) after the login(a button) is clicked. But on the OnClick method I can't seem to access the contents of w_number_edit_text. Both the textbox and the button is on the fragment xml instead of main.xml. Can anyone tell me how to fix this problem. In short how do I access contents of EditText after the Button is clicked? Thank You
My code is below:
package com.example.medrec;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener{
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
//////////////////////////// Problem is here
EditText wNumbers =(EditText)findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
}
}
My activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.medrec.MainActivity"
tools:ignore="MergeRootFrame" />
My fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.medrec.MainActivity$PlaceholderFragment" >
<EditText
android:id="#+id/w_number_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="#string/w_number" />
<Button
android:id="#+id/w_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/w_number_edit_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="#string/w_login_text" />
</RelativeLayout>
You should change this
EditText wNumbers =(EditText)findViewById(R.id.w_number_edit_text);
With
EditText wNumbers =(EditText)rootview.findViewById(R.id.w_number_edit_text);
and onCreateView(......) like a below:
EditText wNumbers
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
login.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
Try this..
You cannot use this inside Fragment you have to use getActivity()
View rootView ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
Initialise EditText on onCreateView :-
public static class PlaceholderFragment extends Fragment implements OnClickListener{
public PlaceholderFragment()
{
}
EditText wNumbers
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
Define rootview in the class.
and detect your EditText using rootView.findviewbyid like below,
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
And the onCreateView() code is as below,
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
//////////////////////////// Problem is here
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
I am working on a simple app to convert the temperature. I am trying to figure out how to process a number that a user inputs and then process it and give out the conversion. I searched different places and found part of the following code but my app crashes when I actually click on the button.
import android.os.Bundle;
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.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.w3c.dom.Text;
public class CelsiusFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.celsiusfragment, container, false);
Button celsiusbutton = (Button) view.findViewById(R.id.button_celsius);
celsiusbutton.setOnClickListener(this);
return view;
}
Button mButton;
EditText mEdit;
TextView mText;
int output;
#Override
public void onClick(View view) {
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int number = 0;
int number1 = number;
output = number1 + 2;
TextView result = (TextView) view.findViewById(R.id.result);
result.setText(String.valueOf(output));
}
});
}
}
Is there a tutorial that you would recommend to accomplish this?
Thanks!
Try to change your code like this:
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/calculate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center_horizontal" />
</LinearLayout>
CelsiusFragment.java
public class CelsiusFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Button calculateButton = (Button) view.findViewById(R.id.calculate_button);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView result = (TextView) view.findViewById(R.id.result);
EditText value = (EditText) view.findViewById(R.id.value);
int output = Integer.parseInt(value.getText().toString()) + 2;
result.setText(String.valueOf(output));
}
});
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;
}
});