I'm using an android panel widget in my application to create a sliding panel effect from the top of my app (similar to the notification panel). When clicked the panel opens to fill about 30% of the screen. By default the panel is closed and has a handle to "show". I'm trying to modify it to be in the shown state by default but am unsure how to do it. I assume that I'll have to modify the Panel.java file but I suppose it might also be done in my layout file.. Any tips?
Assuming you're defining your own class which extends Panel...in your onCreate() override, you could try calling setOpen(true, false); ?
EDIT As you're not extending it yourself, you could simply call the setOpen() method as soon as your activity is created instead. Example...
public class MyActivity extends Activity {
protected Panel topPanel = null;
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate();
setContentView(R.layout.main);
topPanel = (Panel) findViewById(R.id.top_panel);
topPanel.setOpen(true, false);
}
}
Related
I have a main activity where the user has 3 list views and 3 buttons displayed. When the user clicks on 2 of the three buttons I want the user to be sent to another screen (another layout). I already have the two other layouts created and the two other classes created. I am not sure why this is not working. My application reports no errors, no warnings, and I have found that I cannot go through my code step by step because my debugger is not working.
I have an OnClickListner(I am currently just testing one button at a time to get the issue fixed) that is linked to the button in the Main Activity layout.xml android:OnClick. I have an Intent put in place within the Main Activity that (according to multiple sources and YouTube tutorials) is set up properly and the new activity that the user should be redirected to is linked to the appropriate layout. When I run the application the button click registers (I know this because I have a sound effect in place when the button is pressed) but nothing else happens. Also all of my activities are present in the AndroidManifest.xml file. And all of the activity files are at the same folder level, all layouts are under res/layout
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiate button
Button btnClicked = (Button)findViewById(R.id.goal);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
btnClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
ChangeGoal.class));
mp.start();
}
});
// method called when set goal button is clicked
public void setGoal(View view)
{
// intent to send user from main activity to the change goal
activity
Intent setGoal = new Intent(this, ChangeGoal.class);
startActivity(setGoal);
}
}
public class ChangeGoal extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_goal); // connect
class to the other layout
}
}
//button from activity_main.xml
<Button
android:id="#+id/goal"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="#string/Set"
android:onClick="setGoal"
android:textSize="20sp"
android:textColor="#000000"/>`
Again, I am expecting the button click from the main activity to send the user to a new activity where they need to make a selection and then return to the main activity. Thus far the button click registers (I can hear it since I included some audio already upon the button click) but the application does nothing, I am left on the same screen and the button can be pressed again and again.
In your Activity layout, you have declared this on your Button:
android:onClick="setGoal"
Then, in code, you have attached an onClickListener to the same Button. This is wrong. Do either the one, or the other, but not both.
I'm creating app which uses TabLayout. I have also ToggleButton there.
Is there a way to unable changing to another tab while this particular button is pressed?
Update
I have a ToggleButton in an abstract class which extends Fragment:
public void onToggleClicked() {
btnMicrophone.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (something) {
do_something();
}else {
do_something_else();
}
}
}
);
}
There is also a fragment class which extends the abstract class above. I have some assignments there, method callings, Overrided methods (onCreateView, onViewCreated), etc.
MainActivity is almost all generated from pattern.
Everything works fine, but as I said, I need to unable switching between tabs while my button is pressed.
Please update your question with your code, so I can update my answer with the code necessary to solve your problem.
If you are using the new TabLayout (if you are not, you should), you can iterate over your views making them unclickable.
As example code:
LinearLayout tabs = ((LinearLayout)tabLayout.getChildAt(0));
for(int i = 0; i < tabs.getChildCount(); i++) {
tabs.getChildAt(i).setClickable(false);
//This disable all tabs, if you need to disable just specific tabs, you can make some logic to it
}
I created a menu and when I click on register I want to open a new class with just one line of text in it.
The app opens and runs but when I click on the "Register" Button in my menu the app crashes. I have no code erros.
Can you see my issue?
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.register:
Intent intent = new Intent(MeorNot.this, AddMember.class);
startActivity(intent);
return true;
}
}
This is my new class i try to open
package com.meornotFinal;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Add member");
this.setContentView(textview);
}
}
check your activity
<activity android:name="AddMember.class"> in AndroidManifest.xml file.
you are forgetting to add this line . so add this line also in androidmanifest.xml file.
Your current code block for the Activity you call after clicking on the Register button:
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Add member");
this.setContentView(textview);
}
}
You will need to change the this.setContentView(textview); to something like this:
public class AddMember extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_layout_in_your_layout_folder);
// CAST YOUR TEXTVIEW HERE
}
}
In the a_layout_in_your_layout_folder, you can keep a single LinearLayout and add your TextView at runtime. I don't quite see the point of doing such a thing as declaring a TextView in the layout XML would be a far simpler option.
But to each his own I suppose.
You are trying to set a View in setContentView() instead of a layout
this.setContentView(textview);
If you are going to do it this way then you need to create a layout programmatically with something like
RelativeLayout relativeLayout = new RelativeLayout(this);
then you can add your TextView to this and set your layout in setContentView()
Unless you need to do it this way, a simpler way is probably to create the xml layout then get your TextView from there with findViewById(R.id.textView1) after calling setContentView(R.layout.your_layout_file);
Logcat
To turn on the logcat, if not already in Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.
This will give you a console where it logs errors and other debug information. When your app crashes it will give you the reason and usually a java file with line number where it crashed. You may have to do further digging if the error happens before that line but it will give you a good starting point.
An example might be
Caused by: java.lang.NullPointerException
08-07 08:24:02.516: ERROR/AndroidRuntime(334): at your_app.org.ThisActivity.onCreate(ThisActivity.java:26)
Then you know to start looking at line 26 of ThisActivity for something that is null
I have 3 tabs in my sample application with activity group. First tab contains search activity i.e.Home/Root activity and am displaying the results of search in another activity but under same tab i.e Tab1. When I press back button in result activity, it is going to search activity. Everything works fine till here. Now I want to go search activity by pressing tab1 instead of pressing back button. How can achieve this? I tried something like this
public class TabSample extends TabActivity {
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTabTag().equals("tab1")) {
//What should I do to display search activity here
} else {
tabHost.setCurrentTab(1);
}
}
});
tabHost.setFocusable(true);
tabHost.requestFocus();
}
}
Can anyone please help let me know how to invoke search activity when tab is pressed? What will go into if part? Because if I use tabHost.setCurrentTab(index), it will display result activity but not search activity.
NOTE: I followed the tutorial given in this link.
I think what you want to do is this: when the 'tab1' tag is selected, go back to TabGroup1Activity if (and only if) the current activity is not that activity (basically you want to simulate a 'back' press).
If so, what you want is this:
if (getCurrentActivity().getClass() != TabGroup1Activity.class)
getCurrentActivity().finish()
I'm not 100% sure I understand you fully, but let's see :)
In your onTabChanged listener you can switch on which tab have been tabed, and then open the activity as normal inside an activitygroup:
public void onTabChanged(String tabId) {
if (tabId.contentEquals("tab1")) {
Intent intent = new Intent(tabHost.getContext(), TabGroup1Activity.class);
View view = StartGroup.group.getLocalActivityManager().startActivity("tab1", intent).getDecorView();
StartGroup.group.setContentView(view);
}
}
I just reviewed my code and think there's a bit more to explain here. The problem is that you don't stack activities as normal. Instead the workaround is to make a content stack and change these instead. So what I have done is to create a class StartGroup which extends
ButtonHandlerActivityGroup:
public class StartGroup extends ButtonHandlerActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static StartGroup group;
// Need to keep track of the history if you want the back-button to work properly,
// don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("UserList", new Intent(this, UserList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);
}
public void back() {
if (history.size() > 0) {
// pop the last view
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
} else {
finish();
}
}
}
Then from the TabMaster class or what you call it you can use the StartGroup class to change the content view of an activity group.
This is something I wrote to work on devices from 2.2, so there might be an easier and more androidish way to accomplished it, but this works on almost all devices :)
Here is another thread where the use a similar approach:
Launching activities within a tab in Android
Let me know if I can help more.
There is an ArrayList in your ActivityGroup so override onPause() method in ActivityGroup and remove all the ids from ArrayList except the first one which must be your SearchActivity.
So when you go to other tab then comes back to SearchActivity( or on Tab1 ) Home will be displayed.
i have TabActivity in android project which contains some tabs. In each tab i can open various activities, and after open it in a tab i want go back to previous activity in same tab, but default android behavior close my root tab activity. How i can realise behavior that i need?
There are a few ways of doing this. The first involves creating a custom GroupActivity that will keep track of the stack from the LocalActivityManager and then extending that class for each of your tabs. For that, check out this tutorial:
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
A simpler approach is to keep an array of your tab's subviews within your initial ActivityGroup class and then override the back button. Here's some sample code:
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager()
.startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
viewList.add(view); // Add id to keep track of stack.
this.setContentView(view);
}
public void previousView() {
if(viewList.size() > 0) {
viewList.remove(viewList.size()-1);
if (viewList.size() > 0)
setContentView(viewList.get(viewList.size()-1));
else
initView();
}else {
finish();
}
}
The initView() class holds all of the inflating of the original activity's view. This way, you can call this method to regenerate the original activity if there are no more views in the array.