onClick on an imageview within a listview within a dialog - java

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.

Related

Android onClick on Linear Layout

I have following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true"
android:onClick="openGithubUrl">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I want to add a onCLick listener on LinearLayout(#+id/list-item). Is this possible? After the onClick fired I would like to get the content of the child Textview #+id/github_url.
Yes you can set an onClickListener to the linear layout, in your use case,
listItem.setOnClickListener() in the activity.
LinearLayout listItem;
TextView tvGithubUrl;
tvGithubUrl = (TextView) findViewById(R.id.github_url)
listItem = (LinearLayout) findViewById(R.id.list_item)
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get the github_url text here
tvGithubUrl.getText().toString();
}
});

Creating a new instance of an Imagebutton with a button click

The app I am programming has a menu in which the user can pick a new building to build. The user will press "Buy" I would like this to create a new instance of the imagebutton that corresponds to that particular building. My current problem is that I only understand how to do this by setting up these in the xml file and setting them invisible until they buy it. I would like to dynamically create these buildings when they are bought. Is there a way to dynamically create an instance of an image button is this way? I don't really think including my source code is necessary here since I just need an example that works (most examples I have seen just don't work out). But if you would like to see some source code let me know.
Any help is appreciated. Thanks.
Code inside buy building button:
newColonyHut = new ImageButton(runGraphics.this);
newColonyHut.setImageResource(R.drawable.mainhut);
newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(newColonyHut);
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="0dp"
android:layout_marginRight="20dp" >
<Button
android:id="#+id/buyColonyHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_below="#+id/colonyHutPrice" />
</LinearLayout>
You can create ImageView and dynamically add to layout you specified.
sample:
in oncreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
final LinearLayout test = (LinearLayout)findViewById(R.id.test);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(your_image_here);
image.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
test.addView(image);
}
});
the activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer"
android:text="Button" />
</LinearLayout>
As you can see ImageView is created when the button is clicked in the layout and then add it to the LinearLayout dynamically.
The LayoutParams is where you set the height and width of your ImageView respected the its parent which is the LinearLayout

Button in simple ListView onClick

So I have simple listview with a list item that contains a textview, and two imageviews.
I want both of the imageviews to act as buttons. Unfortunately, I can't even get one of them to work. The list view loads but then the app crashes when either image is touched. Here is the xml for the listitem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_card">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#color/Black"
android:id="#+id/name"
android:textSize="20sp"
android:padding="16dp">
</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right">
<ImageView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:id="#+id/email"
android:clickable="true"
android:src="#drawable/ic_action_email"
android:layout_margin="8dp"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#color/LightGrey" />
<ImageView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:id="#+id/internet"
android:clickable="true"
android:layout_margin="8dp"
android:src="#drawable/ic_action_web_site" />
</LinearLayout>
</LinearLayout>
Here is the layout with the listview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvDepartments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:padding="8dp"
android:smoothScrollbar="true"
android:scrollbarStyle="insideOverlay"
android:listSelector="#color/Navy"/>
</LinearLayout>
Here is the related java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.staff_layout);
staffmemberlist = (ListView)findViewById(R.id.lvDepartments);
ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String> (getApplicationContext(),R.layout.emailbrowsingcard,R.id.name, d);
staffmemberlist.setAdapter(arrayAdapter);
mailbutton=(ImageView)findViewById(R.id.email);
mailbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"This line would appear on clicking this icon",
Toast.LENGTH_LONG).show();
}
});
}
this isnt going to work because the ImageView is in the item list so you should consider doing..
mailButton = (ImageView) row.findViewById(R.id.email);
where row is the item list. remember the ImageView isn't in staff_layout, it is in your item_layout.
to make it works you need to move this block:
mailbutton=(ImageView)findViewById(R.id.email);
mailbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"This line would appear on clicking this icon",
Toast.LENGTH_LONG).show();
}
});
to the getView() in your adapter class. and let the View get findViewById or have an instance of the item_layout view in order to reach the buttons.
you should use a custom adapter for that and in custom adapter use findViewById for finding imageviews and setting listener
this is a good tutorial: http://www.vogella.com/articles/AndroidListView/article.html

How to add fixed buttons above a ListView android?

I want to have two buttons top of a listview. If I scroll below the listview I want that the buttons stay at top. I searched through many solutions on stackoverflow however I could not find the exact one for my case.
My layout xml(mainlayout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The graphical layout produced is just what I wanted:
Here is my code to use this layout, in case I might fail to bind the Java code to layout;
public class MainActivity extends ListActivity{
Button latestButton;
Button trackedButton;
ListView lv = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
latestButton = (Button)findViewById(R.id.button1);
latestButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Latest Button!", Toast.LENGTH_LONG).show();
}
});
trackedButton = (Button)findViewById(R.id.button2);
trackedButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Tracked Button!", Toast.LENGTH_LONG).show();
}
});
this.lv = getListView();
String[] values = new String[]{"asdfa","qwerqwer","banbn"};
lv.setAdapter(new ArrayAdapter<String>(BilTrackerMainActivity.this, android.R.layout.simple_expandable_list_item_1, values));
}
however when this activity is called from my other activity my app unfortunately closed. LogCat says:
Your content must have a ListView whose id attribute is 'android.R.id.list'
But I try adding lv = (ListView)findViewById(R.id.mylist); instead of this.lv = getListView();. I could not solve this issue.
I am aware of ListView#addHeaderView but I want to solve my particular problem with using this kind of layout.
Just change your listview id to android:id="#android:id/list"
Use a RelativeLayout. Place the second button to the right of the first button using layout_toRightOf then place the listview beneath button one using layout_below. Something like this (I haven't tested it, apologies for any typos)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_toRightOf="#id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
<ListView
android:id="#+id/mylist"
android:layout_below="#id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Here is the xml you are looking for
<?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"
android:background="#FF394952">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="#android:style/ButtonBar">
<Button
android:id="#+id/btn_AddMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/newItem"
android:textColor="#FF394952"/>
</LinearLayout>
<ListView
android:id="#+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
</LinearLayout>
And when you design Activity for this dont use List activity directly use Activity and set adapter on the ListView.
ListView lv = (ListView) findViewById(R.id.game_list);
ArrayAdapter<String> listItem = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ALBUM_NAME);
lv.setAdapter(listItem);
lv.setTextFilterEnabled(true);
Use below xml file for that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mLlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mLlayout1" >
</ListView>
</RelativeLayout>
Create FrameLayout with [your ListView] and [LinearLayout with your buttons].
Do once something like this within onCreate()
LinearLayout myButtons = ...;
myButtons.bringToFront();
So, your ContentView will have a FrameLayout with two child, one of those is in foreground.
That's all. Hope it helped))

Android how to add a Linearlayout to another LinearLayout from code

I have a LinearLayout in main.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="fill_parent"
android:orientation="vertical"
android:id="#+id/mainLayout" >
</LinearLayout>
I have made another XML file, called item_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/item_bg"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:text="#string/item1"
android:textSize="30dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:gravity="right"
android:text="#string/number1"
android:textColor="#color/number_bg"
android:textSize="30dp" />
</LinearLayout>
Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
try this linearlayout inside another linearlayout add id to linearlayout layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
in oncreate() call
LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.
Use LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);

Categories

Resources