How do i call LocationActivity from button? - android

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) {
}
});

Related

How To Call a page using button in Fragment in Android?

public class Menu1 extends Fragment implements View.OnClickListener {
public static Button button_sbm;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//returning our layout file
//change R.layout.yourlayoutfilename for each of your fragments
/* return inflater.inflate(R.layout.fragment_menu_1, container, false);*/
View myView = inflater.inflate(R.layout.fragment_menu_1, container, false);
button_sbm = (Button) myView.findViewById(R.id.button);
button_sbm.setOnClickListener(this);
return myView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("Home");
}
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainWeb.class);
startActivity(intent);
}
}
How To Intent To MainWeb Class..
This My XML
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/yoga"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="Goto Website"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Fragment to Activity
What to do?
When I click the button, it gives me a message
"Unfortantly App Was Stopped"
Make sure your MainWeb Activity class is mentioned in AndroidManifest.xml.
Don't create a static instances of Button or any UI widget in your fragment or Activity. That may lead to memory leak.

Android studio EditText doesnt load TextView

Im working in a fragment where I want the user to input an answer and then when clicking a button, a textview should show Correct.
This is my code
public class FragmentOne extends Fragment {
private EditText userAnswer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final String theAnswer = (userAnswer.getText().toString());
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (theAnswer.equalsIgnoreCase("Germany")) {
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
}
});
return v;
}
}
However, when Germany is typed, the textView is still just showing 'x' (which I set it to show originally)
Relevant XML's are
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/submitBtn1"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="220dp"
android:layout_height="70dp"
android:inputType="textPersonName"
android:hint="Write answer and press enter"
android:ems="10"
android:id="#+id/editText"
android:textSize="16sp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/showCorrect"
android:layout_below="#+id/skipBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
What has gone wrong?
You have to change this line :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
tv.setText("Correct!");
}
}
});
return v;
}
i.e : in your code userAnswer value is always the same (not checked on Button click)
Code needed to be changed to:
public class FragmentOne extends Fragment {
private EditText userAnswer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect); //changed
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
//TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
// updateScore();
}
});
return v;
}

How to replace text of textView in fragment

I want to change a text of TextView on clicking a button. I have 2 buttons in my fragment and both have an onClick method for same TextView. Here is in detail what's happen in my fragment.
I set a default text to TextView means 1st text.
When 1st button clicked the 2nd text will appear on the TextView.
When 2nd button clicked again 1st text will appear on the TextView.
Here is my Fragment code
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Rahul", "On Ganpati 1 fragment On Create ");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
// Inflate the layout for this fragment
final TextView textView = (TextView)getView().findViewById(R.id.g1TextView);
textView.setText(getString(R.string.Ganpati_1));
Button marathi = (Button)getView().findViewById(R.id.g1Marathi);
marathi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.Ganpati_1));
}
});
Button english = (Button)getView().findViewById(R.id.g1English);
english.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.GanpatiE));
}
});
return inflater.inflate(R.layout.fragment_ganpati_1, container, false);
}
Here is the XML file
<FrameLayout 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"
tools:context="com.example.lenovo.shopping.Shankar">
<Button
android:layout_width="168dp"
android:layout_height="50dp"
android:text="Left"
android:id="#+id/g1Marathi"
android:textAlignment="center"
android:layout_gravity="left|top" />
<Button
android:layout_width="168dp"
android:layout_height="50dp"
android:text="Right"
android:id="#+id/g1English"
android:textAlignment="center"
android:layout_gravity="right|top" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingBottom="50dp"
android:id="#+id/scrollView"
android:layout_gravity="end|fill_vertical">
<TextView
android:id="#+id/g1TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/TextView" />
</ScrollView>
</FrameLayout>
When use Fragment, you need to override onCreateView and return View like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ganpati_1, null);
Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
// Inflate the layout for this fragment
final TextView textView = (TextView)view.findViewById(R.id.g1TextView);
textView.setText(getString(R.string.Ganpati_1));
Button marathi = (Button)view.findViewById(R.id.g1Marathi);
marathi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.Ganpati_1));
}
});
Button english = (Button)view.findViewById(R.id.g1English);
english.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(getString(R.string.GanpatiE));
}
});
return view;
}

how to implemet "On click" for a button which is in custom list view

I have three buttons in a custom list view, and list view is in a fragment not in an activity, but when I set onclick listner . it throws exception null object reference passed.
custome_list_view.xml
<Button
android:text="#string/pickUpButtonText"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="#dimen/standard_hight"
android:background="#drawable/buttonshape"
android:shadowColor="#5BA84F"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:id="#+id/button_pickup_list"
/>
customeListView.Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jobs, container, false);
ArrayList<JobListDataProvider> listJobs = GetRequests();
ListView lv = (ListView)rootView.findViewById(R.id.listViewJobs);
lv.setAdapter(new JobsListAdapter(getActivity(), listJobs));
//.....bindind on click....
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//......................
}
});
return rootView;
}
you have to put this code in JobsListAdapter in public View onCreateView
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//......................
}
});

Accessing UI elements from fragment class

i have a class which extends fragment class as below:
public class FragmentCreateGroup extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ImageView group=(ImageView)findViewById(R.id.group_image);//shows error here
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_creategroup, container,false);
}
}
which is associated to the following xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/cr_group_grpname_desc"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/group_image"
android:layout_width="139dp"
android:layout_height="144dp"
android:src="#drawable/background" />
<EditText
android:id="#+id/cr_group_grpname_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:contentDescription="#string/cr_group_grpname_desc"
android:ems="10"
android:hint="#string/cr_group_grpname_hint"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/cr_group_creategrp_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:background="#drawable/greenbutton"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/cr_group_creategrp_btn_label" />
</LinearLayout>
I tried to access the ImageView in the fragment layout using findViewById(). Since I didn't extend Activity class I was unable to perform this action. Is there any way to access the UI Elements from a non activity class? if any how to perform?
You need to get the view after you inflate it in onCreateView
public class FragmentCreateGroup extends Fragment {
private ImageView group;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_creategroup, container, false);
group = (ImageView) v.findViewById(R.id.group_image);
group.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
group.setImageDrawable(); // set image here
}
});
return v;
}
}

Categories

Resources