Adding buttons on xml file other than main - java

I have two xml files in my application - main.xml and options.xml.
In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that with options.xml: if I write
Button b = (Button)findViewById(R.id.b1);
, b is going to be null. What is the cause of this problem and how do I fix it?
Thank you in advance.

You need to either inflate the options.xml or set it as content view:
setContentView(R.layout.options);
before you can use the views in that layout file.
It sounds like you want to be able to access both layouts so you should do something like this:
View view = LayoutInflater.from(context).inflate(R.layout.options, null);
Button b = (Button) view.findViewById(R.id.b1);

Related

Android checkbox.isChecked is not working

I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

How to add a scrollbar in a EditText dynamically/programmatically in Android?

I know how to adda scrollbar through the xml properties such that there is a bar in the right side of the edittext field once we write stuff in it by making setSingleline(false).
My question is :-
is there a way to do the same through the activity , that is, dynamically?
for eg:-
Code is :-
public class abc extends Activity
{
EditText edit = (EditText)findViewById(R.id.EditText1);
edit.setVerticalScrollBarEnabled(true);
edit.isVerticalScrollBarEnabled();
}
These two does not work. When i add these , i still am not able to see the scrollbar on the rightside of the Edittext when i scroll..
Through XML, when we add :- android:scrollbars="vertical" , then we are able to see the scrollbar..
I am asking for a WORKING way to do it through the activity dynamically or in other words programatically
Please its urgent. I would sincerely be thankful.
In XML
android:fadeScrollbars="false"
Dynamically/programatically:
edit.setScrollbarFadingEnabled(false);

Dialog context issue - Android

I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?
You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.

Dynamically adding textbox in android

I am working on an app and need to add a textbox to the View when a button is selected. How can I do that or add any object dynamically. What class do I use or what method do I need to call? Thanks.
You just need to call the addView method to the target view. It's a method inherited from ViewGroup, see [here][2].
[2]:http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, android.view.ViewGroup.LayoutParams)
One possibility is to define it in your layout XML, and set:
android:visibility="gone"
Then in your code you can do:
TextView myTextBox = (TextView) findViewById(R.id.myTextBoxId);
myTextBox.setVisibility(View.VISIBLE);
and to hide it again:
myTextBox.setVisibility(View.GONE);
Here is the documentation

Categories

Resources