My Android Application Is Giving Fragment Not Attached To Activity Error. - android

//Here Is My Code
My target is to add a custom listview on fragment and fire it on activity creation
// FragmentSongs.java
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class FragmentSongs extends ListFragment implements OnItemClickListener {
ListView listview;
String[] Songs=getResources().getStringArray(R.string.songs);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MyAdapter adapter=new MyAdapter(getActivity(),Songs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
View myview=inflater.inflate(R.layout.fragment_songs, container,false);
return myview;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getActivity(), Songs[position], Toast.LENGTH_SHORT).show();
}
}
// MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String>{
Context context;
String[] songs;
LayoutInflater inflater;
public MyAdapter(Context context,String[] songs) {
super(context,R.layout.songlist_row);
this.context=context;
this.songs=songs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.songlist_row, null);
}
TextView tv1=(TextView) convertView.findViewById(R.id.textView1);
tv1.setText(songs[position]);
return convertView;
}
}
// MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.print("Reached");
FragmentSongs songs=new FragmentSongs();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
transaction.add(R.id.fragment1,songs);
transaction.commit();
}
}
// activitymain.xml
<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="wrap_content"
android:id="#+id/activitylayout"
android:orientation="vertical">
<fragment
android:id="#+id/fragment1"
android:name="android.app.ListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//
this is the name of my fragment which i want to attach to activitymain
fragment_songs.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:baselineAligned="false" >
<ListView
android1:id="#+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="0dp"
android1:layout_weight="1" >
</ListView>
</LinearLayout>
// songlist_row.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:orientation="vertical"
android:padding="10dp"
android:background="#1A237E" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Large Text"
android:background="#C5CAE9"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:background="#C5CAE9"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/textView1"
android:layout_toRightOf="#+id/textView2"
android:background="#C5CAE9"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

There are two ways of inserting a fragment of an activity and you are using both at the same time.
The first way is:
Declare the fragment inside the activity's layout file.
But in this case the fragment will be fixed on the activity like a view.
The second form is:
Programmatically add the fragment to an existing ViewGroup.
In this case you can add the fragment at any time to the activity, replace it with another fragment, remove the fragment when you want.
You can see what I'm telling you watching this link to the Android developer and looking "Adding a fragment to an activity".
Fragments Android Developers
Note: If you decide to programmatically add the fragment do not have to reference it in the XML Activity. On the other hand these lines would thus:
FragmentSongs songs=new FragmentSongs();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
transaction.add(R.id.content,songs);
transaction.commit();
R.id.content: It is the id of the container in which you add the fragment programmatically in your case on the activitymain.xml LinearLayout. As you can see you did not like this but you linked the id of the fragment which is a mistake.
Well do not forget that you should not use two ways of adding the fragments so pick one and hope that what prompted you find it useful.

Related

I don't understand why and how i can fix soft keyboard open and close automatically in Fragment Android

I have added an AutoCompleteTextView to my android project but when I click on the AutoCompleteTextView, the keyboard appears and disappears in a second.
But if I click the AutoCompleteTextView and quickly click on some characters, the keyboard doesn't disappear.
I do not understand why. How can I fix it?
if you need another piece of code ask me.
This is the Fragment.java
package com.example.trasporti;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import com.example.trasporti.adapter.PlaceAutoSuggestAdapter;
import com.google.android.libraries.places.api.model.Place;
import com.here.sdk.core.errors.InstantiationErrorException;
import com.here.sdk.search.SearchEngine;
/**
* A simple {#link Fragment} subclass.
* Use the {#link LocationFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class LocationFragment extends Fragment {
View layoutView;
TextView text1,text2,text3;
public LocationFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static LocationFragment newInstance(String param1, String param2) {
LocationFragment fragment = new LocationFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layoutView = inflater.inflate(R.layout.fragment_location,container,false);
MainActivity mainActivity = (MainActivity) getActivity();
Context context = mainActivity.getContextF();
AutoCompleteTextView autoCompleteTextView = layoutView.findViewById(R.id.autocomplete);
autoCompleteTextView.setAdapter(new PlaceAutoSuggestAdapter(context, android.R.layout.simple_list_item_1));
// text1 = layoutView.findViewById(R.id.TextView1);
//text2 = layoutView.findViewById(R.id.TextView2);
//text3 = layoutView.findViewById(R.id.TextView3);
return layoutView;
}
}
This is Fragment layout
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center_horizontal"
android:background="#0F1D49"
tools:context=".LocationFragment">
<!-- TODO: Update blank fragment layout -->
<AutoCompleteTextView
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#color/white"
android:shadowColor="#color/white"
android:textColorHint="#color/grey"
android:textColor="#color/black"
android:textSize="20sp"
android:hint="Cerca la destinazione"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/autocomplete"
/>
<!---<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView1"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView2"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/TextView3"
android:textColor="#color/white"
android:fontFamily="monospace"
android:padding="12dp"
android:layout_marginTop="32dp"
/>-->
</LinearLayout>
I have followed up your code and make it a demo but nothing happens just like you mentioned. If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method. Please check it out below:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AutoCompleteTextView editText = findViewById(R.id.autocomplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_list_item, R.id.text_view_list_item, countriesList);
editText.setAdapter(adapter);
}
}
Your Fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center_horizontal"
android:background="#0F1D49"
>
<AutoCompleteTextView
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#color/white"
android:shadowColor="#color/white"
android:textColorHint="#color/teal_200"
android:textColor="#color/black"
android:textSize="20sp"
android:completionThreshold="1"
android:hint="Cerca la destinazione"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/autocomplete"
/>
</LinearLayout>
text_view_list_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_view_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#color/teal_200"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/teal_700" />
</LinearLayout>

showPopupMenu method not working when clickng on ImageView

I have a popup menu inside every item on a listview. When you click the imageview to the left (settings img with 3 dots) a popup menu should showup. however, I'm getting error
Could not find a method showPopupMenu(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'settings_img'
does anyone know what this error is. it is obvious that can't find the method, but is it because it can't find the class/view? if so how can I fix it? thanks
main_activity
package com.example.george.hostscanmenus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List hostList;
ArrayAdapter<String> hostAdapter;
PopupMenu popup;
String subnet = "192.168.10.";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostList = new ArrayList<String>();
//populating host addresses
for(int i = 0; i < 255; i++) {
hostList.add(subnet+i +"-aa:bb:00:cc:33:ee");
}
//inflating adapter
listView = (ListView) findViewById(R.id.scan_list);
hostAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.host_item, R.id.ip_address, hostList);
listView.setAdapter(hostAdapter);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(menuItem);
}
}
});
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.host_menu, popup.getMenu());
popup.show();
}
}
array adapter
package com.example.george.hostscanmenus;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by george on 8/11/17.
*/
public class HostAdapter extends ArrayAdapter<String> {
public HostAdapter(Context context, int num, ArrayList<String> allHost) {
super(context, 0, allHost);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
ImageView nodeHostImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.ip_address);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.mac_address);
ImageView nodeArrowImgView = (ImageView) convertView.findViewById(R.id.port_scan_arrow);
// Populate the data into the template view using the data object
nodeHostImgView.setImageResource(R.drawable.ic_computer_white_24dp);
nodeIpTxtView.setText(host.substring(0,host.indexOf("-")));
nodeMacTxtView.setText(host.substring(host.indexOf("-")));
nodeArrowImgView.setImageResource(R.drawable.ic_keyboard_arrow_right_white_24dp);
// Return the completed view to render on screen
return convertView;
}
}
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scan_list_linear"
tools:context=".MainActivity">
<!-- labels for ip / mac addres list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_label">
<TextView
android:id="#+id/host_port_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Host"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/ip_open_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20dp"
tools:text="IP / MAC"
android:paddingLeft="10dp"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/scan_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Scan"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
</LinearLayout>
<!--Listview to display scan output-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scan_list"/>
</LinearLayout>
list item xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/settings_img"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:padding="3dp"
android:src="#drawable/ic_action_more_vert"
android:onClick="showPopupMenu"/>
<ImageView
android:id="#+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_computer_white_24dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="#+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="#+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
<ImageView
android:id="#+id/port_scan_arrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"/>
</LinearLayout>
</RelativeLayout>
menu xml
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/change_icon"
android:title="change icon"
/>
<item android:id="#+id/notifications"
android:title="set notification"
/>
</menu>
You should define your settings_img on your adapter getView and add a click listener for each element.
example:
ImageView settingsImgView = (ImageView) convertView.findViewById(R.id.settings_img);
settingsImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.showPopupMenu() //or popupmenu logic in here if you want
}
});
Usually, when it comes to the adapter, I pass the activity itself instead of the context. Then I pass the activity's context to super() and keep the activity in the field variable. That way I'm able to use all public methods of the parent activity.

How To Set Layout For Android Grid View Custom Adapter

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.activity.MainActivity;
import com.nusecond.suredeal.app.suredeal.activity.Promo;
import com.nusecond.suredeal.app.suredeal.pojo.Campaigns;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ns2 on 2/4/16.
*/
public class CustomPromoAdapter extends BaseAdapter{
private Activity activity;
private LayoutInflater inflater;
private List<Campaigns> campaigns;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomPromoAdapter(Activity activity, List<Campaigns> campaigns){
this.activity=activity;
this.campaigns=campaigns;
}
#Override
public int getCount() {
return campaigns.size();
}
#Override
public Object getItem(int location) {
return campaigns.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater==null)
inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView==null)
convertView=inflater.inflate(R.layout.promo_gridlist,null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail1);
TextView tView = (TextView)convertView.findViewById(R.id.textView1);
// tView.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Campaigns ca=campaigns.get(position);
tView.setText(ca.getName());
ImageView imgView = (ImageView)convertView.findViewById(R.id.imageView1);
thumbNail.setImageUrl(ca.getImage(), imageLoader);
return convertView;
}
}
This is My Custom Adapter To Display Images In Grid View.
Now I'm Displaying only 4 Images But I need 8.
The Text View Also Showing Not Properly.
I Don't Know Where I Need To Change I mean In My Adapter Or In My Layout File.
I Tried To Set LayoutParms But It Won't Work properly.
I'm new To Android Can Any One Help me...
Below Is My Layout....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/thumbnail1"
android:layout_width="88dp"
android:layout_gravity="center"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
I'm Getting The Images And Text From Rest Setver..
Below is My Main Layout....
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".suredeal.activity.Promo" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="2" >
</GridView>
</RelativeLayout>
This is My Main Layout..
This is What I'm Displaying With The Above Code..
This is What I'm Trying To Do...
Any Help Thankful To Them.......

Fragment UI not getting displayed

Activity Layout : activity_text_entry.xml
<?xml version="1.0" encoding="utf-8" ?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/fragmentContainer_TextEntry"
/>
Fragment Layout : fragment_text_entry.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="vertical"
>
<EditText
android:id="#+id/Text_Entry_Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/choose_date_text"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<requestFocus />
<EditText
android:id="#+id/Text_Entry_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/choose_title_text"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" >
</EditText>
<EditText
android:id="#+id/Text_Entry_Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="100"
android:gravity="left|top"
android:hint="#string/enter_text_here"
android:inputType="textMultiLine" >
</EditText>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center|bottom"
>
<Button
android:id="#+id/Text_Entry_Button_Save"
android:text="#string/action_Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
<Button
android:id="#+id/Text_Entry_Button_Cancel"
android:text="#string/action_Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</LinearLayout>
</LinearLayout>
Activity : TextEntryActivity
package com.app.journal_v001;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
public class TextEntryActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_entry);
FragmentManager m_fm_text_entry_activity = getSupportFragmentManager();
Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);
if(m_f_text_entry_activity == null)
{
m_f_text_entry_activity = new Fragment();
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
}
}
}
Fragment : TextEntryFragment
package com.app.journal_v001;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class TextEntryFragment extends Fragment {
//private Button m_btn_Text_Entry_Save;
//private Button m_btn_Text_Entry_Cancel;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflate,ViewGroup parent,Bundle savedInstanceState)
{
View v = inflate.inflate(R.layout.fragment_text_entry, parent, false);
return v;
}
}
I am trying to render TextEntryFragment(fragment) from this TextEntryActivity(activity).
I have kept a series of toasts in various places to track and I can see my code is getting executed till FragmentManager's FragmentTransaction and after that it does not seem to call the Fragment's onCreate/onCreateView.
Can anyone point me whats wrong ?
When I execute I am not able to see the fragment UI being rendered.
remove
if(m_f_text_entry_activity == null)
{
m_f_text_entry_activity = new Fragment();
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
}
and replace with
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
==================EDIT==================================
Also
TextEntryFragment m_f_text_entry_activity=new TextEntryFragment();
instead of
Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);

Fragment and Toggle Button

I am very new to Android. I am trying to add a simple toggle button to a class which extends Fragment class.
I tried everything but I could not get it working. I managed to add a normal button but that is not useful for me.
All I want is add a toggle button.
Please help me.
Thanks all
<?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="#cccccc" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Light Tab"
android:textColor="#333333"
android:textSize="20sp" />
<Button
android:id="#+id/fragment_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="61dp"
android:text="#string/btn_fragment" />
</RelativeLayout>
package com.baris.smartgame;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Light extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.light, container, false);
((Button) view.findViewById(R.id.fragment_button))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
R.string.toast_you_just_clicked_a_fragment,
Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
Use This as an Example :
<LinearLayout
android:id="#+id/txtDayTypes"
android:layout_below="#+id/main_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tglDay1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Saturday"
android:textOn="Saturday"
android:textOff="Saturday"
android:checked="true"/>
</LinearLayout>
and you can Access it by :
if(currentButton != R.id.tglDay1) ((ToggleButton)findViewById(R.id.tglDay1)).setChecked(false);

Categories

Resources