I'm very new to programming in Android, but have been struggling all day with a problem and would appreciate your help.
I'm trying to create a form to get user information (essentially a new contact) which is accessed from the menu. When I click the button to create the new form, I get the following error:
"Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#626fd5e0"
This is my code:
The menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/search"
android:title="#string/settings_search"
android:alphabeticShortcut="#string/settings_shortcut_search" />
<item android:id="#+id/new_contact"
android:title="#string/settings_new"
android:alphabeticShortcut="#string/settings_shortcut_new" />
</menu>
The code for calling the new activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_contact:
//activities to create a new account
//startActivity(new Intent(this, Prefs.class));
Intent i = new Intent(MainActivity.this, NewContact.class);
startActivity(i);
//addSaver("String");
return true;
case R.id.search:
return true;
// More items go here (if any) ...
}
return false;
}
The view for the new contact activity:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">"
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="top">
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a test\nsecond line\n"/>
<Button
android:id="#+id/new_contact_button"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</ScrollView>
And the code for the new activity:
public class NewContact extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_contact);
// Set up click listeners for all the buttons
View newContactButton = findViewById(R.id.new_contact_button);
newContactButton.setOnClickListener(this);
}
Bizarrely if I comment out the button from the class and from the xml then it works correctly and opens up the new activity (but of course I can't do anything with that activity).
In your button xml code, you are missing
android:layout_width="wrap_content"
add that and give it a shot.
Related
I have a listView in my activity_main.xml . I used a layout(list_layout) for my listview's row. list_layout contains 3 textView. I added a activity called "Setting" into my Mainactivity. I want change visibility of list_layout's 3. textView from settin.java with a button.
I mean when I click button (button code is into setting.java(button is into activity_setting.xml)) list_layout's 3.textview must invisible.
This is from activity_main.xml
<ListView
android:id="#+id/listem"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
This is list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
.../>
<TextView
.../>
<TextView
android:id="#+id/turkish_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:visibility="visible"/>
</LinearLayout>
MainActivity.Java
... listview = (ListView) findViewById(R.id.listem);
DataHelper.Database data = new DataHelper.Database(MainActivity.this);
ArrayList<HashMap<String, String>> Liste = data.Listele();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, Liste, R.layout.list_layout, new String[]{"id", "title", "subtitle"}, new int[]{R.id.kelime_id, R.id.english_id, R.id.turkish_id});
listview.setAdapter(adapter);
...
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, Setting.class);
startActivity(intent);
break; ...
//Setting.Java
public class Setting extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
public void click(View view) {//<-----Here is my button's code
textView=(TextView)view.findViewById(R.id.turkish_id);
textView.setVisibility(View.INVISIBLE);
}
}
activity_setting.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
There are different methods for adding a click listener to a button , refer to this link:
add onclick listener to predefined button?
in your case you could implements in your activity the interface OnClickListener
public class Setting extends AppCompatActivity implements OnClickListener
then you should rename your method click to onClick
and in onCreate of your activity you should add the line
findViewById(R.id.yourIdButton).setOnClickListener(this);
don't forget to give an id to your button
<Button
android:"#+id/yourIdButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY BUTTON"
android:onClick="click"/>
you can also remove completely the TextView from layout using "View.GONE" in place of "View.INVISIBLE"
I took some code to make a simple Form on Android. It seems as if some parts of the code are referencing to something. I'm not sure what to put there to make it work? I get errors.
Error:(18, 32) error: cannot find symbol variable activity_form
Error:(33, 88) error: package com.chalkstreet.learnandroid.main does not exist
Error:(48, 50) error: cannot find symbol class MainSource
Error:(57, 41) error: cannot find symbol variable menu_main
I don't think that I need to be using the package that I am not having.
Example problem:
Intent sender = new Intent(Form.this,
===> ???? com.chalkstreet.learnandroid.main.Display.class);
Here's my form:
public class Form extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Initialize buttons and Edit Texts for form
Button btnSubmit = (Button) findViewById(R.id.button_submit);
Button btnSrc = (Button) findViewById(R.id.buttonSrc);
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText email = (EditText) findViewById(R.id.editText2);
final EditText phone = (EditText) findViewById(R.id.editText4);
//Listener on Submit button
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sender = new Intent(Form.this, com.chalkstreet.learnandroid.main.Display.class);
Bundle b1 = new Bundle(); //Bundle to wrap all data
b1.putString("name", name.getText().toString()); //Adding data to bundle
b1.putString("email", email.getText().toString());
b1.putString("phone", phone.getText().toString());
sender.putExtras(b1); //putExtras method to send the bundle
startActivity(sender);
Form.this.finish(); //Finish form activity to remove it from stack
}
});
//Listener on source button
btnSrc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent j = new Intent(Form.this, MainSource.class);
startActivity(j);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
Form.this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks
Create a file called MainSource.java, that is if you haven't done so already, and create a file called menu_main.xml in the menu folder under the res directory and add your menu items, create another layout file in the layout folder under the res directory and name it activity_form.xml, you can then add the necessary views with the ids matching those in Form.java
More illustratively, menu_main might contain
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
activity_form.xml could possibly be something like
<?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">
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something Else" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/buttonSrc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I have no idea what you want MainSource to do so I can't say much about that. I hope this helps.
When you create an explicit Intent, you only need to provide the starting context and the ending class.
Intent i = new Intent(FirstActivity.this, ResultActivity.class);
I am developing an application only to validate the interaction between activities through intents.
I created 5 ImageButton with an image for each. Each button is a movie and if you click any of them, is directed to a new activity with the synopsis of the film. The activity with the synopsis, there is an "up navigation" that returns the MainActivity (home).
The way I developed left very extensive project since created six activities (main activity and 5 activities, one for each film) and 6 layouts. Also, my apk is to 1.5mb.
Could someone help me with suggestions for best practices for I minimize my code or how developed is correct and could be developed in a real application?
I appreciate!!!
My MainActivity
package luizugliano.com.br.appfilmes;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void onClickBtVideo01(View view){
Intent intent = new Intent(getContext(),ActivityVideo01.class);
startActivity(intent);
}
public void onClickBtVideo02(View view){
Intent intent = new Intent(getContext(),ActivityVideo02.class);
startActivity(intent);
}
public void onClickBtVideo03(View view){
Intent intent = new Intent(getContext(),ActivityVideo03.class);
startActivity(intent);
}
public void onClickBtVideo04(View view){
Intent intent = new Intent(getContext(),ActivityVideo04.class);
startActivity(intent);
}
public void onClickBtVideo05(View view){
Intent intent = new Intent(getContext(),ActivityVideo05.class);
startActivity(intent);
}
private Context getContext(){
return 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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My ActivityVideo01 (other activities have the same code so I only put this as an example)
package luizugliano.com.br.appfilmes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class ActivityVideo01 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_video01);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
//O método finish encerrará essa activity
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
My content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView android:text="Sinopse - Filmes" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/layout_marginTop">
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:background="#drawable/btn_img_01"
android:onClick="onClickBtVideo01"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton02"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_02"
android:onClick="onClickBtVideo02"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton03"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_03"
android:onClick="onClickBtVideo03"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton04"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_04"
android:onClick="onClickBtVideo04"/>
<ImageButton
android:layout_width="#dimen/layout_width"
android:layout_height="#dimen/layout_height"
android:id="#+id/imageButton05"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/layout_marginLeft"
android:background="#drawable/btn_img_05"
android:onClick="onClickBtVideo05"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
My content_activity_video01.xml (other layout have the same code so I only put this as an example)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_activity_video01"
tools:context="luizugliano.com.br.appfilmes.ActivityVideo01">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title Synopsis"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Synopsis"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_marginTop="51dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I suggest, that you have two activities, and both these activities will act as container activity for their respective fragments.
In your first activity's fragment have your buttons. In your second activity's fragment, play your movie.
Now when you click on the button, go from first activity's fragment to second activity's fragment. Use interfaces to communicate between your fragments.
This is the way i thought about it, if you have a better way please do share.
Instead Of taking five different activity just take single activity and replace textview's value dynamically on the click of your button.
Just marge the two xml file like that in content_main.xml create another relative layout(which is initially gone view) with height width match parent under the image button linear layout. When one image button is pressed then just gone the view of linear layout of image buttons and visible the textview relative layout and visible the back button of the up navigation. When user pressed the up navigation just gone the textview relative layout, gone the up navigation and visible the linear layout of image button.
you need not to create multiple activities to show synopsis from different films.
you need to create your synopsis activity like a template and pass information about selected film in intent.
now you can change the content of your activity depending upon the information you received from intent.
Basically, whenever you are copying the code you need to ask yourself do you really need to copy the code? almost all the time answer is NO and what you actually need to do is, think of ways you can reuse the code you are copying.
Implement onclicklisstener and use switch case to detect which button has been clicked, Use only one activity to display synopsis instead of four and use bundle along with Intent to send which synopsis has to be shown. Here is the code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
case R.id.imageButton01:
//handle click for imgbutton1;
Intent intent = new Intent(getContext(),ActivityVideo.class);
//send which video should be played along with the intent. use bundle in ActivityVideo class to get this value.
intent.putExtra("Video name", "video1");
startActivity(intent);
case R.id.imageButton02:
//handle click for imgbutton2;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video2");
startActivity(intent);
case R.id.imageButton03:
//handle click for imgbutton3;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video3");
startActivity(intent);
case R.id.imageButton04:
//handle click for imgbutton4;
Intent intent = new Intent(getContext(),ActivityVideo.class);
intent.putExtra("Video name", "video4");
startActivity(intent);
}
}
Use ActivityVideo class instid of ActivityVideo01, 02 ......
ActivityVideo.java ->
public class ActivityVideo01 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_video01);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle extras = intent.getExtras();
String VideoName = extras.getString("Video Name");
// VideoName will be having the sent value.
if(VideoName == "video1"){
//code for what do you want to do when imagebutton1 is clicked
}elseif(VideoName == "video2"){
//code for what do you want to do when imagebutton2 is clicked
}elseif(VideoName == "video3"){
//code for what do you want to do when imagebutton3 is clicked
}elseif(VideoName == "video4"){
//code for what do you want to do when imagebutton4 is clicked
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == android.R.id.home) {
//O método finish encerrará essa activity
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Hope it helps!
I defined a layout view in a .xml file called menu_list_slide_lateral.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_menu_slide"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSliBebe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/bebe" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/foto_diario" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/consejos" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/ajustes" />
</LinearLayout>
Im creating the SlidingMenu from code:
setBehindContentView(R.layout.menu_list_slide_lateral);
setSlidingActionBarEnabled(true);
slideMenu = getSlidingMenu();
slideMenu.setMode(SlidingMenu.LEFT);
slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slideMenu.setShadowWidthRes(R.dimen.shadow_width);
slideMenu.setBehindOffset(100);
slideMenu.setFadeDegree(0.35f);
And mi activity extends from SlidingFragmentActivity:
public class TimelineActivity extends SlidingFragmentActivity
It shows perfectly the menu, but i want to do some actions when the user chooses an option from the menu:
For example, i want to open another activity when i choose the "Bebe" option.
I tried to set a onClick event to that button but it doesn't seem to work, it makes nothing:
inflater = getLayoutInflater();
item = inflater.inflate(R.layout.menu_list_slide_lateral, null);
btnSliBebe = (Button) item.findViewById(R.id.btnSliBebe);
btnSliBebe.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Log.e(TAG, "boton bebe");
}
});
How can i access that buttons and asign them a event?
Thanks!
What you should to is to create separate fragment for slide menu which will contain your layout R.layout.menu_list_slide_lateral and will handle actions. This is easy to do.
Now when you have this fragment you need to insert it into your slide activity.
setBehindContentView which you used is placeholder for menu. So create simple layout which will hold your menu fragment. E.g. call it R.layout.menu_frame and it should be like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Then in your sliding activity set R.layout.menu_frame as behindContentView and add your menu fragment into this view.
public class BaseSlidingActivity extends SlidingFragmentActivity {
protected MenuFragment mSlidingMenuFragment;
private SlidingMenu mSlidingMenu;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
//if we create new menu - create new MenuFragment and insert it into
//menu_frame
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mSlidingMenuFragment = new MenuFragment();
t.replace(R.id.menu_frame, mSlidingMenuFragment);
t.commit();
}
//if activity was restored(e.g. on orientation change) find it in fragment
//manager
else {
mSlidingMenuFragment = (MenuFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
mSlidingMenu = getSlidingMenu();
mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
mSlidingMenu.setShadowDrawable(R.drawable.shadow);
mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
mSlidingMenu.setFadeDegree(0.35f);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}
I hope it helps. You can browse through source code of example at github slidinfmenu example
I am starting to learn android and i have a few questions.
I have my main program as:
public class Radiation_overflowActivity extends Activity implements OnClickListener {
EditText init_cores;
View final_cores;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up click listeners
init_cores=(EditText) findViewById(R.id.init_cores);
//init_cores.setOnClickListener(this);
final_cores=(View) findViewById(R.id.final_cores);
final_cores.setOnClickListener(this);
}
//called when a button is clicked
public void onClick(View v) {
switch (v.getId()){
case R.id.final_cores:
//int r = Integer.parseInt(init_cores.getText().toString().trim());
double initcores=Double.parseDouble(init_cores.getText().toString().trim());
double l=2,t=2;
double fcores=initcores*Math.exp(-l*t);
Intent i=new Intent(this,calcs.class);
i.putExtra("value",fcores);
startActivity(i);
break;
}
}
}
Also, my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/initial_cores" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/init_cores"
android:inputType="numberDecimal" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/final_cores"
android:text="#string/calculate" />
</LinearLayout>
My calcs.java:
public class calcs extends Activity{
TextView calcs_final;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calcs);
calcs_final=(TextView) findViewById(R.id.calcs_final);
double f=getIntent().getExtras().getDouble("value");
calcs_final.setText(Double.toString(f));
}
}
My calcs.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/calcs_final"
android:text="#string/result" />
</LinearLayout>
I want to do this:
The user enters data into the edittext (init_cores) and then i want to make some calculations to compute the final_cores and show it to the user.
I have trouble for showing the results.I am starting another activity (calcs.class and calcs.xml).
Where i must do the calculations?To the calcs.class?
Right now,the user enters a number ,presses the "Calculate" button and then it shows the text (from the strings.xml) "Number of cores"" ,but not the result.
Also, can i use the input from the data and from my calculations and make a plot?If you could give me some directions to this.
Finally,is my approach right?
Thank you!
For plotting your data, you can use the AChartEngine library, which is quite extensive and easy to use.
As per I think your final_cores is a view on which, you want to get your edittext's value for calculation(Hope I am not wrong)
So, just do Something like,
public void OnClick(View v) {
switch (v.getId()){
case R.id.final_cores:
int r = Integer.parseInt(init_cores.getText().toString().trim());
Intent i=new Intent(this,calcs.class);
i.putExtra("value",r);
startActivity(i);
break;
}
}
Remove onClickListener from your init_cores edittext. And get values of r in your Calcs.class using
int r = getIntent().getExtras().getInt("value");
Also be sure in your edit text always you are entering numeric values so it can be parsable into Integer..
Try this and let me know what happen..
Thanks,