app keeps crashing when ever the button is pressed, tried on emulator as well as on a phone. I'm using Android Studio 3.1.2. How can i fix it, i've tried most of other solutions with same problems over here but didnt work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton(View v) {
display(1);
}
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
}
here's its xml code
<?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=".MainActivity">
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="32sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="123dp"
android:id="#+id/quantity"
android:text="0"
android:textAllCaps="true"
android:textSize="32sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDER"
android:textSize="32sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="215dp"
android:onClick="onButton"/>
</RelativeLayout>
do the below:
qt.setText(String.valueOf(n));
// or use qt.setText("" + n);
instead of
qt.setText(n);
It is because
setText(#StringRes int resid)
takes the parameter as "int resId" which it expects to be the id of the String value from the Strings.xml file.
But here you want to display the "int" value in the textview, so you need to use the overloaded method:
setText(CharSequence text)
method instead, and convert your int value to the String using
String.valueOf(n) //or by ("" + n)
Change this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity;
qt.setText(n);
}
To this:
public void display(int n) {
TextView qt = findViewById(R.id.quantity); //add closing bracket
qt.setText("" + n); //makes 'n' a string
}
Related
So I am a begginer & I made a simple CGPA calculator. Everything is working great but I wanna add grades in TextView when each EditText gets a value. So I want to add one or multiple condition to display grades in tv textview when show_grades button is pressed. For Instance if user inputs 4.00, then I want to print grade A+ in tv textview.
As you can see, I have added a simple if statement, but I hade to make 4 individual if statement just to display a single grade in 2 fields. If I want to do it for 8 fields and 12 grades(like A+, A, A-, B+, B, B-....) I have to make 96 statements. Is there a better way to do so.
///import & stuffs
public class MainActivity2 extends AppCompatActivity {
EditText S1, S2;
TextView tv, tv2;
Button show_grade, resetButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
S1 = findViewById(R.id.S1);//edittext assigning section
S2 = findViewById(R.id.S2);
tv = findViewById(R.id.tv);
tv2 = findViewById(R.id.tv2);
show_grade = findViewById(R.id.show_grade);//button assigning section
resetButton = findViewById(R.id.resetButton);
show_grade.setOnClickListener(view -> {
double s1 = Double.parseDouble(S1.getText().toString());
double s2 = Double.parseDouble(S2.getText().toString());
double g1 = 4.00, g2 =3.50;
if(s1==g1 ){
tv.setText("A+");
}
if(s1==g2 ){
tv.setText("A-");
}
if(s2==g1 ){
tv2.setText("A+");
}
if(s2==g2 ) {
tv2.setText("A-");
}
});
resetButton.setOnClickListener(view -> { //executing reset function
S1.setText("");
S2.setText("");
});
}
}
And here is activity's layout for 1 EditText & Textview only
<?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"
tools:context=".MainActivity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="HardcodedText">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="start"
android:orientation="horizontal">
<EditText
android:id="#+id/S1"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:autofillHints="creditCardNumber"
android:ems="10"
android:backgroundTint="#color/colorPrimary"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="#string/S1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<TextView
android:id="#+id/tv"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="10sp"
android:background="#color/color1"
android:hint="Grades"
android:text=""
android:textSize="18sp"
android:textAppearance="#color/colorPrimary2" />
<EditText
android:id="#+id/S2"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:autofillHints="creditCardNumber"
android:ems="10"
android:backgroundTint="#color/colorPrimary"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="#string/S1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<TextView
android:id="#+id/tv2"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="10sp"
android:background="#color/color1"
android:hint="Grades"
android:text=""
android:textSize="18sp"
android:textAppearance="#color/colorPrimary2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="start"
android:orientation="horizontal">
<Button
android:id="#+id/show_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="5dp"
android:backgroundTint="#color/colorPrimary"
android:text="Show grade"
android:textColor="#android:color/white"
tools:ignore="ButtonStyle" />
<Button
android:id="#+id/resetButton"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:text="Reset"
android:textColor="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
OK. if you want to setText on some event like button click than
TextView tvText;
EditText editText;
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String str = editText.getText().toString().trim();
tvText.setText(str);
}
}
);
But, if you want textview to show/update value as per change in EditText without
an event than use TextWatcher():
editText.addTextChangedListener(new TextWatcher()
{
String inputText;
#Override
public void afterTextChanged(Editable mEdit)
{
inputText = mEdit.toString();
tvText.setText(inputText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int
after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int
count){
}
});
In your code change
tv.setText("Your CGPA is: " + ans);
to
tv.setText("Your CGPA is: " + String.valueOf(ans));
TextView accepts String only
I have tried to search in android developers but I had no luck.
My onClick method does not print any message. I want to change my text from ... to ...; however nothing happens when I click it.
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the Cookie button is clicked.
*/
public void eatCookie(View view) {
display("I'm so full");
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(String status) {
TextView statusTextView = (TextView) findViewById(
R.id.status_text_view);
statusTextView.setText("" + status);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<TextView
android:id="#+id/status_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="I'm so hungry"
android:textColor="#android:color/white"
android:textSize="34sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="EAT COOKIE"
android:onClick="eatCookie"/>
</LinearLayout>
Your code is working correct in my device
Try to Clean your project and re run the code again.
Build -> Clean
and then run the code
I target my app to work with API 11 or above. In Activity A, I use the following code lines to capture and send TEXT in Clipboard to Activity B (which is the previous activity).
onCreate:
wvContent = (WebView) findViewById(R.id.wvContent);
LinearLayout ll= (LinearLayout)findViewById(R.id.layout_view);
webkit=new Webkit(this);
wvContent.addView(webkit, 0,0);
WebviewSelectedText webkitSelectedText=new WebviewSelectedText(getApplicationContext(), webkit);
webkitSelectedText.init();
ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
And the copied text to trigger Activity B (back to previous activity with text from clipboard):
public class WebviewSelectedText
{
private Context context;
Webkit webkit;
private final String have_word="have_word";
public WebviewSelectedText(Context context,Webkit webkit)
{
this.context=context;
this.webkit=webkit;
}
#SuppressWarnings("deprecation")
public void init()
{
final ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(null);
setOnSelectTextListener(clipboardManager, webkit);
}
private void setOnSelectTextListener(#SuppressWarnings("deprecation") final ClipboardManager clipboardManager, final Webkit webkit) {
webkit.setOnSelectTextListener(new OnSelectTextListener() {
#SuppressWarnings("deprecation")
public void onSelectText() {
String text = clipboardManager.getText().toString();
text = text.toLowerCase();
//Remove all non-word elements starting and/or ending a string
String strippedInput = text.replaceAll("^\\W+|\\W+$", "");
System.out.println("Stripped string: " + strippedInput);
//receivedText = txtView.getText().toString().toLowerCase();
if(strippedInput!=null)
{
Intent intent=new Intent(context,ActivityB);
intent.putExtra(have_word, strippedInput);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
ActivityA.this.startActivity(intent);
}
}
});
}
Everything is just fine on Emulator, i.e., text is copied and then Activity B is called with the text from Clipboard.
HOWEVER, on my real device (an HTC and a Tablet), Activity B with the copied text does not automatically start until a soft button is touched (any button: back, menu...).
And FYI, here is my layout:
<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:id="#+id/layout_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:layout_weight="1">
<WebView
android:id="#+id/wvContent"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/home"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnPronounce"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/pronounce"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnShowHistory"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/history"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnAddFavourite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/add_favourite"
android:layout_weight="0.25"
/>
</LinearLayout>
I wonder if you can have a look at my code and tell me what the problem is. Thank you very much.
I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here
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.