I have an MainActivity that basically has a framelayout that has different fragments sitting it in depending on what the user is doing.
activity_main.xml
<LinearLayout 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"
android:orientation="vertical" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#27b"
android:layout_weight=".04">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main_framelayout">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".9"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1B5F96"
android:layout_weight=".9"
android:id="#+id/activity_main_status_title"
android:text="#string/activity_main_status_title"
tools:ignore="NestedWeights" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AD3333"
android:layout_weight=".15"
android:id="#+id/activity_main_status_value"
android:text="#string/activity_main_status_value"/>
</LinearLayout>
</LinearLayout>
The problem starts with an interface method void WillYouAccept(final String caller)
#Override
public void WillYouAccept(final String caller) {
Connection f = new Connection();
ShiftView(f);
runOnUiThread(new Runnable() {
#Override
public void run() {
Connection b = (Connection) getFragmentManager().findFragmentById(R.layout.connection_display);
b.updateInitiator(caller);
}
});
}
The line b.updateInitiator(caller); is what initiates the null object reference error. ShiftView has basically been swapping out fragments into the framelayout for me, and so far has worked well.
#Override
public void ShiftView(Object obj) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.activity_main_framelayout, (Fragment) obj);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
Basically a textview in connection_display.xml(Connection.java) needs to be updated from the MainActivity, and somehow initiator = (TextView) view.findViewById(R.id.textView_connection_display_initiator_ID); is null when it is being interacted with from MainActivity.
Connection.java
package "";
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Connection extends Fragment {
MiddleMan mCallBack;
Button accept;
Button deny;
TextView initiator;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallBack = (MiddleMan) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement ReqestConnect");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.connection_display, container, false);
mCallBack.DisplayHome();
initiator = (TextView) view.findViewById(R.id.textView_connection_display_initiator_ID);
accept = (Button) view.findViewById(R.id.button_connection_display_ACCEPT);
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonAccept();
}
});
deny = (Button) view.findViewById(R.id.button_connection_display_DENY);
deny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonDeny();
}
});
return view;
}
private void buttonAccept() {
System.out.println("Accept Button Pressed");
}
private void buttonDeny() {
System.out.println("Deny Button Pressed");
}
public void updateInitiator(final String s) {
initiator.setText(s);
}
}
connection_display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/textView_connection_display_title"
android:id="#+id/textView_connection_display_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_marginTop="45dp"
android:layout_weight=".004">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/textView_connection_display_ID_label"
android:id="#+id/textView_connection_display_ID_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView_connection_display_initiator_ID"
android:text="#string/textView_connection_display_initiator_ID" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight=".004">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/textView_connection_display_exp"
android:id="#+id/textView_connection_display_exp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight=".04">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="#string/button_connection_display_ACCEPT"
android:id="#+id/button_connection_display_ACCEPT"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_connection_display_DENY"
android:id="#+id/button_connection_display_DENY"
tools:ignore="ButtonStyle" />
</LinearLayout>
</LinearLayout>
b was definitely turning up as null, however the bit above had always found the fragment, so I solved it with this:
#Override
public void WillYouAccept(final String caller) {
final Connection f = new Connection();
ShiftView(f);
runOnUiThread(new Runnable() {
#Override
public void run() {
f.updateInitiator(caller);
}
});
}
Related
knowing that there is no errors, only MainActivity shows without homebutton................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
MainActivity.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etfirstvalue,etsecondvalue;
Button btnadd,btnsubs,btnmultiply,btndivide;
Double num1,num2;
TextView tvresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfirstvalue=findViewById(R.id.etfirstvalue);
etsecondvalue=findViewById(R.id.etsecondvalue);
btnadd=findViewById(R.id.btnadd);
btndivide=findViewById(R.id.btndivision);
btnmultiply=findViewById(R.id.btnmultiply);
btnsubs=findViewById(R.id.btnsubs);
tvresult=findViewById(R.id.tvresult);
Clicklistener();
}
public void Clicklistener(){
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1+num2;
tvresult.setText(String.valueOf(result));
}
});
btnsubs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1-num2;
tvresult.setText(String.valueOf(result));
}
});
btnmultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1*num2;
tvresult.setText(String.valueOf(result));
}
});
btndivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1/num2;
tvresult.setText(String.valueOf(result));
}
});
}
}
MainActivity.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"
android:background="#FFFFFF"
android:backgroundTint="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Simple Calculator"
android:textSize="25sp" />
<EditText
android:id="#+id/etfirstvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter First Value"
android:inputType="number" />
<EditText
android:id="#+id/etsecondvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Second Value"
android:inputType="number" />
<TextView
android:id="#+id/tvresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Result"
android:textColor="#color/purple_500"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="ADD" />
<Button
android:id="#+id/btnsubs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Subs" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnmultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Multiply" />
<Button
android:id="#+id/btndivision"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Divide" />
</LinearLayout>
</LinearLayout>
homebutton.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class homebutton extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homebutton);
Button btncalc=findViewById(R.id.btncalc);
btncalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(homebutton.this, MainActivity.class);
startActivity(intent);
}
});
}
}
homebutton.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=".homebutton"
tools:ignore="ExtraText">
<Button
android:id="#+id/btncalc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Calculator" />
</LinearLayout>
You need to clarify your question more. However I believe you meant homebutton should start first and after you click calculate it should take you to the second one.
If that's the case, then you need to define your start activity from the AndroidManifest because currently your app is starting on MainActivity.java
In one of my fragment class, it has two buttons (A and B) on top, used to switch to another fragment when clicked using viewPager.
When buttonA is clicked, it should switch to page A and so on.
When I click button A, how can I make the text in A show below the two buttons?
fragment_menu.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">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="ButtonA"
android:textSize="12sp" />
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_toRightOf="#+id/buttonB"
android:backgroundTint="#color/materialGrey600"
android:padding="15dp"
android:text="ButtonB"
android:textSize="12sp" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MenuFragment
public class MenuFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_menu, container, false);
final ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getActivity().getFragmentManager()));
Button btnA = (Button) v.findViewById(R.id.buttonA);
Button btnB = (Button) v.findViewById(R.id.buttonB);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(0, true);
}
});
btnB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(1, true);
}
});
return v;
}
}
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return new A()
case 1:
return new B()
}
return null;
}
#Override
public int getCount() {
return 2;
}
}
Output
try with below code.hope it is helpful for you ;)
P.S. You just need to set button style as you want :)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_above="#id/llButton"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/llButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp">
<Button
android:id="#+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="15dp"
android:text="ButtonA"
android:textSize="12sp" />
<Button
android:id="#+id/buttonB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:backgroundTint="#color/colorAccent"
android:padding="15dp"
android:text="ButtonB"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
Since you are using Relative Layout you can add layout_below
<androidx.viewpager.widget.ViewPager
android:layout_below="#id/buttonB"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
you are doing it wrong, You cannot align a button to itself. That's why your buttons are not working properly. In your button B do this:
android:layout_toRightOf="#+id/buttonA"
instead of this:
android:layout_toRightOf="#+id/buttonB"
And to your viewPager add "android:layout_below:"
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/buttonA" />
You have to set your layout like this....
[I think you want something like this 2 button fisrt and then viewpager for change the content ][1]
[1]: https://i.stack.imgur.com/dluXA.png
**Code for main Page **
<?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"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/frame_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<Button
android:id="#+id/btn_donner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#drawable/rect_outline"
android:text="Donor's"
android:textAlignment="center"
android:textColor="#color/white" />
<Button
android:id="#+id/btn_blood_bank"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Blood Bank"
android:background="#drawable/rect_outline"
android:layout_weight="2"
android:textAlignment="gravity"
android:textColor="#color/white" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
Code For Java File of two Buttons And ViewPager
//Click Event for donor's and blood bank .....
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment = null;
if (view == view.findViewById(R.id.btn_donner))
{
fragment = new TabDonnerFragment();
}
else
{
fragment = new TabBloodBankFragment();
}
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame_content, fragment);
transaction.commit();
}
};
btn_donner.setOnClickListener(listener);
btn_blood_bank.setOnClickListener(listener);
return root;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment fragment = null;
fragment = new TabDonnerFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame_content, fragment);
transaction.commit();
}
}
I have built an app, which inclues an activity with two fragments. The top fragment is supposed to get two pieces of information (name and age) and insert them into a TextView in the bottom fragment. However, when I run the app, I get a white screen. Logcat shows no errors.
I added the xml files maybe the problem is there.
public class userActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener {
#Override
public void createName(String name, int age) {
BottomFragment bottomFragment= (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setName(name,age);
}
public class TopSectionFragment extends Fragment {
private static EditText name,age;
TopSectionListener activityCommander;
public interface TopSectionListener {
public void createName(String name,int age);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityCommander=(TopSectionListener)context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment,container,false);
name= (EditText)view.findViewById(R.id.name);
age = (EditText)view.findViewById(R.id.age);
final Button save = (Button) view.findViewById(R.id.save);
save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
saveClicked(v);
}
}
);
return view;
}
public void saveClicked(View view) {
activityCommander.createName(name.getText().toString(),Integer.getInteger(age.getText().toString()));
}
}
public class BottomFragment extends Fragment {
private static TextView list;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.bottom_fragment,container,false);
list = (TextView)view.findViewById(R.id.list);
return view;
}
public void setName(String name,int age){
list.setText(name + " " + age);
}
}
activity-
<?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"
android:orientation="vertical"
android:background="#FF4F87BC"
tools:context="com.example.student.familycommitment.userActivity">
<fragment
android:id="#+id/fragment"
android:name="com.example.student.familycommitment.TopSectionFragment"
android:layout_width="match_parent"
android:layout_height="336dp"
tools:layout="#layout/top_section_fragment" />
<fragment
android:id="#+id/fragment2"
android:name="com.example.student.familycommitment.userList$BottomFragment"
android:layout_width="match_parent"
android:layout_height="392dp"
tools:layout="#layout/bottom_fragment" />
</LinearLayout>
bottom-
<?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:orientation="vertical"
android:background="#FF4F87BC"
tools:context="com.example.student.familycommitment.userActivity">
<TextView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="228dp"
android:layout_marginTop="450dp"
android:fontFamily="serif"
android:gravity="start"
android:textColor="#FFFF"
android:textSize="20sp"></TextView>
</RelativeLayout>
top-
<?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"
android:orientation="vertical"
android:background="#FF4F87BC"
tools:context="com.example.student.familycommitment.userActivity">
<TextView
android:id="#+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center_horizontal"
android:text="Please enter your children's names and ages"
android:textColor="#FFFF"
android:fontFamily="sans-serif-black"
android:textSize="30sp"></TextView>
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/name"
android:hint="Name"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:background="#android:drawable/editbox_background_normal"
/>
<EditText
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/age"
android:hint="age"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="25sp"
android:background="#android:drawable/editbox_background_normal"
/>
<Button
android:id="#+id/save"
android:layout_width="134dp"
android:layout_height="34dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#android:drawable/editbox_dropdown_dark_frame"
android:fontFamily="monospace"
android:gravity="center_horizontal"
android:text="save"
android:textColor="#FFFF"
android:textSize="20sp" />
</LinearLayout>
My Android app is composed of an SQLite database which populates individual ListView items with data user saves. Those items are available for display in activity_main.xml.
I have a class called RecordsListFragment which contains the two problematic methods: onItemClick and onItemLongClick. Here is the class in its entirety:
package com.example.benignfella.projectworkinghoursapplication.Fragment;
import android.app.Activity;
import android.os.AsyncTask;
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.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import com.example.benignfella.projectworkinghoursapplication.R;
import com.example.benignfella.projectworkinghoursapplication.Adapter.RecordsListAdapter;
import com.example.benignfella.projectworkinghoursapplication.Database.RecordsDAO;
import com.example.benignfella.projectworkinghoursapplication.GetSet.Records;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
public class RecordsListFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener {
public static final String ARGUMENT_ITEM_ID = "records_list";
Activity activity;
ListView recordsListView;
ArrayList<Records> records;
RecordsListAdapter recordsListAdapter;
RecordsDAO recordsDAO;
private GetRecordsTask task;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
recordsDAO = new RecordsDAO(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_records_list, container, false);
findViewsById(view);
task = new GetRecordsTask(activity);
task.execute((Void) null);
return view;
}
private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Records record = (Records) parent.getItemAtPosition(position);
if (records != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedRecord,", record);
CustomRecordsDialogFragment customRecordsDialogFragment = new CustomRecordsDialogFragment();
customRecordsDialogFragment.setArguments(arguments);
customRecordsDialogFragment.show(getFragmentManager(), CustomRecordsDialogFragment.ARGUMENT_ITEM_ID);
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Records records = (Records) parent.getItemAtPosition(position);
recordsDAO.deleteRecord(records);
recordsListAdapter.remove(records);
return true;
}
public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> {
private final WeakReference<Activity> activityWeakRef;
public GetRecordsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
#Override
protected ArrayList<Records> doInBackground(Void... arg0) {
ArrayList<Records> recordsArrayList = recordsDAO.getRecords();
return recordsArrayList;
}
#Override
protected void onPostExecute(ArrayList<Records> empList) {
if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) {
records = empList;
if (empList != null) {
if (empList.size() != 0) {
recordsListAdapter = new RecordsListAdapter(activity, empList);
recordsListView.setAdapter(recordsListAdapter);
} else {
Toast.makeText(activity, "No Records about records... wait wot m8?",
Toast.LENGTH_LONG).show();
}
}
}
}
}
public void updateView() {
task = new GetRecordsTask(activity);
task.execute((Void) null);
}
public void onResume() {
getActivity().setTitle(R.string.app_name);
getActivity().getActionBar().setTitle(R.string.app_name);
super.onResume();
}
}
Here is activity_main.xml with FrameLayout:
<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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Layout resource file which contains a ListView is called fragment_records_list.xml and here it is:
<?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="#f9f9f9">
<ListView
android:id="#+id/list_records"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:drawSelectorOnTop="true"
android:footerDividersEnabled="false"
android:padding="10dp"
android:scrollbarStyle="outsideOverlay"/>
</RelativeLayout>
Lastly, there is a resource file containing the layout of a single item, list_item.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="#ededed"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:id="#+id/layout_item"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_record_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="ID"
android:textSize="20dp"/>
<TextView
android:id="#+id/text_record_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/text_record_id"
android:padding="5dp"
android:text="Date"
android:textColor="#00942b"
android:textSize="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/text_record_date"
android:drawableStart="#drawable/ic_date_range_black_24dp"
android:padding="5dp"
/>
<TextView
android:id="#+id/text_record_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_id"
android:padding="5dp"
android:text="Description"
/>
<TextView
android:id="#+id/text_record_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:padding="5dp"
android:text="17:00"
android:textSize="16dp"
android:textColor="#004561"
/>
<TextView
android:id="#+id/text_record_dash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_start"
android:padding="5dp"
android:text="-"
android:textSize="16dp"
/>
<TextView
android:id="#+id/text_record_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_dash"
android:padding="5dp"
android:text="20:00"
android:textSize="16dp"
android:textColor="#c7002a"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_below="#id/text_record_description"
android:layout_toRightOf="#id/text_record_finish"
android:drawableStart="#drawable/ic_timer_black_24dp"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/layout_item"
android:background="#000000"
/>
</RelativeLayout>
I haven't found a definite answer to my question, so I'm asking for a bit of help here.
You need set OnItemClickListener and OnLongItemClickListener to ListView, I edited your method in the initializing variable ListView:
private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
recordsListView.setOnItemClickListener(this);
recordsListView.setOnItemLongClickListener(this);
}
This part of the code is to add 3 EditText lines while the button secbutt is clicked. While all three EditText lines are on the screen, the button should disappear.
Now I do not understand why after entering onClick method and starting plusTextField method, it just adds EditText lines infinitely. I want it to execute once and to return to the beginning of summonButton method. What should I do?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
int numberOfLinesLeft = 3;
Button secondaryActivityAddButton;
LinearLayout llForSecondaryButton;
LinearLayout llForSecondaryEditText;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void summonButton(View view){
llForSecondaryButton = findViewById(R.id.secondaryButton);
secondaryActivityAddButton = new Button(this);
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
llForSecondaryButton.addView(secondaryActivityAddButton);
secondaryActivityAddButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
plusTextField();
}
});
}
public void plusTextField() {
llForSecondaryEditText = findViewById(R.id.linearLayout1);
// add edittext
et = new EditText(this);
et.setText("text" + numberOfLinesLeft);
llForSecondaryEditText.addView(et);
numberOfLinesLeft--;
}
}
ActivityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.egrishin.task_a_day.MainActivity">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/main_task_title"
android:gravity="center_horizontal"
/>
<EditText
android:id="#+id/main_task_line_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="What is 1 main thing to be done today?"
android:inputType="textMultiLine"
android:layout_margin="16dp"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout0"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/textline2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/secondary_task_title"
android:gravity="center_horizontal"
android:onClick="summonButton"
/>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp"
>
</LinearLayout>
<LinearLayout
android:id="#+id/secondaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<TextView android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/other_task_title"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
You miss logic of setting the button invisible when counter riches 0 :
public void onClick(View view) {
plusTextField();
if(numberOfLinesLeft == 0) {
view.setVisibility(View.GONE);
}
}
Here is the result, there is no button after adding 3 Edit Texts: