I have a fragment inside the main activity.The fragment contains an image view.My aim is to divide the image view into 3 different buttons.So ,i set up a linear layout which overlaps the image view and it contains 3 clickable views with equal gravity.
In the fragment i implements OnClickListener ,as this-
public class FragmentHomeMenu extends Fragment implements OnClickListener {
View view;
Context activity_context;
View button_search_1;
View button_search_2;
View button_search_3;
...plenty of other stuff
public FragmentHomeMenu() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home_menu, container, false);
activity_context = getActivity().getApplicationContext();
button_search_1 = (View) view
.findViewById(R.id.button_search_1);
button_search_1.setOnClickListener(this);
button_search_2 = (View) view.findViewById(R.id.button_search_2);
button_search_2.setOnClickListener(this);
button_search_3 = (View) view.findViewById(R.id.button_search_3);
button_search_3.setOnClickListener(this);
....plenty of other stuff
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activity_context = activity.getApplicationContext();
}
#Override
public void onClick(View v) {
Log.w("", "some thing was clicked");
switch (v.getId()) {
case R.id.button_search_2:
dostuff1();//stuff1 is not happening in android 4.3,but works on 4.0
break;
case R.id.button_search_1:
dostuff2();//stuff2 is not happening in android 4.3,but works on 4.0
break;
......plenty of other stuff
}
I checked for the problem on plenty of places but could not find solution.My code works perfect on
devices with android 4.0 but in 4.3 nothing happens when i click on the image.
here is the xml-
.......
<ImageView
android:id="#+id/imageview_searchbar"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:layout_below="#+id/rltv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:clickable="true"
android:src="#drawable/search_bar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageview_searchbar"
android:layout_alignLeft="#+id/imageview_searchbar"
android:layout_alignRight="#id/imageview_searchbar"
android:layout_alignTop="#+id/imageview_searchbar"
android:orientation="horizontal"
android:weightSum="21" >
<View
android:id="#+id/button_search_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
<View
android:id="#+id/button_search_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:clickable="true" />
</LinearLayout>
.......lots of other stuff
I figured it out-I just had to bring the linear layout(containing buttons) to the front.
It got hidden under the image when using android 4.3 so i did this-
LinearLayout search_bar_buttons = (LinearLayout) view.findViewById(R.id.ll_search_bar_buttons);
search_bar_buttons.bringToFront();
Thank you guys for helping
Related
I'd like to be able to set a View.OnclickListener to an ImageView inside a page (fragment) of my ViewPager, I don't want to click on the whole page of my ViewPager, I just want to be able to click on a specifig ImageView. I followed these questions (onClick not triggered on LinearLayout with child, How to set OnClickListener in ViewPager
) but they did not help me. This is my actual code:
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
MyActivity.xml
<android.support.v4.view.ViewPager
android:id="#+id/product_view_pager"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:overScrollMode="never" />
MyFragment.class (where I'm binding the views using Butterknife)
#BindView(R.id.container_linear)
LinearLayout mContainer;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
mContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Test", "Clicked!");
}
});
}
Thanks for your support.
One thing to consider would be moving your OnClickListener to the parent LinearLayout you're setting as clickable and seeing if that helps.
Another thing is adding android:focusableInTouchMode="true" to the view you're attaching your OnClickListener to.
Other than that, you might want to share the code for registering the OnClickListener so we can investigate other possible errors.
If I understand correctly you want to click on the image view insideMyPageFragment.xml. This is simple but you need to give ID to the ImageView and refer it in your onCreateView method like you do for LinearLayout and then use onClickListener on that image.
So with recent conversation it seems that in linear layout you need to remove line clickable="false"
Basically that has blocked all the clicks on its children, thus that linear layout as well as the imageview clicks are not working for you. See this code I removed that line. Hope this works
Follow this code.
MyPageFragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:id="#+id/container_linear"
android:gravity="center"
android:orientation="vertical">
<--you need to give ID to the view to be able to
perform events on them specifically.-->
<ImageView
android:id="#+id/my_awesome_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
</LinearLayout>
Now in your MyFragment.class
#BindView(R.id.container_linear)
LinearLayout mContainer;
//declare with butterknife
#BindView(R.id.my_awesome_image)
ImageView myAwesomeImage;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mRootView = inflater.inflate(R.layout.my_layout, container, false);
ButterKnife.bind(this, mRootView);
//you set click listener previously on entire linear layout instead of imageview
//that's why it was not reflecting on imageview click.
myAwesomeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG , "Awesome Imageview clicked!");
}
});
}
I solved this issue using the following chunk of code:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
The reference for this answer can be viewed here: How to detect click on ImageView in ViewPager's PagerAdapter (android)?
I want to change from layout 1 to layout 2, after rotate still keep content. Can somebody show me how to do that?
from this: https://www.dropbox.com/sc/y2nyrzard859hf2/AAD0qVjWoLzKcnQV9a4FTQi_a
to this: https://www.dropbox.com/sc/29hhlbfm31cfs0j/AADCWsFNzD7DKHx4q9i2FlbDa
this is my code but seem like it didn't work
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_create_bill3);
}
else {
setContentView(R.layout.activity_create_bill2);
}
if(fragmentManager.findFragmentByTag("fragment_product")==null) {
fragment_product = new Fragment_Product();
fragmentTransaction.replace(R.id.fragment_product,fragment_product,"fragment_product");
}
else
fragmentTransaction.replace(R.id.fragment_product,fragmentManager.findFragmentByTag("fragment_product"));
if(fragmentManager.findFragmentByTag("fragment_product_chosen")==null) {
fragment_product_chosen = new Fragment_Product_Chosen();
fragmentTransaction.replace(R.id.fragment_product_chosen,fragment_product_chosen,"fragment_product_chosen");
}
else
fragmentTransaction.replace(R.id.fragment_product_chosen,fragmentManager.findFragmentByTag("fragment_product_chosen"),"fragment_product_chosen");
fragmentTransaction.commit();
I using 2 diffent layout, it has a same view but one in horizontal and another in vertical, when rotate, fragment_product still keep content, but fragment_product_chosen are disappear.
You should have 3 clases:
FragmentMain
FragmentSide
MainActivity
click here to see your layout folder
Code in your MainActivity Class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Code in your FragmentMain Class:
public class FragmentMain extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
Code in your FragmentSide Class:
public class FragmentSide extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_side, container, false);
}
}
Then in your activity_main.xml:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_main"
android:layout_marginTop="230dp"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="match_parent"
android:layout_height="220dp"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
/>
same way in your activity_main.xml(land):
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="250dp"
android:id="#+id/fragment_main"
class="au.com.example.multi_fragments.FragmentMain" />
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/fragment_side"
class="au.com.example.multi_fragments.FragmentSide" />
in your fragment_main.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
In your fragment_side.xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 1"
android:textSize="20sp"
android:padding="20dp"
android:textStyle="bold"
android:id="#+id/textViewMain" />
Click here to see the output
I hope this solution is the one you want. Good luck :)
To have different layouts for landscape and portrait modes, create two folders under the res folder: layout and layout-land. All XML files should have the same names in both folders. For more details, read Designing for Multiple Screens. Even though this article is for different screen sizes, the techniques apply to different device orientations as well.
As for saving and restoring data, this is the same as destroying the activity without an orientation change.
I am developing app for Android TV. In app there is a fragment layout that has four ImageView in LinearLayout horizontal direction and a RecyclerView that is below on LinearLayout. When I launch app and navigate through DPAD control then RecyclerView navigate up and down. Requirement is that when fragment launch by default first ImageView of LinearLayout would be selected. After when we press right button of DPAD then next ImageView would be select. When we press down key then focus will move on RecyclerView. When focus will on RecyclerView first child and we press Up key then focus move on first Imageview of LinearLayout.
I have also use imageview.requestFocus() but it is not working. Please help me.
Below is my layout file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/home_fragment_menu_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="10dp">
<ImageView
android:id="#+id/img_playall"
style="#style/home_fragment_menu"
android:src="#drawable/play_all"
android:focusable="true"
android:focusableInTouchMode="true"/>
<ImageView
android:id="#+id/img_trending"
style="#style/home_fragment_menu"
android:src="#drawable/trending"/>
<ImageView
android:id="#+id/img_search"
style="#style/home_fragment_menu"
android:src="#drawable/search"/>
<ImageView
android:id="#+id/img_setting"
style="#style/home_fragment_menu"
android:src="#drawable/settings"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/home_fragment_menu_container"
android:layout_marginLeft="45dp"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/vertical_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</RelativeLayout>
Below is fragment.java file
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
ImageView playAllImage, trendingImage, searchImage, settingImage;
Context context;
RecyclerView verticalList;
ArrayList<ArrayList<VideoContentModel>> categoriesVideoList;
ArrayList<CategoryModel> categoryDataList;
public static HomeFragment newInstance(Context context, ArrayList<ArrayList<VideoContentModel>> categoriesVideoList,
ArrayList<CategoryModel> categoryDataList){
HomeFragment fragment = new HomeFragment();
fragment.context = context;
fragment.categoriesVideoList = categoriesVideoList;
fragment.categoryDataList = categoryDataList;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment_layout, container, false);
playAllImage = (ImageView)view.findViewById(R.id.img_playall);
trendingImage = (ImageView)view.findViewById(R.id.img_trending);
searchImage = (ImageView)view.findViewById(R.id.img_search);
settingImage = (ImageView)view.findViewById(R.id.img_setting);
playAllImage.requestFocus();
verticalList = (RecyclerView)view.findViewById(R.id.vertical_recyclerview);
verticalList.setHasFixedSize(true);
verticalList.setLayoutManager(new LinearLayoutManager(context));
CatListVerticalAdapter adapter = new CatListVerticalAdapter(context,categoriesVideoList,categoryDataList);
verticalList.setAdapter(adapter);
verticalList.clearFocus();
//
if(playAllImage.hasFocus())
Log.i(TAG,"play all has focus");
else
Log.i(TAG,"===playall has not focus");
return view;
}
How we navigate through DPAD above requirement? Please help me..
I have a view pager consisting of 4 tabs. Each tab holds its own fragment.
How would I be able to use fragment transaction to replace the fragment in tab 3 with a new fragment?
I've tried a lot of things, but I have not been able to come up with a solution at all. I've tried looking around on google and stackoverflow, but with no success.
I assume that your fragment has a button that is put in the center. By clicking on it, you can change the layout stays under of this button. The content/layout of the first fragment you mentioned should be replaced with wrapperA and the content/layout of the second one should be replaced with wrapperB. I put a simple red background for wrapperA to distinguish it with wrapperB, wrapperB is also green due to the same reason. I hope this is what you want:
public class SwitchingFragment extends Fragment {
Button switchFragsButton;
RelativeLayout wrapperA, wrapperB;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.layout_switch, container, false);
wrapperA = (RelativeLayout) rootView.findViewById(R.id.wrapperA);
wrapperB = (RelativeLayout) rootView.findViewById(R.id.wrapperB);
switchFragsButton = (Button) rootView.findViewById(R.id.switchFragsButton);
switchFragsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (wrapperB.getVisibility() == View.GONE) {
wrapperB.setVisibility(View.VISIBLE);
// There is no need to change visibility of wrapperA since it stays behind when wrapperB is visible.
// wrapperA.setVisibility(View.GONE);
}
else {
wrapperB.setVisibility(View.GONE);
// Again, there is no need.
// wrapperA.setVisibility(View.VISIBLE);
}
}
});
return rootView;
}
}
The 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"
android:layout_gravity="center"
android:orientation="vertical">
<!-- The content of first fragment should be in "wrapperA". -->
<RelativeLayout
android:id="#+id/wrapperA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red"
android:visibility="visible">
</RelativeLayout>
<!-- The content of second fragment should be in "wrapperB". -->
<RelativeLayout
android:id="#+id/wrapperB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green"
android:visibility="gone">
</RelativeLayout>
<!-- As I said, I assume that the layout switcher button is stable
and so it should be in front of the switching layouts. -->
<Button
android:id="#+id/switchFragsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#color/black"
android:text="Switch"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/white"/>
</RelativeLayout>
Alternatively, you can try to change the fragment directly by notifying your FragmentPagerAdapter as described in this link:
Replace fragment with another fragment inside ViewPager
I have a problem when declaring a button.
I will try to explain as specific as possible.
In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.
My intention is that my main fichero.java to declare the buttons within the fragment.
Here we put the main Layout:
<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:baselineAligned="false">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
android:id="#+id/fragmentTrueFalse"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Grp1"
android:id="#+id/textGrp1"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="450dp"
android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
android:id="#+id/fragmetaskGRP1"
android:layout_below="#+id/textGrp1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
Here we put the secondary Layout that has the buttons:
<?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:id="#+id/fragment_TrueorFalse">
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttontrue"
android:id="#+id/buttontrue"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttonfalse"
android:id="#+id/buttonfalse"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here I leave the my main activity:
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonTrue.setOnClickListener(this);
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
return view;
}
My problem I find in the following line:
buttonTrue.setOnClickListener (this);
The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment
I hope I have spread far.
Thank you very much for everything, this forum is amazing.
Try this:
Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.
Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.
I set you a Log, if you need change it to Toast, or code
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
//Declare your widgets (Buttons)
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonFalse = (Button) view.findViewById(R.id.buttonfalse);
//Set the Listeners
buttonTrue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: True");
}
});
buttonFalse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: FAlse");
}
});
//return the view
return view;
}
void setOnClickListener(View.OnClickListener l)
requires a OnClickListener, which Grp1Fragment is not.
Use something like
buttonTrue.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
... // reaction on the button
}
});