Multiple Activities in Android Studio - android

I'm new to Android. I'm trying to test 2 Activities in my project where when I click on a button in my first Activity, it should take me to the second Activity. Here I have given a click event to my button but when I run the project, I'm unable to see anything, its just a blank screen. What am I missing ?
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
public void onAddCLick(View view) {
Intent TaskIntent = new Intent(this,SecondScreen.class);
startActivity(TaskIntent);
}
}
This is the second activity :-
public class SecondScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent task = getIntent();
}
public void cancl_btn(View view) {
Intent goBack = getIntent();
finish();
}
}
This is activity_main.xml :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ListView
android:layout_width="wrap_content"
android:layout_height="516dp"
android:id="#+id/theListView">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/add_task"
android:onClick="onAddCLick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/setloc"
android:onClick="onMapbtnClck"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/settings"
android:onClick="onSetngClck"/>
</LinearLayout>
This is second_layout.xml :-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<EditText
android:layout_width="364dp"
android:layout_height="wrap_content"
android:id="#+id/task_name_edit_txt"
android:text="Enter your Details here" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Set Location"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Set_loc_btn"
android:onClick="set_usr_loc"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Add_task_btn"
android:onClick="add_usr_tsk_btn"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel_btn"
android:onClick="cancl_btn"/>
</LinearLayout>
</LinearLayout>

Use this code:
In first activity:
public void onAddCLick(View view) {
Intent TaskIntent = new
Intent(MainActivity.this,SecondScreen.class);
startActivity(TaskIntent);
}
In second Activity:
public void cancl_btn(View view) {
Intent goBack =new
Intent(SecondScreen.this,MainActivity.class);
startActivity(goBack);
}

by assuming that you have posted the complete XML file of the second layout,, I tried running it on my android studio and as you can see in the xml file itself you are missing lot of information in it,,,
you need to define the schema as you have done in the first xml and
the < EditText > tag cannot be outside a container, you need to place it inside the container, that is, inside any kind of layout (linear, relative etc)..
if the issue is with the xml file then modifying it as per above two points will solve your problem,, hope this helps

Related

Adding Multiple Layouts Programmatically

Question: How to add multiple Relative Layouts to an existing Relative Layout.
Please forgive my ignorance as I am still in the process of self learning android dev and there are still a few gaps in my understanding.
My goal is to take this screen: Main Activity and Add multiple smaller Relative Layouts to a Linear Layout, in this case that layout is titled rootWorkingLayout. Now currently, my code successfully adds one of the smaller Relative Layouts as seen here: Main Activity.
My seemingly elusive goal is to have 4 or 5 of these on the screen each with different data within the TextViews (But that's the next step. for me) I am unsure of how to accomplish this.
Main Activity Java Class:
public class rootActivity extends AppCompatActivity {
private Toolbar toolbar; //Toolbar Declaration
public LinearLayout rootWorkingLayout;
public static int i = 100; //previewCounter
//Main Function
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_root);
toolbar =(Toolbar) findViewById(R.id.toolBar);
rootWorkingLayout = (LinearLayout) findViewById(R.id.rootWorkingLayout);
setSupportActionBar(toolbar);
Bundle previewReceiver = getIntent().getExtras();
//If There is a Bundle, Process it
if(previewReceiver != null) {
newPreview(previewReceiver);
}
}
//Create Options Menu
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
//Track Options Menu
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_newReport: return true;
case R.id.action_history: return true;
case R.id.action_settings: return true;
default: return super.onOptionsItemSelected(item);
}
}
//Launch New Report Wizard
public void newReport (MenuItem menuItem)
{
Intent startNewReport = new Intent(getApplicationContext(), reportGeneratorActivity.class);
startActivity(startNewReport);//Go To Report Wizard
}
//Process New Preview
public void newPreview (Bundle previewReceiver) {
//Extract Strings from Bundle
String date = previewReceiver.getString("date");
String client = previewReceiver.getString("client");
String machine =previewReceiver.getString("machine");
String serialNum = previewReceiver.getString("serial");
//Create New Inflater
LayoutInflater previewInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View previewLayout = previewInflater.inflate(R.layout.previewplate, null);
//Set Params
LinearLayout.LayoutParams previewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
previewLayout.setLayoutParams(previewParams);// Add Params to Preview Plate
//Add previewLayout to rootWorkingLayout
previewLayout.setId(i);
//previewLayout.setID(i);//Assing new ID
i += 100;//Increment ID
rootWorkingLayout.addView(previewLayout);
//TODO: Configure Preview Plate
}
}
Here is the Main Activity XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.gdt.user.testgdt.rootActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/toolBar"
android:layout_below="#+id/toolBar"
android:id="#+id/rootWorkingLayout"
android:paddingTop="10dp"
android:weightSum="1">
</LinearLayout>
</RelativeLayout>
And here is the Layout of the Relative Layout I am attempting to add multiple times to the rootWorkingLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/previewReportPlate"
android:background="#D1D1D1"
android:layout_below="#+id/previewReportPlateLbl"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Client Name"
android:id="#+id/clientNamePlate"
android:textColor="#000000"
android:paddingLeft="6dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Machine Type"
android:id="#+id/machineTypePlate"
android:textColor="#000000"
android:paddingLeft="6dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:layout_marginLeft="0dp"
android:layout_below="#+id/clientNamePlate"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Serial Number"
android:id="#+id/serialNumberPlate"
android:textColor="#000000"
android:paddingLeft="6dp"
android:paddingTop="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp"
android:layout_marginLeft="0dp"
android:layout_below="#+id/machineTypePlate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date"
android:id="#+id/datePlate"
android:layout_gravity="right"
android:textColor="#000000"
android:paddingRight="10dp"
android:layout_marginLeft="691dp"
android:layout_below="#+id/serialNumberPlate"
android:layout_alignParentEnd="true" />
</RelativeLayout>
My current thoughts are that this is an issue with the previewLayout.setId or a rule that I need to declare somewhere. Likely I belive I need to construct some sort of loop eventually that goes through the process. But at this time I think I need help figuring out how to add multiple previewPlate layouts.
Thanks in advance!
Now currently, my code successfully adds one of the smaller Relative Layouts
Sure, that is because you only call your method once in onCreate.
newPreview(previewReceiver);
You could copy that line multiple times, and you will see more than one layout.
My seemingly elusive goal is to have 4 or 5 of these on the screen each with different data within the TextViews
You will need different Bundles each time you call your method to set different data, but all you need to do is call that method again.
And if you are trying to do it with that plus button in the Toolbar, you can do it like so
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_newReport:
Bundle b = new Bundle();
// TODO: set some data in the bundle
newPreview(b);
return true;

View#bringToFront not working on a button, only on a TextView in Android

My layout's XML is like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hirewand.hudki.UploadResumeActivity"
android:background="#303030"
android:id="#+id/rootLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/actionBar"
android:background="#color/black">
<ImageButton
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#drawable/ic_menu"
android:id="#+id/menu_button"
android:layout_marginLeft="13dp"
android:onClick="moveMenu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Your profile"
android:layout_toRightOf="#id/menu_button"
android:gravity="center_vertical"
android:textSize="25dp"
android:layout_marginLeft="10dp"
android:textColor="#color/white"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="#+id/fragmentPlaceholder"
android:background="#color/black"
android:layout_below="#id/actionBar">
</RelativeLayout>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:text="UPLOAD RESUME"
android:id="#+id/uploadButton"
android:background="#drawable/button_bg_rounded_corners"
android:layout_centerHorizontal="true"
android:onClick="startSignupActivity"
android:drawableLeft="#drawable/ic_action_upload"
android:drawablePadding="0dp"
/>
<TextView
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="This helps us find the perfect job for you"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:textColor="#color/white"
android:textSize="25dp"
android:layout_below="#id/uploadButton"
android:layout_marginTop="70dp"/>
</RelativeLayout>
I'm dynamically adding a fragment onto the place holder and animating it onClick
Here's the Java of my activity:
public class UploadResumeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_resume);
//Hide the place holder, View.GONE implies it occupies no space
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.fragmentPlaceholder);
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragmentPlaceholder, NavigationFragment.newInstance());
ft.commit();
r1.bringToFront();
r1.setVisibility(View.GONE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
#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.
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public void moveMenu(View view) {
RelativeLayout r1 = (RelativeLayout) findViewById(R.id.fragmentPlaceholder);
Animation slideIn,slideOut;
slideIn = AnimationUtils.loadAnimation(this,R.anim.enter_from_left);
slideOut = AnimationUtils.loadAnimation(this,R.anim.exit_to_left);
if(r1.getVisibility() == View.GONE)
{
r1.startAnimation(slideIn);
r1.setVisibility(View.VISIBLE);
}
else
{
r1.startAnimation(slideOut);
r1.setVisibility(View.GONE);
}
}
}
This is how it looks:
When i try open the fragment by clicking:
I need the upload button to go behind the fragment. It works when I use View.setZ(). But my min API level is 14. Why doesn't View.bringToFront() work on the button?
Steve Cs solution to encase my button and Textview inside a RelativeLayout worked.

Calling new Activity from Menu item and getting NPE when setting setOnClickListener on a button

I am a starter in android development and trying some hands on.
Context :
Written a basic app which has multiple menu options and all the options have an activity associated to them , So when a user click on menu option then I am trying to start an activity , which has some basic form widgets like Edit text ,spinner and buttons. I was able to navigate from one activity to other using menu options
Problem:
I am getting below error while setting click listner to submit button
Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference.
I tried debugging the application and found that my widgets are not getting initialized , I have tried adding this code to onCreate, OnStart, OnResume
But getting same results
Below is the activity code(This is not main Activity)
public class AddCar extends AppCompatActivity {
private EditText modelName;
private Button addCar;
private Button refreshBtn;
private Spinner chooseBrandName;
public final String LOG_TAG = AddCar.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#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_add, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
getAllWidgets();
addListenerOnSubmitButton();
}
#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);
}
public void addListenerOnSubmitButton(){
addCar.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
String modelNameText = modelName.getText().toString();
String carBrand=chooseBrandName.getSelectedItem().toString();
Log.d(LOG_TAG,modelNameText);
Log.d(LOG_TAG,carBrand);
}
}
);
}
public void getAllWidgets(){
Log.d(LOG_TAG,"getAllWidgets called");
addCar=(Button)findViewById(R.id.btnCarAdd);
refreshBtn=(Button)findViewById(R.id.btnCarRefresh);
modelName=(EditText)findViewById(R.id.modelName);
chooseBrandName=(Spinner)findViewById(R.id.brandNames);
}
}
and the xml code for this activity is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="#string/CAR_BRAND"
android:textColor="#CCCCCC" />
<Spinner
android:layout_width="0dp"
android:id="#+id/brandNames"
android:layout_height="wrap_content"
android:entries="#array/brand_names"
android:prompt="#string/CHOOSE_CAR"
android:layout_weight="0.60"
android:background="#CCCCCC" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:text="#string/CAR_MODEL_NAME"
android:textColor="#CCCCCC"/>
<EditText
android:id="#+id/modelName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:ems="10"
android:background="#CCCCCC" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:textColor="#CCCCCC"/>
<Button
android:id="#+id/btnCarAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375"
android:text="#string/CAR_ADD_BUTTON" />
<Button
android:id="#+id/btnCarRefresh"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.375"
android:text="#string/CAR_REFRESH_BUTTON" />
</LinearLayout>
Your button is null, you're getting a NullPointerException because you're trying to call setOnClickListener on a null button.
The reason your button is null, is because it's not actually being found in your getAllWidgets() call. The reason for that is because you're not calling setContentView() in onCreate.
Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
}

Android Button click does not work with SliderLayout in RelativeLayout

In my XML layout, I use SliderLayout in RelativeLayout where height and width are match_parent. I have a skip button to finish activity. I think SliderLayout overrides the skip button so I can not use the click event of button. It does not work. How can I avoid of this problem?
activity_tutorial.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_20"
android:text="#string/skip" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
TutorialActivity.java
public class TutorialActivity extends Activity implements
OnSliderClickListener, OnClickListener {
private static final String FONTH_PATH_BUTTON = "fonts/Brandon_bld.otf";
private Typeface buttonFont;
private Button skipButton;
private SliderLayout mDemoSlider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
buttonFont = Typeface.createFromAsset(getAssets(), FONTH_PATH_BUTTON);
skipButton = (Button) findViewById(R.id.skipButton);
skipButton.setTypeface(buttonFont);
skipButton.setOnClickListener(this);
mDemoSlider = (SliderLayout) findViewById(R.id.imageSlider);
HashMap<String, Integer> file_maps = new HashMap<String, Integer>();
file_maps.put("Challenge", R.drawable.tutorial_challenge);
file_maps.put("Select", R.drawable.tutorial_select);
file_maps.put("Image", R.drawable.tutorial_image);
file_maps.put("Friend", R.drawable.tutorial_friend);
for (String name : file_maps.keySet()) {
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView.description(name).image(file_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
// add your extra information
textSliderView.getBundle().putString("extra", name);
mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
mDemoSlider
.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tutorial, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onSliderClick(BaseSliderView slider) {
// TODO Auto-generated method stub
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.skipButton:
finish();
break;
default:
break;
}
}
}
Simply open XML and add the Button in your SliderLayout body. It can be set by dragging the button on top of the SliderLayout.
Your code:
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_20"
android:text="#string/skip" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
Change it to:
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_10"
android:text="#string/skip" />
</com.daimajia.slider.library.SliderLayout>
That should do it. Hope it helps. :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White" >
<com.daimajia.slider.library.SliderLayout
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/skipButton"
style="#style/button_type"
android:layout_width="#dimen/dp_150"
android:layout_height="#dimen/dp_40"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_10"
android:text="#string/skip" />
</com.daimajia.slider.library.SliderLayout>
<com.daimajia.slider.library.Indicators.PagerIndicator
android:id="#+id/custom_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Just move button inside of SliderLayout solves my problem.

onClick setVisibility visible and GONE doesn't work

I have made a small program in which i have used one button and a WebView. WebView visibility is set GONE
and when i press the button 1st time i want to set visibility to visible and when i press the button 2nd time i want the visibility to be GONE. It should do the same thing consecutively. I have tried to make it work using if ..else and with switch .Its strange that if you click the button a lot of times (depending , it can be 3 or 7 or 9 or even more times) the code start to work.
Please help me.
Here is my code:
public class MyMenu extends Activity {
Button info;
WebView webView;
int see=0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_menu);
info = (Button) findViewById(R.id.info);
webView = (WebView) findViewById(R.id.webView1);
webView.setVisibility(View.GONE);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file:///android_asset/odigies.html");
// make listener for odigies button
info.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (see == 0) {
webView.setVisibility(View.VISIBLE);
see = 1;
} else {
webView.setVisibility(View.GONE);
see = 0;
}
}
});
}
//send app with sms
public void send(View v){
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.https://play.google.com/store/apps/details?id=o.tzogadoros");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
}
//send app with email
public void email(View v){
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.fromParts("mailto",
"", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Εφαρμογή Lotto Android");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Δωρεάν καλή εφαρμογή για Λοττο,τζοκερ,κινο και προτο.Δίνει τυχαίους αριθμούς για τα τυχαιρά παιχνίδια. ");
if (emailIntent.resolveActivity(getPackageManager()) == null) {
Toast.makeText(getApplicationContext(),
"Παρακαλώ παραμετροποίησε τον λογοριασμό email σου", Toast.LENGTH_LONG)
.show();
} else {
// Secondly, use a chooser will gracefully handle 0,1,2+ matching
// activities
startActivity(Intent.createChooser(emailIntent,
"Διάλεξε το email σου"));
}
}
// go to tzoker activity
public void tzoker(final View view) {
startActivity(new Intent(this, Tzoker.class));
}
// go to kino activity
public void kino(final View view) {
startActivity(new Intent(this, Kino.class));
}
// go to lotto activity
public void lotto(final View view) {
startActivity(new Intent(this, MainActivity.class));
}
// go to proto activity
public void proto(final View view) {
startActivity(new Intent(this, Proto.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
and the xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eaf39b"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MyMenu" >
<ScrollView
android:id="#+id/vertical_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#eaf39b"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="lotto"
android:src="#drawable/lottoicon" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="tzoker"
android:src="#drawable/tzokericon" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="kino"
android:src="#drawable/kinoicon" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="proto"
android:src="#drawable/protoicon" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menuicon" />
</LinearLayout>
<Button
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:text="#string/Menuodigies"
android:textSize="22sp"
android:textStyle="bold" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/sendsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="send"
android:text="#string/sms"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="#+id/sendemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="email"
android:text="#string/email"
android:textSize="22sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Finally when the webview appear there is no zoom controls,why?
Thanks for your time.
Is it better with that ?
#Override
public void onClick(View v) {
if (webView.getVisibility==View.GONE) {
webView.setVisibility(View.VISIBLE);
} else {
webView.setVisibility(View.GONE);
}
}

Categories

Resources