I'm a beginner in android and making my first app. Here, I had taken 3 textViews and a button named Add. When I click on this button, I need to show the content of 2 textViews on third textView. I used eventListener and buttonClick event but it doesn't work. Please guide me with the code of Add button.
First create the xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/singleEmployee"
android:background="#ffffff">
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then use this Java code to done your desire action:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
tv1 = findViewById(R.id.textView1);
tv2 = findViewById(R.id.textView2);
tv3 = findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv3.setText("");
String tvValue1 = tv1.getText().toString();
String tvValue2 = tv2.getText().toString();
tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
}
});
}
Related
i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});
Your XML should look like this,
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.
I am trying to create a custom dialog class with a custom layout, but, for reasons unknown to be, I am getting a null pointer exception when trying to access the buttons for the layout. Here is the XML for the dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/enableTopRelativeLayout"
android:layout_width="460dp"
android:layout_height="wrap_content"
android:background="#drawable/dialog_background"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/enableInnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imageView1" >
<TextView
android:id="#+id/enableMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:paddingBottom="10dp"
android:text="Start service?"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/enableStrut"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_centerHorizontal="true"
android:text=" " />
<Button
android:id="#+id/noenable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_toLeftOf="#id/enableStrut"
android:background="#drawable/button_background"
android:text="No"
android:textColorLink="#color/color2"
android:textStyle="bold" />
<Button
android:id="#+id/enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_toRightOf="#id/enableStrut"
android:background="#drawable/button_background"
android:text="Yes"
android:textColorLink="#color/color2"
android:textStyle="bold" />
</RelativeLayout>
Here is the code for the class:
public class DialogStartService extends Dialog implements android.view.View.OnClickListener{
public Button yes;
public Button no;
public TextView tv;
public DialogStartService(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.start_service_layout);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
tv = (TextView) findViewById(R.id.enableMessage);
yes = (Button) findViewById(R.id.enable);
no = (Button) findViewById(R.id.noenable);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
dismiss();
}
}
I instantiate the dialog with this code:
DialogStartService enableDialog = new DialogStartService(this);
Button enable = (Button) enableDialog.findViewById(R.id.enable);
enable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff
}
});
Button noenable = (Button) enableDialog.findViewById(R.id.noenable);
noenable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff
}
});
enableDialog.show();
The stack trace indicates the NPE occurs on this line:
enable.setOnClickListener(new OnClickListener() {
My question for the experts is... why is this code causing a NPE, and what is the best way to fix it? Thank you for any input you have.
According to documentation of findViewById() method:
Finds a child view with the given identifier. Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()).
In your code you call findViewById() before show() - that's why Button enable is null.
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
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
Good day everyone,
I'm learning Android development and I try to build some dynamic lists for my application. And I'm stuck... like for 4 hours already.
I have two layouts:
1. main.xml
<?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:id="#+id/action_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
>
<Button
android:id="#+id/button_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_count"
android:text="#string/button_add" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/action_buttons"
>
<LinearLayout
android:id="#+id/action_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
action_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_list_item_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/action_list_item_edittext_drinkName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/action_list_item_title"
android:padding="10dp" />
<EditText
android:id="#+id/action_list_item_edittext_drinkPercent"
android:text="40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_edittext_drinkAmount"
android:inputType="number"
android:padding="10dp"/>
<EditText
android:id="#+id/action_list_item_edittext_drinkAmount"
android:text="0.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_button_remove"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:inputType="numberDecimal"
android:width="50dp"
android:padding="10dp" />
<Button
android:id="#+id/action_list_item_button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/action_list_item_button_remove" />
And code that creates dynamic list:
public class MainActivity extends Activity {
LinearLayout actionList = null;
private Button btnAdd;
private Button btnCalculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActivityElements();
}
private void initActivityElements() {
initAddButton();
initCalculateButton();
}
private void initCalculateButton() {
btnCalculate = (Button) findViewById(R.id.button_count);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView amount = (TextView) findViewById(R.id.action_list_item_edittext_drinkAmount);
//this retrives TextView value from first list
Toast.makeText(getApplicationContext(), amount.getText().toString(),
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initAddButton() {
actionList = (LinearLayout) findViewById(R.id.action_list);
btnAdd = (Button) findViewById(R.id.button_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout listItem = (RelativeLayout) View.inflate(
MainActivity.this, R.layout.action_list_item, null);
TextView name = (TextView) listItem
.findViewById(R.id.action_list_item_edittext_drinkName);
name.setText("Here is the Title " + actionList.getChildCount());
listItem.findViewById(R.id.action_list_item_button_remove)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionList.removeView((View) v.getParent());
}
});
actionList.addView(listItem);
}
});
}
My problem is that I need to get all data from TextView boxes (Amount, Percent), but I can retrive that data only from first item of a list (look at onClickListener for btnCalculate) and can't figure out how to do it, but tried to add 'unique ids' for view (but that brakes layout, badly), tried setting Tags but again with no luck.
Maybe anyone can give a tip? I bet there is some easy way to do it, but I'm failing to find it and google is no help here.
Thanks
I'm not really sure what exactly you are trying to do but it sounds like you are trying to take in information and populate it into a view. I would recommend having a static text box that takes in your information and builds that into a listview. A good example of how to do that is located here http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html.