Relative Layouts and Linear Layouts - android

how do i embed a tab layout within a button. My main layout is a linear layout, but i don't know how to program the main activity.java class. Could anybody help me get started this is what my main.java code looks like right now
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}

you can create tabs with the help of TabActivity class. You can take help from android-wireless-application-development book's unit 3 chapter 8 example.

Related

Android OnClick doesnt work for second layout

I have a Button which opens another layout when clicked. The second layout which opens also has a Button but somehow nothing happens on that Button when clicked.
My Activity:
package com.example.android.myapplicationo9;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static com.example.android.myapplicationo9.R.layout.layout2;
public class MainActivity extends Activity implements View.OnClickListener {
public Button bt;
public Button bt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater factory = getLayoutInflater();
final View textEntryViw = factory.inflate(R.layout.layout2,null);
bt2 = (Button)textEntryViw.findViewById(R.id.button2);
bt = (Button)findViewById(R.id.button);
bt.setOnClickListener(this);
bt2.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.button:
setContentView(layout2);
Toast.makeText(MainActivity.this,"e", Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"e", Toast.LENGTH_LONG).show();
break;
}
}
}
As you are setting new content view layout2 for your MainActivity, you have to initialize views of new layout2 again to use later.
1. Get your Button reference from layout2.
2. Set onClick listener to your Button.
Here is the working code:
case R.id.button:
{
setContentView(layout2);
// Initialize button of layout2
Button button = (Button) findViewById(R.id.button_of_layout2);
// Set onClick listener
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Layout2 Button clicked", Toast.LENGTH_LONG).show();
}
});
Toast.makeText(MainActivity.this,"e",Toast.LENGTH_LONG).show();
break;
}
Hope this will help~
did you impl. on click method on both buttons? also - make sure you are not consuming the click / touch event thus "not passing" it further on throughout the view hierarchy
Use Intent for opening new Activity
Intent intent = new Intent(this, YourClass.class)
startActivity(intent);
Using setContentView only Changes the layout but you're still in the same class
Try using normal onClick methods and make sure you instantiate your views on the second Activity.

Method does not override another method View.OnClickListener (Error in Override)

I m trying to create a new java file for button (sin) as this is about calculator. But it is showing Error in second override annotations. (Method does not override method from its SuperClass). There is one more Error associating with brackets in the bottom part of code. Being newbie this is the first time I'm having my hands on this part of java. I have searched many of the sites but didn't found any solution. Any help will be appreciated.
SinglesinActivity.java
package com.marsh.calculator.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class SinglesinActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.btnsin);
b.setOnClickListener(this);
}
#Override
public void onclick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin:
break;
}
};
}
It's onClick not onclick.
Make the following changes:
#Override public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin: break;
}
};
Its a typo. change onclick to onClick.

Instantiating ImageButtons outside of Activity

Hi Im using several Image buttons that are displayed at the bottom of each activity, and I was wondering if there is any way to instantiate the objects outside of their respective activities Oncreate as they do the same thing in each activity but I do not want to be repeating code..
Any advice would be greatly appreciated thanks.
Marc
at the moment i am receiving a null pointer exception when trying to call the build() function.
Here is my menu where tabs will display at the bottom
package koodoo.hcp.plus;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import koodoo.hcp.utilities.Tabbuilder;
public class Hcp_Menu extends Activity implements OnClickListener {
/**
* Stores all the buttons within the HCP_Menu Activity
*/
ImageButton groupBtn;
ImageButton readingBtn;
ImageButton activityBtn;
ImageButton calendarBtn;
ImageButton ongoingBtn;
Context context = this;
View view = new View(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hcp__menu);
Tabbuilder tb = new Tabbuilder();
tb.build(view, context);
/**
* Create Activity Buttons
*/
//group button
groupBtn = (ImageButton) findViewById(R.id.imageButtonGroup);
groupBtn.setOnClickListener(this);
//reading button
readingBtn = (ImageButton) findViewById(R.id.imageButtonReading);
readingBtn.setOnClickListener(this);
//activity button
activityBtn = (ImageButton) findViewById(R.id.imageButtonActivity);
activityBtn.setOnClickListener(this);
//calendar button
calendarBtn = (ImageButton) findViewById(R.id.imageButtonCalender);
calendarBtn.setOnClickListener(this);
//ongoing button
ongoingBtn = (ImageButton) findViewById(R.id.imageButtonOngoing);
ongoingBtn.setOnClickListener(this);
/**
* create Tab buttons
*//*
//dash tab
dashTab = (ImageButton) findViewById(R.id.dashButton);
dashTab.setOnClickListener(this);
//stats tab
statsTab = (ImageButton) findViewById(R.id.statsButton);
statsTab.setOnClickListener(this);
//invite tab
inviteTab = (ImageButton) findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(this);
//settings tab
settingsTab = (ImageButton) findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(this);
//log tab
logTab = (ImageButton) findViewById(R.id.logButton);
logTab.setOnClickListener(this);*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hcp__menu, menu);
return true;
}
/**
* onclick function for each button on the Activity.
* #param v
*/
#Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.imageButtonGroup: i = new Intent(this, Group.class);
startActivity(i);
break;
case R.id.imageButtonActivity: i = new Intent(this, Activities.class);
startActivity(i);
break;
case R.id.imageButtonCalender: i = new Intent(this, Calender.class);
startActivity(i);
break;
case R.id.imageButtonOngoing: i = new Intent(this, Ongoing.class);
startActivity(i);
break;
case R.id.imageButtonReading: i = new Intent(this, Reading.class);
startActivity(i);
break;
default:
break;
}
}
}
Tabbuilder class
package koodoo.hcp.utilities;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.ImageButton;
import koodoo.hcp.plus.Hcp_Menu;
import koodoo.hcp.plus.Invite;
import koodoo.hcp.plus.Log;
import koodoo.hcp.plus.R;
import koodoo.hcp.plus.Settings;
import koodoo.hcp.plus.Stats;
/**
* Created by Marc Davies on 18/09/2013.
*/
public class Tabbuilder {
ImageButton dashTab;
ImageButton statsTab;
ImageButton inviteTab;
ImageButton settingsTab;
ImageButton logTab;
public void build(View v, final Context context) {
/**
* create Tab buttons
*/
//dash tab
dashTab = (ImageButton) v.findViewById(R.id.dashButton);
dashTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Hcp_Menu.class);
}
});
//stats tab
statsTab = (ImageButton) v.findViewById(R.id.statsButton);
statsTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Stats.class);
}
});
//invite tab
inviteTab = (ImageButton) v.findViewById(R.id.inviteButton);
inviteTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Invite.class);
}
});
//settings tab
settingsTab = (ImageButton) v.findViewById(R.id.settingsButton);
settingsTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Settings.class);
}
});
//log tab
logTab = (ImageButton) v.findViewById(R.id.logButton);
logTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, Log.class);
}
});
}
}
Create a base activity and instantiate buttons in it. Now let other activities inherit the base activity. Dont forget to handle the layouts properly though
you can make a utils class with static initiate Methods.
Those would take a context and an image and return an imageButton. You would still call that in the onCreate of the corresponding Activity, but you wouldnt have to copy paste your code anymore.

ListView: Null pointer exception

Good day. I'm having some issues with my android project specifically listview. I tried searching for other information here in this site, and implemented some of the answers. However, it is still not working.
The error specifically is
NullPointerException at line 76 at MainActivity
Here is the code of my MainActivity
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
final ArrayList<String> studentName = new ArrayList<String>();
ArrayAdapter<String> aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList = (ListView) findViewById(R.id.listName);
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, studentName);
myList.setAdapter(aa);
//droid.R.id.list;
//add
Button bAdd = (Button) findViewById(R.id.addstudent);
bAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent("android.intent.action.ADDSTUDENTS"));
}
});
//edit
Button bEdit = (Button) findViewById(R.id.editstudent);
bEdit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.EDITSTUDENTS"));
}
});
//edit
Button bDelete = (Button) findViewById(R.id.deletestudent);
bDelete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.DELETESTUDENTS"));
}
});
}
public ArrayList<String> getArray(){
return studentName;
}
public void notifyArray(){
aa.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and line 76 by the way is
aa.notifyDataSetChanged();
Here is my code for the AddStudents class
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddStudents extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_student);
Button bAddStudents = (Button) findViewById(R.id.add);
final EditText et = (EditText) findViewById(R.id.student_name);
bAddStudents.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
MainActivity as = new MainActivity();
as.getArray().add(et.getText().toString());
as.notifyArray();
finish();
}
});
Button bBack = (Button) findViewById(R.id.backadd);
bBack.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
and the xml part with the list view is
<ListView
android:id="#+id/listName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
I hope you can help me cause I want to also learn what my mistakes are. I can add other information if you want.
In your AddStudents class, you're calling notifyArray() right after you instantiated MainActivity. MainActivity.onCreate() will not be called just by instantiating it.
Instantiating your MainActivity there is probably not what you want anyway (because that object will be disposed directly after the onClick handler is done).
What you want instead is to access the existing instance of MainActivity. For that, add a reference to the current instance to a static member of your MainActivity class, e.g.
public class MainActivity extends Activity {
public static MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
activity = this;
}
}
Then in your AddStudent class access it via
MainActivity.activity.notifyArray()
This is not the most beautiful way to solve your issue, but it works as long as you can be sure to only have one MainActivity instance. (If not, you could make the array itself static; or create a Singleton wrapper class for it.)
notifyArray() is being called before onCreate.
Try calling getArray().add(et.getText().toString()); and notifyArray(); inside onResume() of MainActivity and NOT from AddStudentActivity( not recommended!)
So onResume() you would ideally want to add a new student to the list, so in your case, you can retrieve the student name using a common sharable object like a hashtable or somethiing similar, make it a singleton, and use it from anywhere in the applciation
The common class may go something like:
class CommonHashtable{
private static Hashtable<String, Object> commonHashtable = null;
public static getInstance(){
if(commonHashtable == null)
commonHashtable = new Hashtable<String, Object>();
return commonHashtable;
}
on getInstance(), it returns a commonHashtable which can be used to store values temporarily!
so, add this on addbutton click event
Hashtable hash = CommonHashtable.getInstance();
hash.put("NEW_STUDENT_NAME", et.getText().toString());
and add this in you onResume() of MainActivity
Hashtable hash = CommonHashtable.getInstance();
Object studentName = (String) hash.get("NEW_STUDENT_NAME");
if(studentName != null){
notifyArray();
}

Will big android Activity wiriting cause any effect to my App

I'm making my first official application to put on the market and i was wondering would over 120 new activities be too many if I'm only using them to pull text? If so is there a way I could turn MysecondActivity to a giant if or switch statement?
This is the example I found in a book I changed up case 0: I'm going to change up the rest be similar to case 0 once i get an answer.
Thank you in advance and sorry for messy code and writing
AndroidManifest.xml
<activity android:name=".MySecondActivity" />
ListActivityExample.java
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class ListActivityExample extends ListActivity{
static final String[] ACTIVITY_CHOICES = new String[] {
"Open new Actvity",
"Open Contacts",
"Open Phone Dialer Example",
"Search Google Example",
"Start Voice Command"
};
final String searchTerms = "superman";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
getListView().setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3){
switch(arg2) {
case 0: //opens new screen
{Intent intent = new Intent(ListActivityExample.this,MySecondActivity.class);
startActivity(intent);
break;}
case 1: //opens phone dialer and fills in the given number
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("content://contacts/people/")));
break;}
case 2:
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("tel:12125551212")));
break;}
case 3: //
{
Intent intent1= new Intent(Intent.ACTION_WEB_SEARCH);
intent1.putExtra(SearchManager.QUERY, searchTerms);
startActivity(intent1);
break;}
case 4: //
{startActivity(new
Intent(Intent.ACTION_VOICE_COMMAND));
break;}
default: break;
}
}
});
}
}
MySecondActivity.java
mport android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MySecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("text here");
setContentView(tv);
}
}
There's nothing I can see about this code that will cause an issue with a large Activity stack. Start as many Activities as you like, as long as they aren't all stacked together at the same time. With this code you are starting a new Activity as a result of tapping an item in the list, but the user must finish the new Activity (press back) when returning to your list to start something else, so they aren't all in memory at once.
Does that help? Or did I misunderstand your question?

Categories

Resources