findViewById() returns null in fragment - java

So I think I have gone through all the answers here on stackoverflow for this problem. However, noone of them have helped.
I am currently working with the camera2 example by Google trying to add an imageview "imageOverlay" that covers the whole screen. However, imageview is always null when trying to retrieve it. Any idea what I am missing?
Edit:
What has been tried:
Moving the ImageView in the camera_fragment.xml into the FrameLayout so that I can retrieve it just like the Button. This seemed like the best idea to make it an overlay.
Using getView()
Using getActivity()
Cleaning and rebuilding.
Using View view = inflater.inflate(R.layout.camera_fragment, null);
Camera_Activity xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="com.irg.hig.imageregistrationgame.CameraActivity"
/>
camera_fragment.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.irg.hig.imageregistrationgame.CameraTextureView
android:id="#+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5" />
<FrameLayout
android:id="#+id/control"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/control_background">
<Button
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/picture" />
<ImageButton
android:id="#+id/info"
android:contentDescription="#string/description_info"
style="#android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="20dp" />
</FrameLayout>
</RelativeLayout>
CameraFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_fragment , container, false);
imageView = (ImageView) view.findViewById(R.id.imageOverlay);
return view;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
view.findViewById(R.id.picture).setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
mTextureView = (CameraTextureView) view.findViewById(R.id.texture);
//imageView = (ImageView) view.findViewById(R.id.imageOverlay);
}

The problem was that there was a second xml file in layout-v21 that was used and not the one in the Layout folder that I was trying to change.
Thanks to everyone that tried to help, it was appreciated immensely.

Try this.
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_layout, null);

Try to replace : View view = inflater.inflate(R.layout.camera_fragment , container, false);
by View view = inflater.inflate(R.layout.camera_fragment, null);

Related

Edit Custom Views programmatically

I created a layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rlMenu"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_centerVertical="true">
<RelativeLayout
android:background="#drawable/dark_rectangle_bord"
android:id="#+id/rl1dia"
android:elevation="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_width="#dimen/sizeCard"
android:layout_height="#dimen/sizeCard"
android:layout_marginBottom="#dimen/padding_bottom_cards"
android:layout_marginEnd="#dimen/padding_end_cards"
android:layout_marginRight="#dimen/padding_end_cards"
android:layout_marginLeft="#dimen/padding_start_cards"
android:layout_marginStart="#dimen/padding_start_cards"
android:layout_marginTop="#dimen/padding_top_cards">
<TextView
android:text="1º Dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1dia"
android:textStyle="normal|bold"
android:textColor="#color/Branco"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:textSize="#dimen/texto_pequeno"
android:gravity="center"
android:fontFamily="sans-serif"
android:layout_centerHorizontal="true"/>
<ImageView
app:srcCompat="#drawable/ic_calendario_1"
android:id="#+id/iv1dia"
android:layout_width="#dimen/sizeImage"
android:layout_height="#dimen/sizeImage"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
/>
After that, I had included it on my MainActivity.
<include
android:id="#+id/my_custom_view"
layout="#layout/custom_view></include>
But in this Custom View there are a RelativeLayout, a ImageView and a TextView. I need to create some Custom Views dynamically, sample as below.
How can i create this Custom View Programatically?
Ex.:
Button bt = new Button(MainActivity.this);
And how can I change the TextView, RelativeLayout, background and the ImageView programatically? Like:
CustomView cv = new CustomView(MainActivity.this);
cv.setImage(R.drawable.chips);
cv.setRlBackground(Color.WHITE);
cv.setText("Hello, World!");
You can use LayoutInflater for that
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = (View) inflater.inflate(R.layout.custom_view, null);
View email = (View) mView.findViewById(R.id.email);
yourParentView.addView(mView);
But make sure when you add view to its parent call removeAllview() method of yourParentView
Maybe you want to create an actual custom View, using include will not create a custom View but will only inject in your layout that particular xml. So, create a class (in this case extends RelativeLayout but you can use whatever suits you here)
public class CustomView extends RelativeLayout {
private TextView textView;
private ImageView imageView;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.custom_view, this);
imageView = (ImageView) findViewById(R.id.image_view);
textView = (TextView) findViewById(R.id.text_view);
}
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setText(String text){
textView.setText(text);
}
}
an xml of the layout of your custom View (custom_view.xml), you might want to use merge instead of include since you already have a parent layout (RelativeLayout in this case)
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/image_view"
android:layout_width="48dp"
android:layout_height="48dp"
/>
</merge>
add your custom view to the Activity layout like this, please note that you need to use the full name of the custom View class you're using, in this case com.example.lelloman.dummy.CustomView
<?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:id="#+id/activity_main"
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">
<com.example.lelloman.dummy.CustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
and in your Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
CustomView customView = (CustomView) findViewById(R.id.custom_view);
customView.setImage(R.drawable.some_icon);
customView.setText("Hello world");
}

Android Fragment findViewById returns Null after inflating View

I'm trying to get an activity to host a fragment, but after inflating the view of the fragment, when I try to access any widgets via findViewById(), it returns null. So in this case mBillTitle is always null, I am not sure what I am missing?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#+id/bill_title"
android:textSize="24dp"
android:gravity="left"/>
....
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+id/bill_detail_title"
android:textSize="20dp"
android:paddingTop="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
....
</LinearLayout>
...
</LinearLayout>
And this is the fragment code
#Override
public void onCreate(Bundle savedInstanceBundle) {
super.onCreate(savedInstanceBundle);
mBill = new Bill();
}
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup parent,
Bundle savedInstanceBundle) {
View view = inflater.inflate(R.layout.bill_details, parent, false);
mBillTitle = (TextView) view.findViewById(R.id.bill_title);
mBillTitle.setText(mBill.getmShortTitle());
...
return view;
}
android:text="#+id/bill_title"
suppose to be
android:id="#+id/bill_title"
Change android:text to android:id.

Can't manage to change text color on Spinner

I've tried a lot of other answers on SO, none of them seem to work for me. I'm not sure what I did wrong.
Here's my xml:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/product_category_spinner"
android:theme="#style/AppartmentBlue" />
And here's my Java (it's a DialogFragment extending class):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_product_dialog, container, false);
Spinner productCategory = (Spinner) v.findViewById(R.id.product_category_spinner);
ArrayAdapter<String> spinnerAdapter =
new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.category_array));
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
productCategory.setAdapter(spinnerAdapter);
// setCancelable(false);
return v;
}
The thing is, the text is white on very bright gray. It came that way by default, if it came in black foreground I wouldn't need to touch it. Why's it doing this? My theme extends from Theme.Holo.Light.DarkActionBar.
I don't wanna set it at runtime, I'm gonna have a lot of spinners and I want it to be global across the app, with the option for me to add later themes.
I hope this helps:
My Spinner:
<Spinner
android:id="#+id/spinner_cat"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:layout_marginStart="#dimen/left_margin_large"
android:layout_marginLeft="#dimen/left_margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
Setting up the adapters:
private final int _singleItemRes = R.layout.simple_spinner_item;
private final int _dropdownRes = R.layout.simple_spinner_dropdown_item;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(_handler.getContext(), _singleItemRes, getTitles());
adapter.setDropDownViewResource(_dropdownRes);
simple_spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#color/actionbar_text"
android:textSize="20sp"
android:textStyle="normal"/>
simple_spinner_dropdown_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/primary_dark"
android:ellipsize="marquee"
android:layout_centerVertical="true"
android:singleLine="true"
android:paddingRight="#dimen/left_margin_small"
android:paddingLeft="#dimen/left_margin_small"
android:textColor="#color/actionbar_text"/>
Let me know if I am missing anything from the answer and I will add it in.

Fragment in Android programming

I think that it's a bit difficult to explain the problem but I'll try. I have a class which extends Fragment and I call it with the follow code
frag = new SearchDoctor();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
Here is a part of class code
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout.search_doctor, container, false);
spinnerCity = (Spinner) rootView.findViewById(R.id.cityList);
The first time that class is called it's alright.
But the next time when I call it thead exit at the line "rootView = inflater.inflate(R.layout.search_doctor, container, false);".
I have understand that the problem is with the fragment that contained in search_doctor.xml (layout) because I had tried it with other layouts many times and it was any problem.
Here is the layout search_doctor.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" >
<Spinner
android:id="#+id/cityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/city_arrays"
android:prompt="#string/city_prompt" />
<Button
android:id="#+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/specialtyList"
android:layout_marginRight="16dp"
android:text="Αναζήτηση" />
<Spinner
android:id="#+id/specialtyList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cityList"
android:entries="#array/specialty_arrays"
android:prompt="#string/specialty_prompt" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ListView
android:id="#+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp" >
</ListView>
<Button
android:id="#+id/btnMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/resultList"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:text="Χάρτης" />
</RelativeLayout>
Please if someone could help me I'll be very pleasure.
Thank's a lot in advance!
This is the solution! I find on the forum so thank you all.
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
Can you try to replace
rootView = inflater.inflate(R.layout.search_doctor, container, false);
with
rootView = inflater.inflate(R.layout.search_doctor, null, false);
Not sure if it helps but if the problem with view hierarchy that might be useful.

Android - ImageView with Another Views in Universal Image Loader

I been struggling right now to customize the such that I can include another types of views in the gridview(Image). I want to achieve the layout below but in the Universal Image Loader it only displays images. Any experience with this?
Xml:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
JAva
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrls[position], imageView, options);
return imageView;
}
I tried this code but it's not working. Basically I wanted to have the layout above.
XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/icon_text"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textStyle="bold"
android:lines="2">
</TextView>
</LinearLayout>
Java
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.icon_text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(album[position].getName());
imageLoader.displayImage(albumsPhoto[position], holder.image, options, animateFirstListener);
return view;
}
ViewHolder is a custom class which is not part of Android SDK. It holds other views like TextView, ImageView and so many. This class can be used in ListView where Adapter is used. Read More to know more about Holder.
As you have layout, following can be your ViewHolder class structure.
static class ViewHolder {
public TextView text;
public ImageView image;
}
You can see that, there is TextView and ImageView in ViewHolder. You need to define such many views as your custom_list_item layout has.
Read -> ViewHolder Pattern – Caching View Efficiently
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_alignRight="#+id/ratingBar1"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/imageView1"
android:layout_below="#+id/imageView1" >
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="text1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="text2" />
</LinearLayout>
</RelativeLayout>
Hi Universal Image Loader is an example for gallery view. In order to achieve the above layout . You need view Holder class and an adapter ,, LIstadapter. You can list you images in list adapter and use ViewHolder for Image and Subtitle.
The MosT important thing will be bitmap crashing if you are loading lot of Images ,, so each tiem you scroll Do delete teh cache mem for Bit maps.

Categories

Resources