I am new to Android Studio/Development, but not programming itself. I am struggling with the syntax (as I do think it is my problem) yet I cannot find a solution anywhere on here or on Google. I have this 2nd activity named FilterActivity. FilterActivity currently has 2 TextViews. Both create dynamic CheckBoxes. Right now, I only have one doing this so I can get one right before I go onto another. Here's the issue, I click on the TextView to get the dynamically created CheckBox and it shows perfectly fine. However, clicking on it again just adds the same values until it appears to completely fill the parent via xml.
No matter what I search, what I do, it all does the same exact thing. I know, I have nothing for onCheckedChanged, but I have previously before and it did not work either. So question(s), should I not be using LinearLayout and do like a container of sorts? There will be a decently big database for the checkboxes (500+) so I was thinking I would have to implement ScrollView at some point. Also, if LinearLayout is the correct way to go, what am I doing wrong? I am just completely spinning and I know it should not be this hard.
Thank you to anyone that gives feedback!
Here's XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="xxx.AppEx.FilterActivity"
tools:layout_editor_absoluteY="81dp"
tools:showIn="#layout/activity_filter">
<View
android:id="#+id/firstView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<View
android:id="#+id/secondView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/AllTextView"
android:clickable="true"
android:text="All"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toLeftOf="#id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<TextView
android:id="#+id/DisTextView"
android:clickable="true"
android:text="Dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:layout_toRightOf="#+id/firstView"
android:layout_alignParentTop="true"
android:gravity="center"
android:textStyle="bold"
android:textAllCaps="true"/>
<LinearLayout
android:id="#+id/CheckBoxLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/secondView">
</LinearLayout>
</RelativeLayout>
Here's the main code:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.Intent;
public class FilterActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener {
LinearLayout CheckBoxLayout;
CheckBox checkBox;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter);
CheckBoxLayout = (LinearLayout) findViewById(R.id.CheckBoxLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView AllTextView = (TextView) findViewById(R.id.AllTextView);
AllTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
AllTextView.setBackgroundColor(Color.BLUE);
onAllClick();
}
});
TextView DisTextView = (TextView) findViewById(R.id.DisTextView);
DisTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//onAllClick();
}
});
}
public void onAllClick()
{
Intent intent = getIntent();
dummy();
}
public void dummy()
{
String[] array = new String[]
{
"Rice", "Beans"
};
LinearLayout CheckBoxLayout = (LinearLayout)findViewById(R.id.CheckBoxLayout);
for (int i = 0; i < array.length; i++)
{
CheckBox checkBox = new CheckBox(this);
checkBox.setText(array[i]);
CheckBoxLayout.addView(checkBox);
}
}
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
}
Related
Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
XML:
<?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/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.
so I was working on some basics of android when I stumbled upon this. I am trying to implement a listview(having its layout stored in file eachrow.XML)and a button on my app such that when the user clicks one button "schoollife", the listview automatically imports layout stored from other file "eachrow2.XML".
the layout of eachrow.xml consists of buttons only, while the layout of eachrow2.xml consists of buttons.
Everything seems to work fine, the user clicking the button imports the other layout , but the problem starts when user tries to click buttons from the second layout. in the error logs, it is stating that the button 'subject' causing a null pointer exception
eachrow.xml:
eachrow2.xml:
===========codes========
eachrow2.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="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/subjectbutton"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:padding="1dp"
android:layout_margin="1dp"
android:textAllCaps="false"
android:background="#color/colorPrimary"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/marksbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:text="click the button to show marks of each subject"
android:textAlignment="center"
/>
</LinearLayout>
mainactivity.java:
package com.example.ansh.reportcard;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ListView l;
private String[] d_myself;
private ArrayAdapter adp;
private HashMap<String,String> d_school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.a_list);
d_myself= new String[]{"something"
};
d_school=new HashMap<>();
d_school.put("A","B");
Button B_myself= (Button)findViewById(R.id.myself);
B_myself.setOnClickListener(this);
Button B_school=(Button)findViewById(R.id.school);
B_school.setOnClickListener(this);
Button subject=(Button)findViewById(R.id.subjectbutton);
subject.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(view.getId()==R.id.myself){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow,R.id.textView,d_myself);
l.setAdapter(adp);
}
if (view.getId()==R.id.school){
adp=new ArrayAdapter(MainActivity.this,R.layout.eachrow2,R.id.subjectbutton,d_school.keySet().toArray());
l.setAdapter(adp);
}
if (view.getId()==R.id.subjectbutton){
Button tmp=(Button)view;
CharSequence x=tmp.getText();
TextView t= (TextView) findViewById(R.id.marksbox);
t.setText(d_school.get(x.toString()));
}
}
}
According to my understanding to your question, You declaring the Button from the xml file activity_main.xml, Instead you should find it from the eachrow2.xml file.
The subjectbutton is in the eachrow2.xml so you need to find the view from this file only instead activity_main.xml.
I am new in Android Learning and try to complete my task, in which i need to create the Workout simple app, with dropdown, button and text. In app preview, app is looking fine but when i run on device it gives me an error "Unfortunately, FindWorkOutActivity has stopped working."
I have searched on internet about this error and unable to understand the Logcat information as i didn't learn about this. Kindly help me to solve this error.
Below are the Files of my app:
activity_find_work_out.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.rj.findworkout.FindWorkOutActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_workout_here"
android:id="#+id/text_hello"
android:layout_centerHorizontal="true"
android:layout_below="#+id/button_id"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_id"
android:text="New Button"
android:layout_centerHorizontal="true"
android:layout_below="#+id/spinner_id"
android:layout_marginTop="20dp" />
<Spinner
android:id="#+id/spinner_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:entries="#array/work_out"
android:onClick="onClickWorkOut"
>
</Spinner>
</RelativeLayout>
FindWorkOutActivity.java
package com.example.rj.findworkout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
/**
* Created by Rj on 5/25/2017.
*/
public class FindWorkOutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_work_out);
}
public void onClickWorkOut(View view){
TextView workouts = (TextView) findViewById(R.id.text_hello);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
String workout = String.valueOf(workouttype.getSelectedItem());
workouts.setText(workout);
}
}
Your problem is the onClick property in the Spinner. The proper way is to add setOnItemSelectedListener on it instead. So first you need to remove the
android:onClick="onClickWorkOut"
and in the Activity code, add a OnItemClickListener instead.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
workouttype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I am new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be 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:orientation="vertical" >
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}
I'm relatively new to android development and I'm trying to find a way to inflate a view repeatedly each time when a button is pressed, in a different location, so every inflated view has its own position:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
View teamBox = View.inflate(this, R.layout.team_box, rlTeam);
final TextView teamBoxView = (TextView) findViewById(R.id.team_task_box);
teamBoxView.setX(0);
teamBoxView.setY(230);
}
}
The XML code of the layout:
<?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"
android:id="#+id/rlTeam">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/teamAddBtn"
android:text="+"
android:textSize="30sp"
android:onClick="createTeam"/>
</RelativeLayout>
XML code of the view that's being inflated:
<?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">
<TextView
android:layout_width="192dp"
android:layout_height="120dp"
android:id="#+id/team_task_box"
android:text="New Team" />
</RelativeLayout>
I want to use the same view to inflate multiple boxes with different coordinates in the layout. Every time I press the button to inflate the view again it inflates the box in the same coordinates so they overlap. I need to make the second box to appear to the first one's right, the third below 1st and so on, much like a grid of boxes.
Try this code and tell me whether it works. Remove the inflating of layout
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class teamCreateScreen extends Activity {
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(getApplicationContext());
if(tv.getId()>0) {
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
}
tv.setId(i);
r1Team.addView(tv, relativeParams);
i++;
}
}
Declare int i=0; as a global variable and increment it in the createTeam() method.