My note application has 2 fragments on a screen: A list of notes fragment and a note detail fragment to display the selected note in the second fragment, it has an input text field (android:inputType="textMultiLine"). As I want to handle event keyboard hidden so I can save the change user has made when they close the keyboard. Can anyone give me a clue to do this task?
I think that the best and universal solution for keyboard issue, is measure the VisibleDisplayFrame on layout change:
Rect visibleRect = new Rect();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false);
mMain.getWindowVisibleDisplayFrame(visibleRect);
mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
Rect r = new Rect();
mMain.getWindowVisibleDisplayFrame(r);
if(r.height() < visibleRect.height()){
//keyboard shown
}
if(r.height() == visibleRect.height()){
//keyboard hidden
}
}
});
return mMain;
}
Here's how i handle it on my Note Detail fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.view = inflater.inflate(R.layout.fragment_note_detail_of_style_searching, container , false);
listviewFilter = (ListView) view.findViewById(R.id.listview_filter);
noteDetailView = view.findViewById(R.id.note_detail_of_style_view);
photoDetailView = view.findViewById(R.id.gv_note_detail_photo);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = view.getRootView().getHeight() - view.getHeight();
if (heightDiff > 200) {
isWatchingKeyboard = true;
photoDetailView.setVisibility(View.GONE);
return; //must exit now
}
if(isWatchingKeyboard){
isWatchingKeyboard = false;
viewForFocus.requestFocus();
photoDetailView.setVisibility(View.VISIBLE);
}
}
});
return this.view;
}
I did it this way:
private int prevBottom;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_status, container, false);
prevBottom = view.getBottom();
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (view.getBottom() > prevBottom) {
view.requestFocus();
}
prevBottom = view.getBottom();
}
});
return view;
}
and added this to the view I was inflating:
android:focusableInTouchMode="true"
and this to the activity in the AndroidManifest.xml
android:windowSoftInputMode="adjustResize"
Related
I'm using BottomSheetDialogFragment to show some data.But when I'm starting the fragment it's appearing 50% of the screen .So, my question is how to make it full screen when it shows.
BottomSheetDialogFragment Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
}
);
return v;
}
You can use dialog fragments, plz refer this:
public class DialogFragments extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.dialog_dialogfragment_layout, null);
getDialog().setTitle("Title");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
getDialog().getWindow().setGravity(Gravity.BOTTOM);
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, (int) (metrics.heightPixels * 0.30));// here i have fragment height 30% of window's height you can set it as per your requirement
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationUpDown;
}
and when you want to open,open Bottomsheet dialog like this way :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
DialogFragments dialogFragment = new DialogFragments(this);
dialogFragment.show(fm, "Bottomsheet Fragment");
}
}
);
return v;
}
You have to apply this style to you BottomSheetDialogFragment
android.R.style.Theme_Material_Light_NoActionBar_Fullscreen
My Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Uri uri = Uri.parse("Home");
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.mesage_linear1);
final TextView message = new TextView(getActivity());
number_view.setText("Long Text");
int m_width = message.getMeasuredWidth();
int m_height = message.getMeasuredHeight();
Toast.makeText(getActivity(),"Height : "+m_height+"\nWidth : "+m_width,Toast.LENGTH_LONG).show();
return rootView;
}
I want to get the height and width of the programmatically created TextView.
Output is Height : 0 and Width : 0
At that point the View hasn't been laid out yet. You have to manually measure it (via View.measure()) or to wait until it is laid out:
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = textView.getHeight();
int width = textView.getWidth();
}
});
TextView tv=(TextView)findViewById(R.id.text);
tv.setText("Android");
tv.measure(0, 0);
tv.getMeasuredWidth();
tv.getMeasuredHeight();
tv.getTextSize();
I'm using V4.Fragments. So from activity i'm creating new instance of my fragment and after that i begin transaction.
The main problem,that my imageview control is defined(in axml) as Layout_width="match_parent"
and weightSum=1 (for height).
For some reason i need to know height/width of my imageView.
What i tried :
was created class that implements IOnGlobalLayoutListener
class GlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
{
System.Action _OnGlobalLayout;
public GlobalLayoutListener (System.Action onGlobalLayout)
{
this._OnGlobalLayout = onGlobalLayout;
}
public void OnGlobalLayout ()
{
_OnGlobalLayout ();
}
}
After that,in my V4.Fragment class,i'm doing this:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
Action CallBack = () =>
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
//new instance of class,that implements IOnGlobalLayoutListener
GlobalLayoutListener Global = new GlobalLayoutListener(CallBack);
imgView.ViewTreeObserver.AddOnGlobalLayoutListener(Global);
retun _View;
}
Trick doesnt worked. Always return zero(height/width).
Second variant,i implemented interface of IOnGlobalLayoutListener directly in MyFragment
public class MyCustomFragment : Android.Support.V4.App.Fragment,View.IOnTouchListener,ViewTreeObserver.IOnGlobalLayoutListener
{
int ImgHeight;
int ImgWidth;
ImageView imgView;
Bundle Arguments;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
ViewTreeObserver Vto = imgView.ViewTreeObserver; //also tried with _View.ViewTreeObserver; result same
Vto.AddOnGlobalLayoutListener(this);
retun _View;
}
Same problem,doesnt worked.
And my last variant is like :
public class MyCustomFragment : Android.Support.V4.App.Fragment
{
int ImgHeight;
int ImgWidth;
ImageView imgView;
Bundle Arguments;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
imgView.ViewTreeObserver.GlobalLayout += (sender, e) => //also tried with _View
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
retun _View;
}
Again no luck,what i'm doing wrong? Thanks.
PS Sorry for my eng.
All works fine,i was doing something wrong.
So this code works great :
imgView.ViewTreeObserver.GlobalLayout += (sender, e) =>
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
Enjoy!
When I change spinner value, chart is updated on TAB2 but not in TAB1 even if the TAB1 is active and showed on the screen.
Here is my code...
onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_charts_line, container, false);
layoutChart = (LinearLayout) rootView.findViewById(R.id.chart);
...all renderer settings...
return rootView;
onViewCreated:
public void onViewCreated(View view, Bundle savedInstanceState){
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_actionbar);
final Spinner spinner_nav = (Spinner) mToolbar.findViewById(R.id.spinner_nav);
spinner_nav.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cSpinner = spinner_nav.getSelectedItem().toString();
showChart(cSpinner);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
cSpinner= spinner_nav.getSelectedItem().toString();
showChart(cSpinner);
}
showChart:
public void showChart(String account){
if (mChartView == null) {
LinearLayout layout = (LinearLayout) layoutChart.findViewById(R.id.chart);
mChartView = ChartFactory.getTimeChartView(layoutChart.getContext().getApplicationContext(), mDataset, mRenderer, "MMM yyyy");
layout.addView(mChartView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
mChartView.repaint();
}
}
What I want is to update all charts in every tab, or at least to update chart in current tab. Every variable name is different in every tab Fragment.
#Override
public Object instantiateItem(View pager, int position) {
ViewGroup view = new ViewGroup(mContext) {
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
};
view.setBackgroundColor(Color.BLUE);
View view2 = new View(mContext);
LayoutParams lp = new LayoutParams(200, 200);
view2.setLayoutParams(lp);
view2.setBackgroundColor(Color.BLACK);
view.addView(view2);
((ViewPager)pager).addView(view, 0);
return view;
}
Is it possible?
If not, how can I add a ViewGroup to a ViewPager?
Please, help me out.
You should use FragmentPagerAdapter to add fragment to ViewPager.
Fragments can contain any viewgroups.