how to use the widgets assigned in a activity in another activity? - android

I am creating an android project . In that i i got the ids from the XML file and wrote the click events in a (class or activity) . I want to use the widgets from another class without getting the id's again. like (Button btn=(Button)findViewById(R.id.button1);) i want to use this code in one class
But I want to use the Button btn in another class and also the click event should work.

If you want to use any View in more than one Activity you can create a class BaseActivity that extends Activity and include all the common Views in that Activity and then extend this BaseActivity to all the Activity in which you want to use common views/layouts/headers/footers. For a Pseudo code you can check my answer here.

Related

Method calls to Fragment from MainActivity

On an Android Project,
MainActivity with ViewPager and 3 Fragments:
1st page is fragment FindFrag.
Currently search query and display of results as list (in FindFrag) is coded in MainActivity:
accessed EditText in FindFrag from MainActivity, fetched query, formatted result, and displayed results in FindFrag from MainActivity ( the codes are written in MainActivity Class)
Is this the correct method?
1) Should I write it in FindFrag Class ?
2) How can I call methods in FindFrag from MainActivity (hook a method to an event in MainActivity) ?
3) Shall I use OnFragmentInteractionListener?
How ?
4) How to access views in fragments within the fragment class itself ?
My app works successfully. My question is whether this is correct style/method in Fragment - Activity Interactions
U need to code search query and display results in FindFrag , answer of How is you have to put layout code of searchview and listview or recyclerview in FindFrag.xml and copy your javacode from MainActivity.java to Your FindFrag.java
Best way to access rootview in fragments is to use within fragment class.
mainactivity is a separate java class, it should have a separate .xml file with a framelayout to show the fragments.
all other fragments should be in separate classes with separate .xml files.
use this tutorial its easy and clear : https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/

Request Window Feature from another class

I want the screen set to (Window.FEATURE_NO_TITLE). I know that I can put requestWindowFeature(Window.FEATURE_NO_TITLE); in the OnCreate method but I have another Class let's say Class B which is extended to the Fragment and it has a button in the layout. I want when that button is clicked in clicked in Class B, the Window should be shown without the Title. I tried as below:
AnandSahib anad = new AnandSahib();
anad.requestWindowFeature(Window.FEATURE_NO_TITLE);
I think you cant do it.you should call anad.requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView().please See this Android Developer site

Functions for an activity in a separate class

I have a MainMenuActivity with multiple fragments. I want to put my code for the fragment "Home" in a separate class. This is my current approach.
public class Home extends MainMenuActivity{
public TextWatcher tw = new TextWatcher() {...};
public Home(){
}
public void buttonPressed(View view){...}
}
I want to add a TextWatcher to a EditText and functions which are executed, when a button is pressed
(defined in the xml: android:onClick="buttonPressed").
If this approach is possible, where and how can I create this class and should I put something in the Home constructor?
You should use Fragments in your project. Here are some examples :
http://developer.android.com/training/basics/fragments/creating.html
http://developer.android.com/training/basics/fragments/fragment-ui.html
For your example you need to change MainMenuActivity to MainMenuFragmentActivity and create Home Fragment.
Best wishes.
With Java you shouldn't be afraid to have very large files (check View.java from the android source code, it's more than 15,000 lines long). There's no way to split a class like there is in C#.
But you say you use fragments. Why is your activity code so large? Usually most of the code go in the fragments, not in the activity.

android common OnItemClickListener and separate button listeners

In my app I have around 12 activities. The first layout of my activity is a list view. By clicking each list it gets redirected to other activities. I have implemented the Listview with OnItemClickListener and is working good.
In all the other 11 activities, I have a common title bar with a logo and a button named as S. When the user clicks on the S button, I am showing the same list of items in the First activity of my app, within the current activities by splitting the page. Now instead of writing a separate OnItemClickListener for each activity, how to write it once and use in multiple activities?
In the same way I have placed 3 button in include layout and I am using this in various activities, how to write a separate common button action, it can be used in multiple activities.
While #SamirMangroliya's method works, this is an alternate method you can use, and the one I've been using for a while.
Create your listener in another class file (say, MyClickListener.java):
public class MyClickListener implements OnItemClickListener {
// This can be OnClickListener, OnTouchListener, whatever you like.
// Implement your method here:
public onItemClick(...) {
// Your selection process, etc.
}
}
Then in each of your Activity objects, all you have to do is:
myObject.setOnItemClickListener(new MyClickListener());
how to write a separate common button action, it can be used in multiple activities.
you should create one Activity(called BaseActivity),
class BaseActivity extends Activity{
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Button btn1 = (Button)findViewById(r.id.btn1);
.
.
.
//now setonclickListner here...
}
}
then extends it ...class MyActivity extends BaseActivity
It sounds like you should implement ActionBar navigation. For a more detailed description about how to do it the Android way, see the Design guide. To support versions of Android between 1.6 and 2.3, link in the ActionBarSherlock library.

create common class which includes intents of header and footer icons throughout whole application in android

i stuck with a unusual problem
i have a restaurant application which includes menu icons fixed at the bottom of every layout,
my problem is i dont want to create onsetclicklistner() method of every icon on each of my activity class....
please give some suggestion so that i can make a common class where i can put all my footer icon click event and activities in it and use it on my every activity class with different setcontentview...
hope you all get my question...
looking forward for your reply
You don't need to setup onClickListeners programmatically. You can also put them in the layout XML like this:
<Button android:id="#+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_button_text"
android:onClick="myOnClick"
/>
Then you can declare a base activity class that contains this method in it, like this:
public class BaseActivity extends Activity {
public void myOnClick(View v) {
// Do whatever you want to do when the button is clicked
}
}
Then you write all of your activity classes so that they derive from BaseActivity instead of Activity.

Categories

Resources