View centered on Preview but not on device - android

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"?

Related

How to view data from database (REST)

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

onClickListener in Button doesn't work

I'm trying to display log value on the console, when I click on a Button inside a fragment. I don't know why but nothing happens when I click.
I tried by all possible ways (onClick directly on the layout, setOnClickListener extends OnClickListener, setting "clickable"=true).
Thank you for the help!
public class displayFacesFragment extends Fragment {
public static final String TITLE = "Faces";
public String selectedPeriod;
public Set<String> idFurnitures;
Button button75;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View inputFragmentView = inflater.inflate(R.layout.fragment_display_faces, container, false);
button75 =(Button) inputFragmentView.findViewById(R.id.button75);
button75.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Test", "onClickListener");
}
});
return inputFragmentView;
}
<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"
android:id="#+id/fragment_faces"
tools:context="com.example.haddad.managemyrounds.controller.fragment.displayFacesFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<Button
android:id="#+id/button75"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>

Android: How to avoid ImageView flashing when the screen is clicked?

I have an ImageView on the screen that I've scaled. When I click on the screen, the image goes back to its original size, and then the scaled size, and then back again constantly. Any ideas on how to fix this? Or why this is happening?
My code for the ImageView's fragment
public class LargeFragment extends Fragment {
Button back;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.large,container,false);
final ImageView p = (ImageView) view.findViewById(R.id.large);
final SharedPreferences pref = getActivity().getSharedPreferences("high",0);
p.setImageResource(getResources().getIdentifier(pref.getString("selectedPlayer","pink").concat("large"), "drawable",view.getContext().getPackageName()));
view.post(new Runnable() {
#Override
public void run() {
p.requestLayout();
final int w = view.getWidth();
p.getLayoutParams().height = w; //scaling the image here
}
});
back = (Button) view.findViewById(R.id.exit);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LargeFragment.this.getActivity().finish();
}
});
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#drawable/bg2">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/large"
android:rotation="270"
android:scaleType="fitCenter"
android:layout_gravity="center_vertical"
/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_gravity="end"
android:layout_height="35dp"
android:id="#+id/exit" />
</FrameLayout>
use android:scaleType in xml or ImageView.setScaleType() to scale the image. I think centerCrop is what you need.
see android:scaleType to get more information.

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

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

Categories

Resources