Android- Cannot Cast from View to Button - java

Okay, I'm new to both java and android programming, but I've come across the most frustrating error ever and I can't seem to be able to find the solution. here is where the error is at.
final Button button = (Button) findViewById(R.id.activity_button);
And here is the XML of the button itself.
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
I'm not seeing the problem, I imported import com.awesometech.uselessbutton.R; which didn't help. Any suggestions? I'm using Eclipse.

Change to
final Button button = (Button) findViewById(R.id.button_main);
You have
<Button
android:id="#+id/button_main" // id is button_main not activity_button
Probably activity_button id belongs to another view and you are casting it to a Button.

In your main Activity, in the findViewbyId() method, you pass the ID of your Button which you declared in you xml.
So your button code is :-
<Button
android:id="#+id/button_main"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/button_text" />
here , id is button_main
so in you Class, you need to do
final Button button = (Button) findViewById(R.id.button_main);//Button ID goes here

Related

How to add an Icon to a Button in Android from Java?

I want to add an icon to a Button that I created from Java, after hours I didn't find a solution yet...
I have something like this:
Button button = new Button(context);
And I'd like to have something like this:
button.setIcon(Icon myicon);
Is this possible? ;)
You can use the MaterialButton:
MaterialButton button = new MaterialButton(this);
button.setIcon(ContextCompat.getDrawable(this,R.drawable.ic_add_24px));
button.setText("Button");
If you want a Button with image then in XML you have ImageButton that have attribute of drawableLeft/drawableRight..etc.
So you can set in from there. In java I think you can set in the same way by calling setResourceDrawable.
you could also setIcon an XML file, Please try this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:drawableRight="#drawable/ic_icon" <!--also have drawableLeft:-->
android:drawablePadding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

How do I make android:textIsSelectable="true" work with my intent?

I'm having trouble making my textview editable - while I can make it editable, I can't pass the text to another activity via an intent.
In the xml of my View activity, for my textViewComment textbox, I have:
<TextView
android:id="#+id/textViewComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/textboxes_for_contact"
android:textIsSelectable="true"
The text in the textbox is indeed selectable, I am able to copy it with a long press.
However, I have an Edit activity - when an edit button is clicked in View activity, the text in textViewComment should be passed to this Edit activity with an intent. Instead, the Edittext in my EditView is empty.
When I remove android:textIsSelectable="true" the text is passed via the intent successfully. But... I can't edit it in the textview.
I also tried it programmatically with commentname.setTextIsSelectable(true); but this gives the same problem.
In ViewContact class I have
i.putExtra("comment", commentname.getText());
In EditContact class:
comment = i.getStringExtra("comment");
The xml in my EditContact class for textViewComment is:
<EditText
android:id="#+id/textViewComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:background="#drawable/textboxes_for_contact"
android:maxLength="360"
/>
Any idea how I can make the text in ViewContact selectable while also passing the intent successfully? Is this a known issue?
Replace:
i.putExtra("comment", commentname.getText());
with:
i.putExtra("comment", commentname.getText().toString());
and see if you have better luck.

Android Single line textView (for a calculator app) that takes input from buttons, is this an issue?

I am a beginner trying to make a calculator app in Android Studio that takes input from buttons. This is proving to be much more difficult than I thought it would be compared to just using EditText, but it has been a great learning experience so far.
I am working on the display portion of the app for which I am using a textView. I have it set up so that when a button is clicked, the button's value is appended to the textView. It's a calculator, so I want the textView to be displayed on a single line and shift horizontally for new input when the width of the textView is full.
I found a way to do this from another person's post that works by setting the following in the XML:
android:inputType="text"
android:maxLines="1"
When I did this it does exactly what I want, but I get an IDE warning that says:
Warning: Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?
Without the android:inputType="text" piece of code, the textView doesn't seem to scroll properly and I don't want to use an EditText. Is this something to worry about? Is there a better way to do this?
Thanks for the help.
Do not use inputType in TextView.
From this link - https://youtu.be/HYt1Ntu89X4
What I understood is you want the text to scroll dynamically i.e. on Button press.
You can do it like this :
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hzw"
android:fillViewport="true"
android:layout_gravity="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:maxLines="1"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="end"/>
</HorizontalScrollView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/b"
android:text="Add Text"/>
Java
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText("123+123");
hzw = (HorizontalScrollView)findViewById(R.id.hzw);
b = (Button)findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv1.append("d+1+cff");
hzw.post(new Runnable() {
#Override
public void run() {
hzw.fullScroll(View.FOCUS_RIGHT);
}
});
}
});

Android - Button covering TextView in RelativeLayout only in version 5.0+

The target I need to achieve: 2 lines of text and 1 straight line in between should always be on top of Button. So in such a case, setting text in Button's attribute is not applicable according to my knowledge. Therefore I use TextView and View aligning on top of Button to achieve it.
Problem: A strange UI appearance I have come across of Android 5.0+ is that when the Button is enabled, it covers TextView and View even it comes before TextView in RelativeLayout xml file. (according to my knowledge, this means that TextView should on top of Button) The following is my xml code:
<RelativeLayout
android:id="#+id/rl_daily_check_in_button_container"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginBottom="4dp"
android:layout_centerHorizontal="true"
android:layout_above="#+id/textview_check_in_days_in_row">
<Button
android:id="#+id/button_daily_check_in"
android:layout_width="140dp"
android:layout_height="140dp"
android:background="#drawable/daily_checkin_button" />
<TextView
android:id="#+id/textview_check_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/view_line"
android:layout_marginBottom="4dp"
android:text="#string/title_not_yet_daily_check_in"
android:textColor="#color/text_gray"
android:textStyle="bold"
android:textSize="36sp" />
<View
android:id="#+id/view_line"
android:layout_width="94dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_above="#+id/textview_check_in_points"
android:layout_marginBottom="6dp"
android:background="#color/text_gray" />
<TextView
android:id="#+id/textview_check_in_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/points_can_get_daily_check_in_placeholder"
android:textColor="#color/text_gray"
android:textSize="14sp" />
</RelativeLayout>
But when the app is in Android 5.0-, say 4.4.2, the UI is exactly what I expect. I have tried to set android:elevation="xxdp" attribute, it success until I set the background of Button programatically. Can anyone explain the reason and how the Andoird 5.0+ render its UI? Many Thanks!
Button on Andoird 5.0+:
Button on Android 5.0-:
From Android Lollipop (5.0), the Button has a default StateListAnimator. It has set the default android:elevation and android:translationZ attributes to the Button. Since the Button has default elevation set to it, it appears above the other Views that doesn't have elevation set (Views that has elevation less than the Button, to be precise). i.e. The Views that has higher elevation set to it will appear above the Views that has lower elevation set.
So that's why, when you changed the Button to ImageView, the problem is solved, because ImageView has no default elevation set.

Android crash on button.setEnabled(false)

Ok so I finally figured out the source of my problem. After a bunch of tests and trying out different things I noticed that after I put the codes
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
in onCreateView, it would crash upon startup. This is in my menu1_Fragment.java and is my first fragment. In my class I start off by defining my buttons.
Button button1;
Button button2;
Button button3;
Button button4;
Then the first thing I do in onCreateView is run the function setButton();
public void setButton() {
button1 = (Button) rootview.findViewById(R.id.upgbutton);
button2 = (Button) rootview.findViewById(R.id.upgbutton1);
button3 = (Button) rootview.findViewById(R.id.upgbutton2);
button4 = (Button) rootview.findViewById(R.id.upgbutton3);
}
What happens here is that these buttons are actually in my menu2_layout.xml which is my second fragment but my rootview allows me to find it. Then after I defined my buttons right below I set all my buttons to false as seen in the first bit of code because I don't want the user to be clicking the buttons yet. This is where I found that it crashes upon start. Here is the code for my buttons.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state3_xml"
android:id="#+id/upgbutton1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state4_xml"
android:id="#+id/upgbutton2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state5_xml"
android:id="#+id/upgbutton3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I gave all my buttons their own selector_state because that is what initially I thought the problem was. Here is the code for my selector.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"></item>
<item android:drawable="#drawable/buttonclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/buttongray" android:state_enabled="false"></item>
</selector>
If you can please help I have been stuck on this issue for a really long time and made more posts then I need to, but I just can't seem to solve it. Also I cannot access my logs as I am having technical issues which basically freeze my android studio when I try to click android monitor and my app is running.
If all this isn't enough I also posted on github
https://github.com/BeniReydman/Slide_Menu-Slide_Menu
I will try to solve as much questions as you may have, but I am new and this is my first App. I haven't even been doing this for over a week so please bear with it.
What happens here is that these buttons are actually in my
menu2_layout.xml which is my second fragment but my rootview allows me
to find it.
I would usually say post the error but this has thrown me,
rootview is menu1_layou.xml, the buttons are in menu2_layout.xml.
So you need to call findViewById() on the View that contains the ID's you are looking for.
Yes the code will compile and your IDE will not throw any errors but that doesn't mean it will work.
If you are still having problems after you resolve this then please post the crash logs.

Categories

Resources