I don't know whay when i setting longClickListener to my a view , it is not fire,It is my layout :
<?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">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/map"
android:layout_below="#+id/toolbar"
android:layout_height="match_parent">
</RelativeLayout>
<TextView
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="16"
android:id="#+id/zoom_level"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<RelativeLayout
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:padding="10dp"
android:background="#drawable/zoom_in_bg"
android:layout_above="#+id/zoom_out"
android:src="#drawable/ic_add_black_24dp"
android:layout_width="wrap_content"
android:id="#+id/zoom_in"
android:layout_marginBottom="1dp"
android:layout_height="wrap_content"/>
<ImageView
android:padding="10dp"
android:background="#drawable/zoom_out_bg"
android:id="#+id/zoom_out"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_remove_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
and it is fragment
public class MapFragment extends BaseFragment implements View.OnLongClickListener
{
private View rootView;
private org.mapsforge.android.maps.MapView mapView ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment_navigation, container, false);
mapView = new org.mapsforge.android.maps.MapView(getContext());
mapView.setOnLongClickListener(this);
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
relativeLayout.addView(mapView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
return rootView;
}
#Override
public boolean onLongClick(View view)
{
KLog.error("Long Click called");
return true;
}
}
i have tried SetClickable(true) , setLongClickable(true), setFocusable(true) but i cannot solve this problem.what is the solution?
please advise
You can add this code to your class:
mapView.setLongClickable(true);
Or add android:longClickable="true" to xml.
Put android:longClickable="true" in your Relative layout where you want to add onLongClickListener:
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/map"
android:layout_below="#+id/toolbar"
android:layout_height="match_parent"
android:longClickable="true">
</RelativeLayout>
Try with set long click listener on Relative layout instead of mapView:
RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.map);
relativeLayout.setOnLongClickListener(this);
Related
There are some views and one FrameLayout in the layout. I want to custom NavHostFragment replace FrameLayout with RelativeLayout, it works but the views hierarchy is wrong. The ImageView should be top, but it is in the bottom.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
and my custom NavHostFragment:
public abstract class BaseModuleFragment extends NavHostFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = null;
if (getContentViewId() > 0) {
view = inflater.inflate(getContentViewId(), container, false);
View frameLayout = view.findViewById(R.id.lb_nav_host_container);
if (!(frameLayout instanceof FrameLayout)) {
throw new RuntimeException("frame layout must exist");
}
frameLayout.setId(getId());
} else {
view = super.onCreateView(inflater, container, savedInstanceState);
}
return view;
}
}
Was the method wrong, or how to solve?
Change your layout to this one. It should be ok
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_below="#+id/ivTop"
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
These pictures show:
Wrong:
FrameLayout cover ImageView, it is wrong
Right
ImageView should be top, this is right
FrameLayout top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ImageView top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
I'm trying to launch a fragment from an activity. However, when I run the app and click on the button that has to launch the fragment I get the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074}
I can't see where I've made something wrong. It should be correct. This is the class where I implement the fragment. I'm only gonna upload the View onCreateView for this class as no more is needed:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_kontakt, container, false);
sendmail = (Button) view.findViewById(R.id.sendknap);
sendmail.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
msg = (EditText) view.findViewById(R.id.besked);
String message = msg.getText().toString();
sendemail(message);
}
});
return view;
}
I launch the fragment from here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.Kontakt) {
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.add(R.id.kontaktfragment, fragment)
.commit();
}
return super.onOptionsItemSelected(item);
}
What have I missed?
Any help is appreciated!
EDIT:
This is the XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/velkomst"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/velkomst" >
</FrameLayout>
</RelativeLayout>
And this is the kontakfrag XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/kontaktfragment"></FrameLayout>
</RelativeLayout>
And this is the fragment_kontakt XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/besked"
android:layout_weight="0.70"
android:textSize="20dp"
android:layout_marginTop="30dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:background="#ffffff"
android:gravity="top"
android:paddingTop="30dp"
android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt"
android:scrollIndicators="right"/>
<Button
android:layout_width="144dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendknap"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:textSize="20dp"/>
</LinearLayout>
My mistake was that I created the fragment as a separate layout and not as a framelayout on the frontpage. So the solution was to create a framelayout on the frontpage. That makes sense :)
First to the layout of activity, add this
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in that activity java code add
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
Here I'm implementing TabLayout with viewPager in a Fragment using library of com.astuetz.PagerSlidingTabStrip. Everything is fine but now I want to add custom Tab and some more functionalities.
test_tab_fragment.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/mainTabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#111"
app:pstsDividerColor="#color/text_color"
app:pstsIndicatorColor="#color/green"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="true"
app:tabGravity="fill"
app:tabMode="fixed" />
<com.cws.widget.NonSiwpablePager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
TestTabFragment.java
public class TestTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_tab_fragment, container, false);
PagerSlidingTabStrip myTabLayout = (PagerSlidingTabStrip) view.findViewById(R.id.mainTabLayout);
NonSiwpablePager pager = (NonSiwpablePager) view.findViewById(R.id.pager);
ViewPagerAdapter mAdapter = getViewPagerAdapter();
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(4);
myTabLayout.setViewPager(pager);
for (int i = 0; i < mAdapter.getCount(); i++) {
View tabView = mAdapter.getCustomeView(getActivity(), i);;
tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab_view, myTabLayout,
false);
String title_arr[] = {"ADVISORY", "CALENDER", "TOP ADVISORS", "EXPERT VIEW"};
int[] drawables = new int[]{R.drawable.advisory_normal, R.drawable.calender_normal, R.drawable.topadvisor_normal, R.drawable.expertview_normal};
TextView mTextView = (TextView) tabView.findViewById(R.id.textView);
ImageView mImageView = (ImageView) tabView.findViewById(R.id.imageView2);
mImageView.setImageResource(drawables[i]);
mTextView.setText(title_arr[i]);
myTabLayout.addView(tabView);
}
return view;
}
And custom_tab_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="#dimen/et_topPadding"
android:paddingTop="#dimen/et_topPadding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/text_color"
android:textSize="#dimen/custom_tab_tSize" />
</LinearLayout>
</LinearLayout>
If anyone has done it so please help me to solve it.
I'm using this library for the fab implementation.
As you can see in this video, the fab position is changing when the fragment loads.
After debugging it, I found that the listview "setAdapter()" method causes the position change.
I don't have a clue why it's happening..
My layout:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sad_face"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_leagues" />
</LinearLayout>
</FrameLayout>
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab_new_league"
android:src="#drawable/ic_action_add"
app:fab_colorNormal="#color/orange"
app:fab_colorPressed="#color/redDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#id/toolbar"
android:layout_marginBottom="-28dp"
android:layout_marginRight="28dp"
android:elevation="8dp"/>
</RelativeLayout>
My fragment
public class LeagueListFragment extends MyFragment {
#InjectView(android.R.id.list) ListView mListView;
#InjectView(android.R.id.empty) LinearLayout mEmpty;
public static Fragment newInstance() {
return new LeagueListFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_league_list, container, false);
ButterKnife.inject(this, view);
// Fetch league list and set up adapter
List<League> list = League.getAll();
mAdapter = new LeagueAdapter(getActivity(), list);
// Set up ListView
mListView.setEmptyView(mEmpty);
mListView.setDivider(new ColorDrawable(getResources().getColor(R.color.gray)));
mListView.setDividerHeight(2);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
}
Margin changes in the runtime on pre-Lollipop because shadow mustn't be taken into account when margins are set.
Try to disable the shadow by :
fab:fab_shadow="false"
Fixing the issue is not expected for now.. (See makovkastar answer)
I have the follwing layout:
<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" >
<View
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
Programatically I'm replacing the view "content" with my Fragment:
View view = inflater.inflate(R.layout.kwiz_game, container, false);
contentView = view.findViewById(R.id.content);
currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
getFragmentManager().beginTransaction().replace(R.id.content,
currentFragment);
However the fragment is taking all the space and the Layout with the TextView is not shown.
I also tried to use RelativeLayouts with alignParentBottom=true and other stuff...
View of the Fragment which replaces the content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_kwiz_item" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="centerCrop" />
</RelativeLayout>
OnCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_back, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
if (drawable != null) {
imageView.setImageDrawable(drawable);
} else {
imageView.setBackgroundResource(drawableId);
}
return view;
}
After hours of debugging I finally found the mistake... Some of my layouts had views with the id "content". I thought when I look for R.id.content that the "nearest" element will be used... that's not the case!
Thanks anyone!
I modified your layout a little bit and this seems to achieve what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomNav" />
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
</RelativeLayout>
And a small side note, I see you used fill_parent in your layout, but that's deprecated since API level 8, so you should use match_parent instead :)
Hope this helps, cheers!
You should use FrameLayout as your fragment container, and put (and then replace) your fragments into that FrameLayout container.