Code for tap on text view to display text - android

i am developing an android app,this is my first app development,
As per my requirement , i need code to implement UI as below in the image.
Hopping the image is explaining my requirement. the below image is the App UI view. Thanks..
I have used the below code , but it is not working. can anyone finds the fault..
- DrinkActivity.java
TextView t ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
- activity_drink.xml
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<ListView
android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<TextView android:text="This is my first text"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28sp"
android:editable="true"
android:clickable="true"
android:layout_height="wrap_content"
android:onClick="onClick">
</TextView>

You could achive this behavior by designing 2 fragments:
1. The first one will be a simple TextView fragment that will display your tab text only.
2. The second fragment will be as a LinaerLayout vertical layout that will contain 2 TextViews one after another, the first will be the same TextView as in the first fragment while the second TextView will contain the description of your tab.
then all what you should do is on the click of the first fragment replace it with the second one using this code:
if (!isTabOpen)
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabFragment)
.add(R.id.containerForFragments,TabAndDescriptionFragment)
.commit();
}
else
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabAndDescriptionFragment)
.add(R.id.containerForFragments,TabFragment)
.commit();
}
after you have created the plusButtonFragment and the userNewFragment.

Related

add fragment to fragment working well,but finish() method to remove second fragment not working well

I am adding a fragment into a RelativeLayout which is a small part of other fragment(containing viewpager) which is adding well, but when i am trying to vanish the fragment calling finish() its not working.
xml code:
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Share you thoughts here..."
android:paddingLeft="10dp"
android:background="#drawable/edittext_status_background"
android:padding="20dp"
android:elevation="5dp"
android:id="#+id/editTextWritePostId"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fitsSystemWindows="true"
android:background="#f9f9f9"
android:id="#+id/relativeLayoutIdNewsFeedForPostBar"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post"
android:textColor="#ffffff"
android:background="#1470a6"
android:id="#+id/postButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkInButtonId"
android:layout_toLeftOf="#+id/smileyButtonId"
android:layout_centerInParent="true"
android:src="#drawable/ic_checkin"
android:background="#android:color/transparent"
android:layout_marginRight="3dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/smiley"
android:layout_toLeftOf="#+id/imageUploadButtonId"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:id="#+id/smileyButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/camera"
android:layout_toLeftOf="#+id/videoUploadButtonId"
android:layout_centerInParent="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:id="#+id/imageUploadButtonId"
/>
</RelativeLayout>
**//adding new fragment to below layout**
**<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutIdForSmiley"
>
</RelativeLayout>**
</LinearLayout>
Fragment code to remove new fragment:
smileyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SmileyShowFragment smileyFragment=new SmileyShowFragment();
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
if(!(smileyFragment.isVisible())) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
relativeLayoutIdForSmiley.setVisibility(View.GONE);
}
}
});
I am trying to vanish the RelativeLayout with visibility gone and also vanish fragment with getActivity().finish() but none of these working.can you help me please. I have searched internet a lot which are same as i am doing.
I guess I have figured out your issue. Here is what you are doing the mistake. In your smileButton onclick you are creating a new instance of SmileyShowFragment(SmileyShowFragment smileyFragment=new SmileyShowFragment();) and checking weather the fragment is visible using if(!smileyFragment.isVisible()). No matter how many time you trigger your click, this condition will be true always, as the fragment instance is created every time you the click the button. The solution is, create the instance of the fragment outside of the button click. Try some thing like this.
final SmileyShowFragment smileyFragment = new SmileyShowFragment();
smileyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.relativeLayoutIdForSmiley);
if(!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
Have you tryed to use detach or remove on your fragment ?
Go check : https://developer.android.com/reference/android/app/FragmentTransaction.html

One fragment for multiple activities

We always have heard using multiple fragments with one activity. Is opposite possible? I am curious about this. Can we use same fragment for multiple activities. Please give ONE EXAMPLE.
How to reuse one Fragment in multiple Activities
The green background with two buttons is a single fragment that is reused among multiple activities.
1. Make your fragment class and layout
MyFragment.java
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myLayout = inflater.inflate(R.layout.my_fragment_layout, container, false);
// add click listeners to the buttons in the fragment
Button buttonOne = myLayout.findViewById(R.id.button_1);
Button buttonTwo = myLayout.findViewById(R.id.button_2);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
return myLayout;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
Toast.makeText(getContext(), "Button One", Toast.LENGTH_SHORT).show();
break;
case R.id.button_2:
Toast.makeText(getContext(), "Button Two", Toast.LENGTH_SHORT).show();
break;
}
}
}
my_fragment_layout.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:background="#android:color/holo_green_dark"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
2. Add the fragment to your activities
activity_blue.xml
<?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:background="#android:color/holo_blue_dark"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToRedActivityButtonClick"
android:text="Go to red activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_red.xml
<?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:background="#ff3636"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToYellowActivityButtonClick"
android:text="Go to yellow activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_yellow.xml
<?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:background="#f9f478"
android:orientation="vertical">
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Notes
For simplicity we added the fragment directly to the xml. You can also load fragments dynamically in code. See the documentation for help with that.
Yes, it is possible to have one fragment with multiple activities.
But you will need to program the layout with java using LayoutParams and embed them in every fragment instance.
On every activity, you need to call this fragment
Create your UI Components in Java, add them to the layout dynamically from Java Class i.e. Your Activities.
I would suggest this approach will not be easy to maintain, if you are not super comfortable with Java exclusively. You will need to forget XML for this approach as nothing will be there in it at all, everything will be done with Java classes only.
generic_error_msg_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/White" >
<TextView
android:id="#+id/error_message_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/font_size_16sp" />
<Button
android:id="#+id/error_button_handler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="left|center_vertical"
android:textSize="#dimen/font_size_16sp"
/>
</RelativeLayout>
GenericErrorFragment.Java
public class GenericErrorFragment extends Fragment{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View genericView = inflater.inflate(R.layout.generic_error_msg_fragment, null);
TextView errorText = (TextView) genericView.findViewById(R.id.error_message_textview);
errorText.setText("error msg" );
Button errorbutton = (Button) genericView.findViewById(R.id.error_button_handler);
errorbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic launch some other activity
}
});
return genericView;
}
}
you can load this fragment in any activity and can define your custom text for error and button handler
I will not write whole code but I can give you exact example you are looking for
Think of an application in which a person can register either as admin or as user
Now while working on it you make 3 fragments in admin registration activity asking
1.) personal information
2.) academic information
3.) admin details
Now for user registration activity say you make 2 fragments to get the following information
1.) personal information
2.) user details
here you used personal information fragment 2 times for 2 activities
code for this is child's play main thing is the concept

Dynamically adding fragments below an existing fragment in a LinearLayout

I am making an activity which has a section for comments based on addresses inputed by the user.
The user will add a new comments section by pressing a button. This is a non fixed number of sections, so I have initially added this section as a fragment in the xml.
I have an onClick function that adds another fragment to the linearlayout.
My problem is that the new fragments always add to the top of the linearlayout , i.e. above the existing fragment/fragments (i.e. the new fragment will push all the other fragments down).
How can I add the new fragment so that it displays below the existing fragment?
Code in .java file:
public class SpecialReportAdden extends ActionBarActivity {
int numOfFragments;
LinearLayout addenHolder;
TextView report;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_special_report_adden);
numOfFragments=1;
Button addenSave = (Button)findViewById(R.id.btnAddendumSave);
Button addenAddComment = (Button)findViewById(R.id.btnAddendumAddComment);
addenHolder =(LinearLayout)findViewById(R.id.AddenLinLayHolder);
addenSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this should save all comments including those added in dynamically produced fragments
//save database
Intent i = new Intent (SpecialReportAdden.this, MenuPage.class);
startActivity(i);
}
});
addenAddComment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("number of Fragments at start of OnClick", Integer.toString(numOfFragments));
Fragment newAddenFrag = addNewfragment(numOfFragments);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.AddenLinLayInnerHolder, newAddenFrag);
ft.addToBackStack(null);
ft.commit();
numOfFragments++;
Log.i("number of Fragments updated", Integer.toString(numOfFragments));
}
});
}
public Fragment addNewfragment(int number) {
AddendumLinInputFragment adden = new AddendumLinInputFragment();
TextView tx = (TextView) findViewById(R.id.txtVAddendumFragReportNum);
tx.setText("Report "+number+" :");
tx.setTextColor(Color.BLACK);
TextView address = (TextView)findViewById(R.id.txtVAddendumFragAddress);
address.setText("A new address");
address.setTextColor(Color.BLUE);
return adden;
}
xml file for the .java activity
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.danielt.pestcontrol.SpecialReportAdden"
android:id="#+id/RellayAddenComments">
<TextView
android:id="#+id/txtVAddendumTitle" android:text="Service Report Addendum" android:textStyle="bold" android:layout_centerHorizontal="true" android:elegantTextHeight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"/>
<TextView
android:id="#+id/txtVAddendumTechName"
android:layout_below="#+id/txtVAddendumTitle"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Technician Name: "
android:textSize="18sp"
/>
<TextView
android:id="#+id/txtVAddendumDate"
android:layout_below="#+id/txtVAddendumTechName"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: "
android:textSize="18sp"
/>
<ScrollView
android:id="#+id/scrlVAddendum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtVAddendumDate">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayHolder"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/AddenLinLayInnerHolder"
android:orientation="vertical">
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.danielt.pestcontrol.AddendumLinInputFragment"
android:id="#+id/addendum1CommentsFragment"
android:layout_below="#+id/txtVAddendum1Date"
android:layout_marginTop="24dp"
tools:layout="#layout/fragment_addendum_lin_input" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add another Addendum Comment"
android:id="#+id/btnAddendumAddComment"
android:layout_below="#+id/scrlVAddendum"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save / Update Comments"
android:id="#+id/btnAddendumSave"
android:layout_below="#+id/btnAddendumAddComment"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</LinearLayout>
</ScrollView>
I havent added the fragment code but I will if someone wants to see it.
Thanks for any help.
You don't need fragment to hold only comment view. Just inflate new comment view and add it to the parent view.
LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.yourLayout, container, false);
container.addView(inflatedLayout);
Where container is a view that hold all comments.
Regarding adding fragments, according to Fragments documentation:
If you're adding multiple fragments to the same container, then the
order in which you add them determines the order they appear in the
view hierarchy
Therefore if it's behave different, it could be related to OS version or bug :)

Fragments added to linearLayout are stacking, Causing only final one added is visible

I have a view that contains a YouTubePlayerSupportFragment, with a small box around it for styling. I want to add multiple instances of this view on top of each other (vertically) to another view. I have it working for adding a single view, but when I try to add multiple they just stack on each other, so that only the one that was added last is visible. I was able to prove this was happening, by programatically making the first view added longer and I could see the extra height of it beneath the topmost view.
Here is my code:
fragment_youtube_video:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:layout_marginTop="5dp"
android:background="#FFFFFFFF">
<fragment
android:id="#+id/youTubeVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:textColor="#000000"
android:text="Video" />
</LinearLayout>
fragment_videos:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
<ImageView
android:id="#+id/topBar"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="45dp" />
<ImageView
android:id="#+id/logoImageView"
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/CD_logo"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/pagerTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/logoImageView"
android:background="#80000000"
android:paddingBottom="10dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="12dp" >
[REMOVED FOR SPACE]
</RelativeLayout>
<ScrollView
android:id="#+id/scrollableVideoBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pagerTitle"
android:layout_alignRight="#+id/pagerTitle"
android:layout_below="#+id/pagerTitle">
<LinearLayout
android:id="#+id/videosLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#a0000000">
</LinearLayout>
</ScrollView>
</RelativeLayout>
VideosFragment.java:
private void addVideoViews() {
int count = 0;
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (String vidURL : videoURLs) {
new YoutubeFragment();
YoutubeFragment player = YoutubeFragment.newInstance(count, vidURL);
fragmentTransaction.add(R.id.videosLayout, player, "x_" + count);
count++;
}
fragmentTransaction.commit();
}
I could provide more code, but I believe these are the necessary pieces. Please also feel free to let me know if I am going about this incorrectly. I am so close, I just can't figure out for the life of me why they are stacking.
Any help would be appreciated!
We went into Chat, and this is what we came to:
LinearLayout parentLayout = (LinearLayout) view.findViewById(R.id.videosLayout);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
LayoutInflater inflater = LayoutInflater.from(getActivity());
for (String vidURL : videoURLs) {
final String vidUrl2 = vidURL;
LinearLayout placeholder = (LinearLayout) inflater.inflate(R.layout.fragment_youtube_video, container, false);
View youtuber = placeholder.findViewById(R.id.youTubeVideo);
YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
fragmentTransaction.add(youtuber.getId(), player);
placeholder.setId(12345);
parentLayout.addView(placeholder);
player.initialize("secret", new OnInitializedListener() {
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
if (!arg2) {
arg1.cueVideo(vidUrl2);
}
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
}
});
}
fragmentTransaction.commit();
Placeholder is a LinearLayout with two children, a LinearLayout to hold the youtube fragment, and a TextView to hold the title.
I'm assuming, that when you add multiple videos, you're just swapping them within your fragment. There aren't multiple fragments, so only one video would ever show at any given time.
I think you should use a ListView instead, that way, you can populate it with numerous videos that all have the same general layout resource. Hopefully this helps.

Fragments not allowing button or activity behavior

I have a main activity that starts up when the app opens. Once the activity is started, it then opens a GridView fragment from the main activity onCreate(Also, the main activity and fragment share the same XML layout).
The problem I am having is that whenever I try to add a onClick event to my Button, nothing happens unless I remove the code that opens up the GridView's fragment from my main activity.
NOTE: I am using fragments for my GridView because I am displaying lots of images at the same time, so I've set up Fragment classes to handle them efficiently without it effecting performance.
Would there be any way around this?, cheers in advance.
Main activity:
public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "ImageGridActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.image_grid_fragment);
if (BuildConfig.DEBUG) {
Utils.enableStrictMode();
//Whenever I remove this code here:
}
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
//To here, it works
Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.w("myApp", "no network");
}
});
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView"
style="#style/PhotoGridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="#dimen/image_thumbnail_size"
android:horizontalSpacing="#dimen/image_thumbnail_spacing"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="#dimen/image_thumbnail_spacing" >
</GridView>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
I'm trying to set the Click event from the main layout as you can see
here. What could I do to fix this problem I have?
You'll never interact with the Button from the activity layout because you add the Fragment directly on the FrameLayout which holds the content, so the fragment will cover the previously set content of the Activity. You could modify the activity layout like this:
R.layout.act_ImageGridActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
followed by using the above FrameLayout when doing the fragment transaction:
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.frag_container, new ImageGridFragment(), TAG);
ft.commit();

Categories

Resources