I have a problem.
I just created an app in which I can navigate through tabs and sections.
How can i tell eclipse to load me the "main.xml" layout and not to create a new Linear layout with a text view?
Is it possible guys?
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FirstTabFragment extends Fragment
{
private Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.activity = getActivity();
LinearLayout linearLayout = new LinearLayout(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(params);
TextView textView = new TextView(activity);
textView.setText("This is a sample fragment. I am programmatically added");
linearLayout.addView(textView);
return linearLayout;
}
}
My main.xml code is:
<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"
tools:context=".MainActivity"
android:background="#drawable/screenshot1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="167dp"
android:text="Button" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="44dp"
android:text="Button" />
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (RelativeLayout) inflater.inflate(R.layout.main, container,false);
}
final LayoutInflater inflater = (LayoutInflater)getSystemService("layout_inflater");
TextView textView = (TextView)inflater.inflate(R.layout.main,null);
Related
I have res/layout/main_menu_layout.xml file:
<?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="wrap_content"
android:layout_gravity="center"
>
<sidekick.cpp.CMainMenuButton
android:id="#+id/manualMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_manual_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/editorMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_editor_text" />
<sidekick.cpp.CMainMenuButton
android:id="#+id/exitMainMenuButton"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="18sp"
android:text="#string/main_menu_button_exit_text" />
</LinearLayout>
This layout is used in the class below:
package sidekick.cpp;
import android.widget.*;
import android.content.*;
import android.view.*;
public class CMainMenuLayout extends LinearLayout {
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.main_menu_layout, null);
this.addView(view);
} }
Now I want to use the layout in a fragment. If I use the layout's ID (R.layout.main_menu_layout) in onCreateView() then the below code seems to work:
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_layout, container, false);
}
...
}
But I need to use the CMainLayout class, not R.layout.main_menu_layout. The code below seems to work too but is it correct?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CMainMenuLayout layout = new CMainMenuLayout(this.getContext());
return layout;
}
First, you can inflate xml layout in CMainMenuLayout directly
public CMainMenuLayout(Context context) {
super(context);
this.setOrientation(VERTICAL);
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_layout, this, true);
}
then use CMainMenuLayout class in xml for CMainMenuFragment. For example
res/layout/main_fragment_layout.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="wrap_content">
<sidekick.cpp.CMainMenuLayout
android:id="#+id/mainMenuLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
and in CMainMenuFragment
public class CMainMenuFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
...
}
I'm trying to hide two TextViews on screens which have a smaller width than 500px.
I tried the following (this is not my complete code, but essential):
public class HeaderFooterFragment extends Fragment {
private TextView lable;
private TextView app;
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
// some code...
super.onActivityCreated(savedInstanceState);
}
public void setLableInvisible()
{
lable.setVisibility(View.INVISIBLE);
app.setVisibility(View.INVISIBLE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
}
My XML is:
<TextView
android:layout_width="wrap_content"
android:layout_height="#dimen/toolbar_height"
android:id="#+id/lable"
android:layout_weight="0.3"
android:textAlignment="gravity"
android:gravity="left"
android:text="#string/app_name"
android:textSize="#dimen/site_fontsize"
android:paddingTop="#dimen/site_paddingtop"
android:paddingLeft="#dimen/site_paddingleft"
android:textColor="#color/site_color"
android:scaleType="fitCenter"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="#dimen/toolbar_height"
android:layout_weight="0.05"
android:id="#+id/app"
android:textAlignment="gravity"
android:gravity="left"
android:text="#string/app_name_extra"
android:textSize="#dimen/apptitlex_fontsize"
android:paddingTop="#dimen/apptitlex_paddingtop"
android:paddingLeft="#dimen/apptitlex_paddingleft"
android:textColor="#color/colorAccent"
android:textAllCaps="true"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:visibility="visible"
/>
When I start the app on a Smartphone with width 480px, I get a null object reference. If i start the app on a much more bigger screen, the app isn't crashing.
Thanks in advance.
Im' not sure that the view is created yet in OnActivityCreated, Try initialize your textView in OnCreateView
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
try this and delete onactivitycreated method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
Declare TextView onCreateView instead of onActivityCreated section
View RootView; //Declare Global
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
RootView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) RootView.findViewById(R.id.lable);
app = (TextView) RootView.findViewById(R.id.app);
// Add Your staff here
return RootView;
}
Instead of this
setLableInvisible();
Use below code to hide textview
app.setVisibility(View.INVISIBLE)
Similarly user it for second textview.
Thanks
If I try to inflate a view within a fragment I am getting NULL.. For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Here I will inflate my view using the layout ID & return.
return view;
}
Whenever a button is clicked I need to create a dynamic view e.g.: button & add to the LinearLayout. I would like to perform this operation inside my fragment class like this:
public void addPlaces() {
Button button = new Button(null);
button.setText("button name");
// e.g. like adding button to enter code here linear layout
linearLayout.addView(button);
}
So, if I get inflate LinearLayout inside onCreateView and use it in add class, I'm getting NULL. How to achieve?
Declare the variable as a instance variable and then initialize Linear Layout
LinearLayout linearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
return rootView;
}
Then
public void addPlaces() {
Button button = new Button(getActivity());
// needs activity context
// fragment hosted by a activity. use getActivity() to get the context of the hosting activity.
button.setText("button name");
linearlayout.addView(button);
}
Example: Modify the below according to your requirement.
fragment1.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearlayout"
android:layout_height="fill_parent"
android:layout_above="#+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPlaces();
}
});
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}
Snap shot of my emulator
Edit :
activity-main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.fragments.Myfragment"
android:id="#+id/frag"
android:layout_above="#+id/button1"
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_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
Button b;
Myfragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new Myfragment();
fragmentTransaction.add(R.id.frag, fragment);
fragmentTransaction.commit();
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
fragment.addPlaces();
}
});
}
}
Myfragment.java
public class Myfragment extends Fragment {
LinearLayout linearLayout;
View rootView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
public void addPlaces() {
Button button = new Button(getActivity()); // needs activity context
button.setText("button name");
linearLayout.addView(button);
}
}
I am trying to use linkify ina Fragment and I can't get it to actually linkify and I can't find my documentation on how to use it correctly. If someone could help that would be great!
ContactFragment.java:
public class ContactFragment extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group,
Bundle saved) {
View V = inflater.inflate(R.layout.activity_contact_fragment, group, false);
TextView myEmail= (TextView) V.findViewById(R.id.textView1);
myEmail.setText("info#komodostudios.com");
Linkify.addLinks(myEmail, Linkify.EMAIL_ADDRESSES);
return inflater.inflate(R.layout.activity_contact_fragment, group, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
activity_contact_fragment.xml:
<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"
tools:context=".ContactFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="info#komodostudios.com" />
</RelativeLayout>
You are returning the wrong View from onCreateView method. It should be return V; instead of return inflater.inflate(R.layout.activity_contact_fragment, group, false);
I had a good ListView, but now I added Fragments, now my Code doesn't work in the onCreateMenu...
How to make my ListView back?
So here's my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView;
Bundle b = getArguments();
int Nr = b.getInt(ARG_SECTION_NUMBER);
if (Nr == 0) {
myFragmentView = inflater.inflate(R.layout.layoutexample, container, false);
} else {
myFragmentView = inflater.inflate(R.layout.another_layout_example, container, false);
}
return myFragmentView;
}
I don't understand, where to put my code (for example if I want a ListView)!
Can I use something similar to the inflater?
Please help, as a beginner I only understand half of the information, Google Developers gives me...
This is my ListView:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
That's the list_item:
<?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:padding="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
Here's the example.xml (they both look the same):
<?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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
After having inflated your view, you can add :
ListView listView = myFragmentView.findViewById(android.R.id.list);
listView.setAdapter(....);
Edit : I didn't know the id you set to your ListView. Replace android.R.id.list by R.id.listView1