Linear Layout
I am trying to create a LinearLayout on the main screen to act as a menu. I didn't want Buttons or have to create images, I like the look I get with the TextViews and Layouts. My problem is I cannot get the onClick() to work past the first layout, its been driving me nuts the past couple days. Is there something small I'm missing or am I doing it all wrong?
Main.Java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
}
}
}
Main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="6dip" >
<LinearLayout
android:id="#+id/main_btn_computer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="5" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Operating Systems"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/light_blue" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Windows, Linux, Dos, and Mac"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/grey_font" />
</LinearLayout>
<LinearLayout
android:id="#+id/main_btn_networking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="10" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Networking"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/light_blue" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cisco, HP Procurve"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/grey_font" />
</LinearLayout>
</LinearLayout>
Why are using onClickFeature(View v) it's onClick(View v).
are setting your onClickListener()?
Call your LinearLayout
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
Set the onClick Listeners
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
Remove the android:onClick="" attributes
Related
Iam Developing an android app in which one of its module is of Quiz.I want to show Tick image on button when someone click the right answer and wrong when wrong ans is seleceted, I want to show these tick and cross images on the right most of my button.Right now Iam displaying the toast for right and wrong answer.Also I want to show the next quiz question on the same activity when user click the next or previous button.I dont want to create another activity for next question rather want to show the next question on the same activity.
the sample image like what I want to do is like this :
enter image description here
Code of Quiz xml is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/empty"
tools:context=".activity.QuizActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="vertical">
<TextView
android:id="#+id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quiz"
android:textStyle="bold"
android:textSize="#dimen/_30ssp"
android:textColor="#FFF"
android:gravity="center_horizontal"/>
<TextView
android:layout_below = "#id/quiz_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Which was the first non test playing country to beat India in an international match?"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/afghan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Afghanistan"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
<Button
android:layout_below = "#id/afghan"
android:id="#+id/bang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Bangladesh"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"></Button>
<Button
android:id="#+id/srilanka"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Srilanka"
android:textStyle="bold"
android:textColor="#fff"
android:gravity="left|center_vertical"
android:textAllCaps="false"
android:textSize="15sp"
android:background="#drawable/button_border"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/left_arrow"
android:layout_gravity="bottom"
android:layout_margin="#dimen/_20sdp"/>
<ImageView
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:background="#drawable/right_arrow"
android:layout_gravity="bottom"
android:layout_marginBottom="#dimen/_20sdp"
android:layout_marginLeft="#dimen/_150sdp"/>
</LinearLayout>
</LinearLayout>
Code for Activity is :
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.vshine.neuron.riseshine.R;
public class QuizActivity extends AppCompatActivity {
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
btn1 = findViewById(R.id.afghan);
btn2 = findViewById(R.id.bang);
btn3 = findViewById(R.id.srilanka);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Sorry! Wrong Answer",Toast.LENGTH_LONG).show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Congratulation! Right Answer",Toast.LENGTH_LONG).show();
}
});
getSupportActionBar().setTitle("Quiz");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
}
I'd suggest you do couple of things.
Instead of hardcodding your buttons' onclick methods, try determining which button was clicked and was it the write answer or not.
You can make a method which will check if the clicked button holds the right answer or not.
For reusing the activity, you can simply update the textview (that holds the question) and the button's texts.
I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?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">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}
I'm building a Contact app using ListView. My ListView has 2 button. In this app, to test the respond ability of the buttons, I intended to set the button "Edit" so that when I click on it, it will change to "Clicked", and the button "Delete" will change to "Clicked", too. When I ran the Debug, the app stopped working and I couldn't get it work again (before I added the onClickListener, this app had worked property).
I don't know what is the error, and have tried many ways to fix.
Here is my row_listview.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="match_parent" android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:src="#drawable/if_male3_403019" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="Akai Shuichi"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="0982xxxxxx"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"
android:weightSum="2"
android:orientation="vertical">
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#3FE0FF"
android:onClick="myClickHandler"
android:text="Edit"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#F73131"
android:onClick="myClickHandler"
android:text="Delete"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
And here is my MainActivity.java:
package com.huy9515gmail.mycontact;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.edt_inputName) EditText edtName;
#BindView(R.id.btnAdd) Button btnAdd;
#BindView(R.id.btnEdit) Button btnEdit;
#BindView(R.id.btnDelete) Button btnDelete;
#BindView(R.id.edt_inputNumber) EditText edtNumber;
#BindView(R.id.rdbtn_male) RadioButton rdbtn_male;
#BindView(R.id.rdbtn_female) RadioButton rdbtn_female;
#BindView(R.id.rdbtn_others) RadioButton rdbtn_others;
#BindView(R.id.gender) RadioGroup genderSelection;
private ListView lvContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
lvContact = (ListView) findViewById(R.id.lv_contact);
final ArrayList<Contact> arrContact = new ArrayList<>();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "") || ((rdbtn_male.isChecked()==false) && (rdbtn_female.isChecked()==false) &&(rdbtn_others.isChecked()==false))) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, R.layout.row_listview, arrContact);
lvContact.setAdapter(customAdapter);
}
});
}
public void myClickHandler(View v) {
LinearLayout vwParentRow = (LinearLayout) v.getParent();
Button btnEdit = (Button) vwParentRow.getChildAt(0);
Button btnDelete = (Button) vwParentRow.getChildAt(1);
btnEdit.setText("Clicked");
btnDelete.setText("Clicked");
}
}
And here is the row_listview.xml layout:
Instead of setting the android:onClick in XML, do the same as you did for btnAdd.
Find the view by id or use butterknife, then put the following in onCreate()
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
Make sure you have an element in your XML file with the id like this:
android:id="#+id/btnAdd"
i am trying to write a program that changes the background color of the screen to the color that i decided.
i wrote something like that but when it runs it crashes
what is sthe problem,please help me.here is the xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#FFFFFF"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="White" />
</LinearLayout>
</LinearLayout>
and here is the .java code
package com.example.flashlight;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class FlashLight extends Activity {
Button red,green,blue,white;
LinearLayout view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
red=(Button) findViewById(R.id.button1);
green=(Button) findViewById(R.id.button2);
blue=(Button) findViewById(R.id.button3);
white=(Button) findViewById(R.id.button4);
red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.RED);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_flash_light, menu);
return true;
}
}
you need to assign an id to your LinearLayout
<LinearLayout
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#FFFFFF"
then initialize your view
view = (LinearLayout) findViewById(R.id.view)
red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.RED);
}
});
you are not assigning the view variable to anything, that is probably causing a NullPointerException in your code
try adding in your XML file this line under the LinearLayout:
android:id="#+id/view"
and adding this line to your onCreate:
view = (LinearLayout)findViewBiId(R.id.view);
Try this code
This will work for me...
In youyr xml file set id for your Linear Layout as a view.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:id="#+id/view"
android:background="#FFFFFF"
>
In Your java file your linear layout view is null because of id.
so put below line in your java file
view = (LinearLayout) findViewById(R.id.view);
This will work for me....
Happy coding..
After onClick use this below code:
view.setBackgroundColor(Color.DKGRAY);
Example:
public void onClick(View v) {
view.setBackgroundColor(Color.DKGRAY); //Add any color you want
}
Hi I have generated a dynamic list. I have SeekBar and a ToggleButton for each of the rows in List. I have been trying to generate events for the toggle and seek from the last 1 week but in vain. Please help me with it.
Here is the XML code with the List(smartunits.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/unitsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid_gradient"
android:orientation="vertical" >
<ListView
android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Now the code for each row(listunits.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" >
<TextView
android:id="#+id/unitName"
android:layout_width="40px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="20px"
android:textColor="#ffffff" />
<TextView
android:id="#+id/unitCodeId"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#ffffff"
android:visibility="invisible" />
<SeekBar
android:id="#+id/unitsseek"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:background="#drawable/off_grey"
android:textOff=""
android:textOn="" />
</LinearLayout>
Here is the code for ListActivity
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SimpleCursorAdapter;
import android.widget.ToggleButton;
public class UnitsListActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// put the bottom code here
setContentView(R.layout.smartunits);
Bundle extras = getIntent().getExtras();
final AutomationDBAccessor db = new AutomationDBAccessor(this);
Cursor cUnits = db.getUnits(extras.getInt("roomFloorId"), extras.getInt("roomId"));
String[] from = new String[] { AutomationDBAccessor.colUnitName, AutomationDBAccessor.colUnitCodeID };
int[] to = new int[] { R.id.unitName, R.id.unitCodeId };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.listunits, cUnits, from, to);
this.setListAdapter(sca);
View viewUnits = LayoutInflater.from(getBaseContext()).inflate(R.layout.listunits, null);
final ToggleButton toggle = (ToggleButton) viewUnits.findViewById(R.id.toggle);
toggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) { // TODO Auto-generated method stub
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
});
}
}
Now I am unable to handle the ToggleButton events.
I never try this, But you can do something like,
In your listunits.xml
<ToggleButton
android:id="#+id/toggle"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:textOff=""
android:textOn=""
android:onClick="toggleButtonClick" />
And in your UnitsListActivity
write this method..
public void toggleButtonClick(View view)
{
ToggleButton toggle = (ToggleButton) view;
if (toggle.isChecked()) {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.on));
} else {
toggle.setBackgroundDrawable(getResources().getDrawable(R.drawable.off_grey));
}
}
Run this code and let me know what happen..