I created layout file, consist of one linearlayout and two nested linearlayout inside the main one. When I using getParent method, it select the second nested linearlayout. My target was the first one nested linearlayout. So I gives the first nested linearlayout a ID called linear_top. Then I declared in the onCreateView, test and debug, I have no luck it shows that is null.
My target was AnimatedGifImageView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Love Story"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.music.flow.lib.AnimatedGifImageView
android:id="#+id/music_anim"
android:layout_width="match_parent"
android:layout_height="76dp"
android:scaleType="fitXY"
android:contentDescription="Animation"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="#+id/music_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:numStars="4"
android:rating="3.5" />
<ImageView
android:id="#+id/btn_playmusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:src="#drawable/resume" />
</LinearLayout>
OnCreateView
linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
AnimatedGifImageView animatedGifImageView =
(AnimatedGifImageView) linearTop.getChildAt(1); /* Null Exception */
Child views are indexed from 0. In other words, the first child view is 0, the second child view is 1... and so on.
Also, why don't you just get the the ImageView by it's ID?
AnimatedGifImageView musicAnim = (AnimatedGifImageView) findViewById(R.id.music_anim);
#Override
public View onCreateView(View v, String name, Context context,
AttributeSet attrs) {
LinearLayout linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1); // NullPointer
return super.onCreateView(v, name, context, attrs);
}
Does your code look like this? You are getting a NullPointerException because in onCreateView, the parameter View v may be null, as mentioned in the docs here. Are you using fragments? If not, then don't use onCreateView. Place your code instead in onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearTop = (LinearLayout) findViewById(R.id.linear_top);
ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1);
setContentView(R.layout.activity_nested_linearlayout);
}
also, as what Sound Conception mentioned, you could just try to get the view directly by using its id instead of manually getting it using an index.
Related
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");
}
I'am trying to create a Linear Layout and an editText base on a layout.xml file here is the java code :
private EditText editText(int _intID) {
EditText editText = (EditText)findViewById(R.id.player_name);
editText.setId(_intID);
editText.setHint("Element "+_intID);
editTextList.add(editText);
return editText;
}
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
LLMain.setId(_intID);
LLMain.addView(editText(_intID));
LLMain.setOrientation(LinearLayout.VERTICAL);
linearlayoutList.add(LLMain);
return LLMain;
}
Here is the xml layout I want to use as a base :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:id="#+id/linear_player_base">
<EditText
android:id="#+id/player_name"
android:background="#drawable/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
/>
</LinearLayout>
I don't understand why when I do :
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
and then :
LLMain.setId(_intID);
I have this error : Attempt to invoke virtual method 'void android.widget.LinearLayout.setId(int)' on a null object reference
why LLMain is null ?
You must inflate a custom view with you layout and the find the linear layout in this view...
Do it like this:
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.playerLayout, null);
LinearLayout LLMain= (LinearLayout) v.findViewById(R.id.linear_player_base);
Have you loaded this xml file that you want to use as base ??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutNameHere);
I've made the next layout xml code which I call it layout_one -
<?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" >
<LinearLayout
android:id="#+id/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.
I can't seem to get an onClick event on an imageview from a class that extends Fragment. What am i doing wrong here? I'm relatively new to developing for android..
Currently, this code gives me a null pointer exception - when i add the onClickListener code
Here is my code:
ImageView mImageView = (ImageView) v.findViewById(R.id.option_icon);
mImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
these icons are placed in a listview which is placed on a Dialog:
Dialog's list view and button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/preset_list_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="#string/ok"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"/>
</LinearLayout>
items within the list view (imageview and textview):
<?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" >
<ImageView
android:id="#+id/option_icon"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/option_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp" />
</RelativeLayout>
It must be that the ImageView you're trying to find it outside the scope of whatever v refers to.
If the image view is inside a list view then you should add the onClickListener from within the adapter
The imageView that you are trying to find is not accessible from v. If the imageView is in the main layout then use findViewById without using v else first inflate the view and then use view.findViewById.
I have layout with controls:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contact_phones_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/contact_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="phone" />
<Spinner
android:id="#+id/contact_phone_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
And I want to inflate it into another layout on the fragment. Quantity of these controls depends on values in a array. Array contains controls objects. So every time onCreateView rises I filled the layout from the array:
private void addLine(PhoneLine line) {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = line.getParent();
((ViewGroup) view.getParent()).removeView(view);
layout.addView(view, layout.getChildCount() - 1);
setButtonVisible(false);
}
if line is null controls are created this way:
private void addLine() {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = inflater.inflate(R.layout.contact_phone_line, layout, false);
EditText phoneEditText = (EditText) view.findViewById(R.id.contact_phone);
Spinner phoneType = (Spinner) view.findViewById(R.id.contact_phone_type);
phoneLines.add(new PhoneLine(phoneEditText, phoneType, view));
layout.addView(view, layout.getChildCount() - 1);
}
But after that I get same values in all EditText/Spinner controls, values equal to last element in the array. What can be wrong? May be there is more pure way to add controls dynamically?
I fixed it by adding controls when onResume rises, not onCreateView. But I still don't understand why onCreateView changes all values.