How can i start an instance of activity.
My custom activity has a constructor take some paramters.
public class CustomActivity extends Activity {
public CustomActivity(String someParameters) {
}
}
And i create an instance of it. How to start it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomActivity activity = new CustomActivity("TEST");
// how to start to the above activity from this activity
}
Related
protected void onCreate (Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
addPreferencesFromResources(R.xml.prefs);
}
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
addPreferencesFromResource (R.xml.YourFileName);
}
I have layouts main activity and another list activity. But when i try to call list activity i am getting crash. Please help me to start list activity (Note: List activity has EditText and button to add text to existing listview in list Activity)
All i wanted to add text listview which is already exist in listactivity.
Many Thanks!!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
EditText txt=(EditText)findViewById(R.id.txtPass);
if (txt.getText().length()==0){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
return;
}
if(txt.getText().toString().contains("Test")){
Button btn;
btn=(Button)findViewById(R.id.btnSubmit);
Intent intent=new Intent(this,Passwordlist.class);
startActivity(intent);
}
}
First, make some change to your code:
Button btn;
EditText txt
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do this stuff here and not in onClick. Your views should be defined in //onCreate() and before the onCLick()
txt=(EditText)findViewById(R.id.txtPass);
btn=(Button)findViewById(R.id.btnSubmit);
}
public void onClick(View view){
Intent intent=new Intent(YourActivity.this,Passwordlist.class);
if (txt.getText().toString().matches("")){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
}
else if(txt.getText().toString().contains("Test")){
startActivity(intent);
}
}
See if this works.
I have a viewpager with an adapter that extends FragmentStatePagerAdapter, this has to show some fragment that always repeats the same layout (in whic there is a listView) for every page ... now the problem is how can I update the listView that is currently visible by clicking on a button that is outside of the Fragment? I can refresh only the ones that are in the not visible pages,but not the ones that are displayed or adjacent...
One possible solution is to reset the adapter, but it doesn't seems to be right one, because it' s slow... could you please help me?
public class MainActivity extends FragmentActivity implements OnClickListener{
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
//here I would like to call the method caricaLista() of the fragment
break;
}
}
public static class AdapterPager extends FragmentStatePagerAdapter {
public AdapterPager(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int i) {
Fragment fragment;
Bundle args = new Bundle();
args.putInt("position", i);
fragment=new Fragment1();
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 1000;}
#Override
public CharSequence getPageTitle(int i) {
String title=String.valueOf(i);
return title;
}
}
public static class Fragment1 extends Fragment implements OnItemClickListener{
int fragmentPosition;
ListView lv;
List<Lista> lista= new ArrayList<Lista>();
public Fragment1(){
}
public static Fragment1 newInstance(){
return new Fragment1();
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
caricaLista("")}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
lv=(ListView) rootView.findViewById(R.id.listViewCompiti);
Bundle args = getArguments();
int position= args.getInt("position");
fragmentPosition=position;
lv.setOnItemClickListener(this);
homeListAdapter= new HomeListAdapterWithCache(ctx,R.layout.list_item_home,lista);
lv.setAdapter(homeListAdapter);
return rootView;
}
You have to store the Current Fragment into your MainActivity , and change every time the page changes.
So you call the method of the current fragments.
Check out how your MainActivity should look like:
public class MainActivity extends FragmentActivity implements OnClickListener{
&Fragment mCurrentFragment;*
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
*mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mCurrentFragment = pagerAdapter.getItem(position);
}*
});
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
*mCurrentFragment.caricaLista();*
break;
}
}
}
I have custom class InternalTimerServiceController in my application. I want to use it in another class inside android annotations. And it seems that autocompletion does not work correctly in this case.
I have this interface
public interface InternalTimerServiceControllerContract
{
void doWork();
}
And this class
#EBean
public class InternalTimerServiceController implements InternalTimerServiceControllerContract
{
#Override
public void doWork()
{
// do work
}
}
And this is my Activity
public class MyActivity extends Activity
{
// try uncomment line below and see if autocomplete works properly
//#Bean(Internal)
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
It's a bug, thanks for reporting. I've created a ticket for it: http://youtrack.jetbrains.com/issue/IDEA-98298
i have got this error getting location. i send manually coordiantes through emulator control when application starting but still got this error my code is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//for getting location
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new MyLocationListener(etTextOut);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, ll);
}
and my location class is
public class MyLocationListener implements LocationListener {
EditText etTextOut;
public MyLocationListener(EditText etTextOut) {
// TODO Auto-generated constructor stub
this.etTextOut = etTextOut;
}
//public TextView tv;
#Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLongitude();
loc.getLatitude();
String msg = "Latitude:" + loc.getLatitude() + "\nLongitude:" + loc.getLongitude();
etTextOut.setText(msg);
}
i want to set my coordiantes in edittext of the main class and send it over to PC through socket