i am new to Android and i am facing a problem in calling different activities from the same screen with same user interface.
Actually i want to implement d functionality of a tab activity but instead of tabs i am providing buttons and the buttons should act like tabs.
I am unable to do that. I am going wrong some where.
Can anyone help me please.....
HomeScreen class is:
public class HomeScreen extends Activity implements OnItemClickListener {
public Integer[] images = { R.raw.mobile, R.raw.note_books, R.raw.ac,
R.raw.drivers, R.raw.camera, R.raw.home_theaters, R.raw.pda,
R.raw.tv, R.raw.washing_machines, R.raw.scanners };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gv = (GridView) findViewById(R.id.gridV);
LayoutInflater inflater = getLayoutInflater();
gv.setAdapter(new GridViewAdapter(images, inflater));
gv.setOnItemClickListener(this);
if (StaticUtils.scheckStatus){
parseData();
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent contents = new Intent(HomeScreen.this, Cat.class);
contents.putExtra("homescreen", arg2);
startActivity(contents);
}
Cat.class is this:
class Cat extends Activity implements OnClickListener{
private Button mBtnContents, mBtnBrand, mBtnCategory, mBtnBack;
#Override
public void onCreate(Bundle si){
super.onCreate(si);
setContentView(R.layout.gridtab);
int i = getIntent().getIntExtra("homescreen", 0);
mBtnContents=(Button) findViewById(R.id.btnContents);
mBtnContents.setOnClickListener(this);
mBtnBrand=(Button) findViewById(R.id.btnBrand);
mBtnBrand.setOnClickListener(this);
mBtnCategory=(Button) findViewById(R.id.btnCategory);
mBtnCategory.setOnClickListener(this);
mBtnBack=(Button) findViewById(R.id.btnBack);
mBtnBack.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v==mBtnContents){
int i = getIntent().getIntExtra("homescreen", 0);
Intent in=new Intent(Cat.this, Pc.class);
in.putExtra("homescreen", i);
startActivity(in);
} else if(v==mBtnBrand){
startActivity(new Intent(Cat.this, Sd.class));
} else if(v==mBtnCategory){
startActivity(new Intent(Cat.this, Sbc.class));
} else if(v==mBtnBack){
startActivity(new Intent(Cat.this, Hs.class));
}
}
}
When i click on contents button its displaying the details but when i click on the other buttons its not showing anythng
Instead of "v==mBtnContents" use "v.equals(mBtnContents)" because View is an object.
Related
In my project, when i click on the button in the first Activity it goes to a second activity. Second Activity plays a video. Video works fine, but when i the press back button on phone it goes back to the first Activity but video sound is still on, meaning video is still playing. How can i stop it when i press back button or click back ?
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(), gnrl.class);
startActivity(int0);
}
#SuppressLint("CutPasteId") public class gnrl extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.matematik);
String[] myArray= getResources().getStringArray(R.array.Matematik1);
ArrayAdapter<String> aad= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray);
setListAdapter(aad);
ListView listview=(ListView)findViewById(R.id.listmat);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (position == 0) {
Intent int0 = new Intent(getApplicationContext(),Test.class);
startActivity(int0);
}
}
});
}
private void setListAdapter(ArrayAdapter<String> aad) {
// TODO Auto-generated method stub
}
}
Try overriding the onPause method of the second activity and stop the video playback there.
Just call finish() when you leave the activity playing the video.
protected void onPause() {
finish();
}
Or if you want to leave the activity after pressing a button, do something like
Button goBackButton = (Button) view.findViewById(R.id.back_button);
edit.setOnClickListener(new OnClickListener() {
....
#Override
public void onClick(View view) {
Intent goBackToMainActivity = new Intent(this, MainActivity.class);
startActivity(goBackToMainActivity);
finish();
}
});
But remember,this also stops the video from playing if for instance your phone goes into sleep mode, or anything else that interferes with the activity (dialogs etc.).
I advice you take a couple of minutes to read up on the basics of Activity in Android.This is a good place to start.
Alternatively, in the manifest you can set the "noHistory" attribute in your activity element to true.
Read more on that here
I am developing android application which having multiple tabs.
Now in launcher activity tabs display perfectly and can navigate through the tabs.
but if i call activity(which i wanted to show as Tab) on Button Clicked then tabs seems disappear.
please refer the given code and let me know if i am doing something wrong.
This is Main TabActivity
public class MyTabActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabHost tabHost=getTabHost();
TabSpec deshTab=tabHost.newTabSpec("Deshboard");
deshTab.setIndicator("Deshboard");
Intent DeshboardIntent=new Intent(this,DeshboardActivity.class);
deshTab.setContent(DeshboardIntent);
tabHost.addTab(deshTab);
TabSpec clientTab=tabHost.newTabSpec("client");
clientTab.setIndicator("client");
Intent intent=new Intent(this,ClientActivity.class);
clientTab.setContent(intent);
tabHost.addTab(clientTab);
}
}
Now i wanted to start client activity like
void onButtonClick(View view)
{
int id = view.getId();
switch(id)
{
case R.id.client_btn:
Intent clientIntent = new Intent(DeshboardActivity.this,ClientActivity.class);
startActivity(clientIntent);
break;
}
}
but when i click this button it starts new activity but NOT in tab.
what should i do to display this activity in tab on button click also.?
Below code is ClientActivity which i wanted to display as TAB.
public class ClientActivity extends Activity
{
private DatabaseHandler dbHandler;
private ListView clientListView ;
private BaseAdapter listAdapter;
TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.clientactivity);
dbHandler = new DatabaseHandler(this);
final List<Client> clientList = dbHandler.getAllclient();
clientListView = (ListView)findViewById(R.id.client_list);
listAdapter = new ClientListAdapter(this, clientList);
clientListView.setAdapter(listAdapter);
clientListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Client client = clientList.get(position);
Toast.makeText(getApplicationContext(), client.getFirstName(), Toast.LENGTH_LONG).show();
Intent clientInfoIntent = new Intent(getApplicationContext(), ClientInfoActivity.class);
clientInfoIntent.putExtra("client",client);
startActivity(clientInfoIntent);
//finish();
}
});
}
}
That's because your ClientActivity is launched on top of your TabActivity. That way your tabbar is not shown anymore.
I suggest you use fragments for your ClientActivity.
A tutorial how to use fragments:
http://developer.android.com/guide/components/fragments.html
I have a listview in my activity.
first time I click listview item, it works fine.
when I press back button and comes back to this activity, I can't click on the item anymore.
this listview works fine with android2.
when i test with android 4, happens this problem.
I found this kind of problem here, and I've tried all 'descendentFocusabilty' thing,
but not solved yet.
I know i shouldn't post almost same question. but i couldn't find any answer works fine there.
please help me solve this problem.
here is my child1 activity
public class TabChild1 extends NavigationActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabchild1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("red");
adapter.add("green");
adapter.add("blue");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = (ListView) parent;
String item = (String) listView.getItemAtPosition(position);
Intent intent = new Intent(TabChild1.this, TabChild2.class);
int iNum = position;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
goNextHistory("TabChild2", intent);
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
here is my NavigationActivity
public class NavigationActivity extends Activity {
public void goNextHistory(String id, Intent intent) {
NavigationGroupActivity parent = ((NavigationGroupActivity) getParent());
View view = parent.group.getLocalActivityManager()
.startActivity(id,intent)
.getDecorView();
parent.group.replaceView(view);
}
#Override
public void onBackPressed() {
NavigationGroupActivity parent = ((NavigationGroupActivity) getParent());
parent.back();
}
}
here is my NavigationGroupActivity
public class NavigationGroupActivity extends ActivityGroup {
ArrayList<View> history;
NavigationGroupActivity group;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
history = new ArrayList<View>();
group = this;
}
public void changeView(View v) {
history.remove(history.size() - 1);
history.add(v);
setContentView(v);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back() {
if(history.size() > 1) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
#Override
public void onBackPressed() {
group.back();
return;
}
}
and my child1 layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
i think You have to use finish() inside the onBackPressed() method of class TabChild1. and comment the line parent.back() inside the onBackPressed() method of class NavigationActivity.
I think this will work for you.
I have created an android application. In that application when I click the listview item it should display in the another listview in the same layout.
Is this possible in android?
Well, quick snippet:
public Activity1 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
// ListView event
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedString", listView.getItemAtPosition(position));
startActivity(intent);
}
});
}
}
public Activity2 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle b) {
// stuffs here
....
String valueFromActivity1 = getIntent().getString("SelectedString");
// ok now, u've got value from Activity1, do whatever w/ it
}
}
No you have to make intent and pass the variables of the current selected item of the listview to that intent and display the dynamic listview for that Item
I've been able to pass a selected image from grid view to a new full screen activity. I am now trying to capture the EXIF data from the image and pass it into a new activity.
The first activity of passing the int from grid view seems to be working fine.
public class test extends Activity {
public static int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thumb);
GridView gridview = (GridView) findViewById(R.id.thumbgridview);
gridview.setAdapter(new tImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(test.this,test2.class);
pos=position;
intent.putExtra("pos", pos);
startActivity(intent);
finish();
}
});}
}
The second activity which displays the full image seems to be working fine.
public class test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full);
Bundle bundle= getIntent().getExtras();
ImageView image = (ImageView) findViewById(R.id.imagefull);
int pos = bundle.getInt("pos");
bundle.getFloat(ExifInterface.TAG_MAKE);
tImageAdapter obj = new tImageAdapter(this);
image.setImageResource(obj.tThumbIds[pos]);
Button bDIR = (Button) findViewById(R.id.bDIR);
bDIR.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(test2.this,Direct.class);
intent.putExtra(ExifInterface.TAG_MAKE, 0);
startActivity(intent);
finish();
}
});
Now when I proceed to the final activity all I am seeing in the text view is the word Make.
public class Direct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newthumb);
Bundle bundle= getIntent().getExtras();
bundle.getFloat(ExifInterface.TAG_MAKE);
TextView textview = (TextView) findViewById(R.id.dirtext);
textview.setText(ExifInterface.TAG_MAKE);
}
}
I am not getting any errors in debug and there hasn't been a single force close issue. Is there something I am missing? I've only been working with java for a couple weeks but this type of activity seems like it should be doable. (or I'm just an idiot)
Thanks!
bundle.getFloat(ExifInterface.TAG_MAKE); does not read anything. You are nowhere actually reading the Exif data from the image file. You simply are showing in the TextView the content of the static String named ExifInterface.TAG_MAKE.
The documentation is available: ExifInterface. You will need to do something like:
ExifInterface exifReader = new ExifInterface(filename);
textview.setText(exifReader.getAttribute(ExifInterface.TAG_MAKE));