How to view data from database (REST) - android

Bottom navigation SSI already done bottom Navigation in my app using android studio. So, I would like to create a button in one of my fragments. How can i do it? Any video references? Thank you.

It's so easy not much harder just put your button in your fragment layout then after creating the instance in your fragment and put listener for that particular button like bellow.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
android:id="#+id/mBtnPresent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
#SuppressLint("ValidFragment")
public class FragmentTest extends Fragment {
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppCompatButton mBtnPresent=view.findViewById(R.id.mBtnPresent);
mBtnPresent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform action what you want on button click
}
});
}
}

Related

List of fragment in android

I know that to show a list of views I can use recycler view with but what if I wanna reuse also my fragment logic too?
for example, if I have a fragment like this
XML
<androidx.constraintlayout.widget.ConstraintLayout 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=".views.fragments.testview">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hi"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment class
public class TestView extends Fragment {
private TextView txt;
#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.test, container, false);
initComponent(v);
return v;
}
private void hardTask1(){};
private void hardTask2(){};
//Some other hardtask
}
What I could do if I need to display a list of fragments in some activity but I don't want to rewrite all my TestView in the adapter?

View centered on Preview but not on device

I am adding a fragment to an activity and it's layout is very simple:
<?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:gravity="center_vertical"
android:padding="16dp">
<Button
android:id="#+id/fragment_pay_request_pbutton"
style="#style/ButtonsStyle"
android:text="Pay" />
<Button
android:layout_marginTop="16dp"
android:id="#+id/fragment_pay_request_rbutton"
style="#style/ButtonsStyle"
android:layout_below="#id/fragment_pay_request_pbutton"
android:text="Request" />
<Button
android:layout_marginTop="16dp"
android:id="#+id/fragment_pay_request_bbutton"
style="#style/ButtonsStyle"
android:layout_below="#id/fragment_pay_request_rbutton"
android:text="Bills" />
</RelativeLayout>
And all is ok on the preview:
But on device:
I'm using Lolipop 5.1 and overlay mode for testing layouts.
Has someone an idea why it doesn't work and what I could do to solve this kind of problem.
BTW: The fragment is very simple:
/**
* Created by laurentmeyer on 15/03/15.
*/
public class PayOrRequestFragment extends BaseFragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View toReturn = inflater.inflate(R.layout.fragment_pay_request, container, false);
Button pay = (Button) toReturn.findViewById(R.id.fragment_pay_request_pbutton);
Button request = (Button) toReturn.findViewById(R.id.fragment_pay_request_rbutton);
configureButton(request, pay);
return toReturn;
}
private void configureButton(Button request, Button pay) {
request.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] parameters = {"com.payment.laurentmeyer.mobilepayment.fragments.ReceiveFragment", "com.payment.laurentmeyer.mobilepayment.fragments.RequestMethodsFragment"};
mCallbacks.createSliderWithParameters(parameters);
}
});
pay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] parameters = {"com.payment.laurentmeyer.mobilepayment.fragments.SendFragment", "com.payment.laurentmeyer.mobilepayment.fragments.VerificationFragment"};
mCallbacks.createSliderWithParameters(parameters);
}
});
}
}
You must be putting the fragment into a view container in the Activity... You didn't post the layout for your Activity. Does your view container have android:layout_height="wrap_content"?

On android, when I set the Text of a TextView dynamically it doesn't change

I am new to the android platform so I know I am just missing something by here is what I have
<!-- activity_main -->
<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.example_app.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.example_app.MainFragment"
/>
</FrameLayout>
Then I created a fragment called MainFragment.java.
public class MainFragment extends Fragment {
public TextView displayTextView;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
return rootView;
}
}
I add the fragment in the OnCreate function in the activity and it seems to be adding everything okay but when I click on the button nothing happens, I logged the function to see its being called but setting the textview doesn't do anything. What am I doing wrong?
<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.example_app.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button10"
android:textColor="#ffffff"
android:textSize="34dp"
android:text="10%"
android:layout_below="#id/amountEditText"
android:background="#drawable/pct_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doSomething"
/>
<TextView
android:text="Blah Blah Blah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/displayTextView"
></TextView>
</RelativeLayout>
And here is the click Event in the MainActivity.java
public void doSomething(View v) {
this.mainFragment.displayTextView.setText("Nothing to set for some reason!");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
//
this.mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.mainFragment);
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(mainFragment, "mainFragment").commit();
}
}
Declaring onClick-Listeners via XML is possible if you work with Activities. According to the Google API, however, you need to declare OnClickListeners programmatically if you use Fragments.
[...] You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
Source:
http://developer.android.com/guide/topics/ui/controls/button.html
So you may want to use this instead:
public class MainFragment extends Fragment {
public TextView displayTextView;
public Button yourButton;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
yourButton = (Button)rootView.findViewById(R.id.button10);
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayTextView.setText("Nothing to set for some reason!");
}
});
return rootView;
}
}
The problem is the way you try to execute the button click. From your question i understand that Button and TextView is in Fragment view. So do why are Trying to access it in activity ?? It is wrong, then there is no point of using Fragments. Also this will lead to many serious issues later .
Fragments are Reusable components so all the functionalities of a fragment must stay inside the Fragment Class
Change Fragment code :
public class MainFragment extends Fragment {
public TextView displayTextView;
public Button yourButton;
public MainFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
this.displayTextView = (TextView)rootView.findViewById(R.id.displayTextView);
yourButton = (Button)rootView.findViewById(R.id.button10);
//Setting click listener for button in fragment
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayTextView.setText("Nothing to set for some reason!");
}
});
return rootView;
}
}
and remove android:onClick="doSomething" from XML

Add buttons to PreferenceFragment

I want to add a couple buttons to the bottom of my preferences screen for setting defaults and restoring defaults. This answer doesn't cover how to do this using PreferenceFragment. What is the recommended way to accomplish this.
Activity that loads the preferences fragment:
public class SettingsActivity extends Activity {
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
// load up the preferences fragment
getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit();
}
}
PreferenceFragment implementation:
public class PrefsSettingsFragment extends PreferenceFragment {
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
addPreferencesFromResource( R.xml.preferences);
}
}
preferences.xml:
<?xml version="1.0"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference android:key="edit_text_preference_server_address" android:title="#string/preference_server_address"/>
<SwitchPreference android:key="switch_preference_bat" android:title="#string/preference_bat"/>
<SwitchPreference android:key="switch_preference_comm" android:title="#string/preference_comm"/>
<SwitchPreference android:key="switch_preference_dev_mode" android:title="#string/preference_dev_mode" android:defaultValue="true"/>
</PreferenceScreen>
I meet the same question, and I found a way to handle this issue.
Overwrite the method onCreateView in PreferenceFragment, using the given the LayoutInfalter by parameters to create your own View, and return this view.
It's my code. I hope this can be helpful
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.set_alarm, null);
Button save = (Button)v.findViewById(R.id.save);
Button revert = (Button)v.findViewById(R.id.revert);
Button delete = (Button)v.findViewById(R.id.delete);
save.setOnClickListener(this);
revert.setOnClickListener(this);
delete.setOnClickListener(this);
if(mId == -1){
delete.setEnabled(false);
}
return v;
}
I modified the previous post a little bit, so that the button will get attached at the bottom of the view.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);
Button btn = new Button(getActivity().getApplicationContext());
btn.setText("Button on Bottom");
v.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return v;
}
Just create your own layout for the settings activity which contains list view with id #android:id/list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<Button
android:id="#+id/button"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
And then in the activity class set the content view before adding preference fragment
public class SettingsActivity extends Activity {
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
setContentView(R.layout.settings_activity)
// load up the preferences fragment
getFragmentManager().beginTransaction().replace( android.R.id.content, new PrefsSettingsFragment()).commit();
}
}
you may try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);
Button SendLogbtn = new Button(getActivity().getApplicationContext());
SendLogbtn.setText("send log file");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(100, 0, 0, 500);
SendLogbtn.setLayoutParams(params);
v.addView(SendLogbtn);
SendLogbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do your code
}
});
return v;
}

Fragment.onCreateView has null container

The following is running on an Android 1.6 so I'm using the compatibility package for fragments. In the following TestFragment is a static nested class:
public class FragmentTestActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView result = new TextView(getActivity());
result.setText("Hello TestFragment");
return result;
}
}
}
The main.xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<fragment class="com.test.FragmentTestActivity$TestFragment"
android:id="#+id/test"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
The strange thing is that the container parameter in onCreateView is null.
Now, if I add the fragment programatically like so(just change the onCreate method of the Activity) the container is no longer null. Why?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment frag = new TestFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
}
The documentation mentions that it can be null:
public View
onCreateView(LayoutInflater
inflater, ViewGroup container, Bundle savedInstanceState)
[...]
container: If non-null, this is the parent view that the
fragment's UI should be attached to. The fragment should not add the
view itself, but this can be used to generate the LayoutParams of the
view.
To be clear: you shouldn't do anything like container.addView(...).

Categories

Resources