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;
}
}
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 to receive clicked list number. When I use getcheckeditemposition() method of ListView, return number is always 1. I don't know why. I have to solve this problem.
This is my code:
ChoiceDialogFragment.java
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import aa.SendFragment;
public class ChoiceEmotionDialogFragment extends DialogFragment {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> data = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
data.add("a");
data.add("b");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);
final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dismiss();
}
});
final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("checkedNum", listView.getCheckedItemCount()+"");
}
});
listView = (ListView) view.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
listView.setAdapter(adapter);
return view;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
android:text="choice"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dialog_title">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" />
<TableRow>
<Button
android:id="#+id/dialog_button_cancel"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#null"
android:text="cancel" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#90909090" />
<Button
android:id="#+id/dialog_button_confirm"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#null"
android:text="confirm" />
</TableRow>
</TableLayout>
</RelativeLayout>
you have to set the item as checked on clicking the item
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listView.setItemChecked(position, true);
}
});
Existing questions
You can use setOnItemClickListener to get selected item first, and store it to a global variable;
int selectedItemPosition;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);
final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dismiss();
}
});
final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Access the selected item here
Log.d("checkedNum", selectedItemPosition+"");
}
});
listView = (ListView) view.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Store the position
selectedItemPosition = position;
}
});
return view;
}
I list dynamically string values from an ArrayList into EditTexts with a for loop, and ImageButtons are next to every EditText. I want this: when I click an ImageButton, the corresponsive EditText be editable.
Here is my code from the activity:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View showProduct = layoutInflater.inflate(R.layout.row_show_product, null);
for (int i = 0; i < product.size(); ++i) {
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product);
edt_row_show_product.setText(product.get(i));
ImageButton ib_row_show_product_edit_icon = (ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon);
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//container.getParent()...;
}
});
container.addView(showProduct);
}
I made a quick app to make your requirement work. Please take a look.
I had the code working in a Fragment so if you are working in an Activity please make the required changes.
Fragment Code:
package viewtest.myapplication;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public FragVisibilityInterface mInterface = null;
private LinearLayout showProduct, editTextsLL;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mInterface = new FragVisibilityInterface() {
#Override
public void toggleFragVisibility() {
}
};
}
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Button testBtn = (Button) rootView.findViewById(R.id.testbutton);
/*testBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideFrag();
}
}); */
String [] myArray = getResources().getStringArray(R.array.populate_array);
ArrayList<String> product = new ArrayList<>(Arrays.asList(myArray));
for (int i = 0; i < product.size(); ++i){
View showProd = createNewTextView(product.get(i));
LinearLayout editTextLL = (LinearLayout) rootView.findViewById(R.id.editTextsLL);
editTextLL.addView(showProd);
showProd = null;
}
return rootView;
}
/*private void hideFrag() {
SecondActivityFragment secFrag = new SecondActivityFragment();
getFragmentManager().beginTransaction().add(R.id.fragment, secFrag, "SecFrag").commit();
getFragmentManager().beginTransaction().hide(this).commit();
}*/
private View createNewTextView(String text) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View showProduct = layoutInflater.inflate(R.layout.edit_text_layout, null);
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.editText);
edt_row_show_product.setText(text);
Button ib_row_show_product_edit_icon =(Button) showProduct.findViewById(R.id.button);
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edt_row_show_product.requestFocusFromTouch();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_row_show_product, InputMethodManager.SHOW_IMPLICIT);
}
});
return showProduct;
}
public interface FragVisibilityInterface{
public void toggleFragVisibility();
}
}
EditText and Button Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
/>
</LinearLayout>
Main Fragment Layout
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<TextView
android:id="#+id/textview"
android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/testbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textview"
android:text="Button"/>
<LinearLayout
android:id="#+id/editTextsLL"
android:layout_below="#id/testbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
The code may be a little rough around the edges as it was made real quick. Please make relevant changes. Hope this helps! :)
You can set set the tag of the imageview with the corresponding EditText and get the tag in the onClick
final EditText edt_row_show_product = (EditText) showProduct.findViewById(R.id.edt_row_show_product);
edt_row_show_product.setText(product.get(i));
ImageButton ib_row_show_product_edit_icon =(ImageButton)showProduct.findViewById(R.id.ib_row_show_product_edit_icon);
ib_row_show_product_edit_icon.setTag(edt_row_show_product); //set the Edittext object here above
ib_row_show_product_edit_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText)v.getTag()
//do what ever you want with the EditText
}
});
I've looked at most of the other threads but I can't figure out what is causing my application to crash. I keep getting "attempt to invoke virtual method Android.view.View android.view.View.findViewById(int) on a null reference object. This error occurs at line 25 or
"View ObjectView = inflater.inflate(R.layout.fragment_layout_biking,container,false); "
How do you inflate a view for a newly created object in Android?
Thank you.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)getView().findViewById(R.id.input);
final ScrollView scroll = (ScrollView)getView().findViewById(R.id.scrolling);
Button addbox = (Button)getView().findViewById(R.id.CheckBoxAdd);
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
for (int box = 0; box <25; box++){
CheckBox newcheckbox = (CheckBox)getView().findViewById(R.id.NewCheckBox);
newcheckbox.setText(input1.toString());
scroll.addView(newcheckbox);
}
}
});
return ObjectView;
}
}
Here is the xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout"
android:orientation="vertical" >
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<Button
android:id="#+id/CheckBoxAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Checkbox" />
<ScrollView
android:id="#+id/scrolling"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout2"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
getView returns the View you inflated in onCreateView, before it returns null. In your code you have two options, the first is to use ObjectView.findViewById, to look for the view you need. The latter is to override onViewCreated, and use the first parameter, which in turn is the view you returned in onCreateView, to look for the view you need
Here is the final code that produces the desired result.
package fragments_for_app;
import android.app.adventureprototype.R;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class Fragment1 extends Fragment {
LinearLayout layout;
CheckBox newcheckbox;
//18
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)ObjectView.findViewById(R.id.input);
layout = (LinearLayout)ObjectView.findViewById(R.id.linearlayout2);
//final ScrollView scroll = (ScrollView)ObjectView.findViewById(R.id.scrolling);
final Button addbox = (Button)ObjectView.findViewById(R.id.CheckBoxAdd);
layout.removeAllViews();
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
newcheckbox = new CheckBox(ObjectView.getContext());
newcheckbox.setText(input1.getText().toString());
layout.addView(newcheckbox);
//work on this thursday
}
});
return ObjectView;
}
}
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
}