I have a problem with my first application,this is also my first question,i have 2 activity and 2 layout
this is the MainActivit.java
public class MainActivity extends TabActivity {
TabHost tabHost;
public static int meth;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadAllVar();
UpdateHud(meth);
TabSwitcher();
}
public static int LoadAllVar(){
//Load all var from files (not added for now)
meth = 200;
return meth;
}
private void UpdateHud(int meth){
String methstring = Integer.toString(meth);
TextView textSell = (TextView)findViewById(R.id.textMethCount);
textSell.setText(methstring);
}
public void TabSwitcher() {
tabHost = getTabHost();
TabHost.TabSpec tab1spec = tabHost.newTabSpec("Tab1");
tab1spec.setIndicator("Cook");
Intent firstintent = new Intent(this, CookTab.class);
tab1spec.setContent(firstintent);
tabHost.addTab(tab1spec);
}
}
and this is the second Activity
public class CookTab extends Activity {
public static int meth;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cooktab);
LoadVarFromCook();
CheckCookClick();
}
public void LoadVarFromCook (){
meth = LoadAllVar();
String methstring = Integer.toString(meth);
Toast.makeText(getApplicationContext(),methstring,Toast.LENGTH_SHORT).show();
}
public void CheckCookClick (){
Button buttoncook = (Button) findViewById(R.id.buttonCook);
buttoncook.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
meth = meth + 1;
Toast.makeText(getApplicationContext() , "+1" , Toast.LENGTH_SHORT).show();
//HERE I NEED TO UPDATE THE VALUE OF METH INSIDE THE ACTIVITY_MAIN.XML
}
});
}
}
this is the MainActivity layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TabHost
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"></TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent"></FrameLayout>
</TabHost>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:id="#+id/layoutHUD">
<TextView
android:layout_width="50dp"
android:layout_height="40dp"
android:id="#+id/textMethCount"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp" />
</RelativeLayout>
</RelativeLayout>
and this is the second tab layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/textMeth"
android:layout_marginTop="250dp"
android:layout_marginLeft="145dp"
android:text="Meth"
android:gravity="center"
android:textSize="29dp"
android:textStyle="bold"/>
<Button
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/buttonCook"
android:layout_below="#+id/textMeth"
android:layout_alignLeft="#+id/textMeth"
android:layout_alignStart="#+id/textMeth"
android:text="Cook"/>
</RelativeLayout>
I need to change the text inside a TextView located inside the activity_main.xml when i press a button inside the cooktab.xml how can i do that? findViewById() dosen't work
P.S. for now i add only one tab ,i will add more tab but for now i think about the first one
Try using EventBus it allows you to call functions in other parts of your app. Alternatively use a standard java interface link
I resolve the problem following this tutorial http://mrbool.com/how-to-create-an-activity-android-with-a-tabhost-view/27990
Related
The following code is trivial but simulates the problem.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/btnGoToExtra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="266dp"
android:text="Go To Extra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_extra.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btnBackToMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="186dp"
android:text="Back To Main"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(R.layout.layout_extra);
findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(R.layout.activity_main);
});
});
}
}
Question
I can navigate to the layout_extra from activity_main and return to activity_main. Unfortunately, after this round trip, I can no longer be able to navigate to layout_extra anymore.
What causes this problem and how to fix it?
Explanation will follow in chat but that is not how it is supposed to be done, For views to change, You have other options like using Fragments or using new activity entirely, I would also like to suggest moving to kotlin.
public class MainActivity extends AppCompatActivity {
View mainView;
View extraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
extraView = getLayoutInflater().inflate(R.layout.layout_extra, null);
setContentView(mainView);
mainView.findViewById(R.id.btnGoToExtra)
.setOnClickListener(v -> {
setContentView(extraView);
});
extraView.findViewById(R.id.btnBackToMain)
.setOnClickListener(vv -> {
setContentView(mainView);
});
}
}
Reading through the snippet above, here's how everything went
The line
findViewById(R.id.btnGoToExtra)
.setOnClickListener
Registers Button with id btnGoToExtra for click events and when that happens you have the nested
findViewById(R.id.btnBackToMain)
.setOnClickListener
Which registers the element with id backToMain for click events and that's all
Calling setContentView() multiple times is not recommended. The normal way to switch between layouts in the same activity is to use a ViewFlipper or FrameLayout
From here
You then have:
XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
code
private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}
private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);
// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}
I'm new to Android and recently created a dashboard for my app. I followed a tutorial but it only covered the front end. I created a layout called content_main.xml with three Cardviews. Each of these Cardviews contains the code
android:clickable="true"
Beginning the back end of things, I created a java receiver class called DashboardActivity (as seen below). I want to make my Cardviews clickable so that when one is clicked, I am taken to another screen within my app. The other screens in my app I am trying to get to are fragments. Each of these screens has its own java class in my "Fragments" folder. One of them, of which I try calling on in the code that follows is called SettingsViewFragment().
So far, I have tried the following. It does nothing.
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = (CardView) findViewById(R.id.dash_settings);
cd2 = (CardView) findViewById(R.id.dash_profile);
cd3 = (CardView) findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment1 = new SettingsViewFragment();
}
});
}
}
UPDATE: FULL CODE
public class DashboardActivity extends AppCompatActivity {
CardView cd1, cd2, cd3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
cd1 = findViewById(R.id.dash_settings);
cd2 = findViewById(R.id.dash_profile);
cd3 = findViewById(R.id.dash_activity);
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Testing 123 123 123");
}
});
}
}
And my XML: (it continues for 3 cards this way)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.newproject.DashboardActivity"
tools:showIn="#layout/toolbar">
<FrameLayout
android:id="#+id/dashframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/bg_circle1"
android:src="#drawable/ic_settingspicture"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="View your settings"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow along with guided tutorials"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
Replace
Fragment fragment1 = new SettingsViewFragment();
With this
Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.Main_Layout,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Change Fragment fragment1 = new SettingsViewFragment (); by Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();
//if show toast The problem is not the onClick, the problem is when loading the fragment
Fragments should be instantiated with the newInstance() static method.
cd1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment settingsFragment = SettingsFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.contentView, settingsFragment);
fragmentTransaction.commit();
}
});
In the SettingsFragment
public static SettingsFragment newInstance() {
return new SettingsFragment();
}
XML for content_main
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/dash_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/dash_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</android.support.v7.widget.CardView>
<FrameLayout
android:id="#+id/contentView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
</FrameLayout>
</LinearLayout>
This will transact the fragment into the FrameLayout that is positioned below the CardViews. Now this is probably not ideal, you would most likely want to launch a new activity that will host the fragment but this should work. Unless this is already in some Activity that hosts the fragment. But the key code is in the onClick.
I am taking data from my database using and want to set that as my list item. Upon clicking and holding an item, I want a dialog to open prompting the user to select one of the given options. My code is as follows:
My MainActivity.java class
public class MainActivity extends AppCompatActivity {
ListView todoList;
DbHelper dbHelper;
ArrayAdapter<String> myAdapter;
Button rename, delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DbHelper(this);
todoList=(ListView)findViewById(R.id.to_do);
loadTaskList();
todoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.item_dialog);
dialog.setTitle("Select");
rename = dialog.findViewById(R.id.rename_task);
delete = dialog.findViewById(R.id.delete_task);
dialog.show();
return false;
}
});
}
private void loadTaskList() {
ArrayList<String> taskList=dbHelper.getTask();
if(myAdapter==null){
myAdapter=new ArrayAdapter<String>(this,R.layout.todo_item,R.id.task_title,taskList);
todoList.setAdapter(myAdapter);
}else {
myAdapter.clear();
myAdapter.addAll(taskList);
myAdapter.notifyDataSetChanged();
}
}
My activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.mylist.MainActivity">
<ListView
android:id="#+id/to_do"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"/>
</LinearLayout>
My Layout for the list item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Task"
android:textSize="20sp"
android:layout_centerVertical="true"
android:padding="5dp"/>
<ImageButton
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_delete_black_24dp" />
<ImageButton
android:id="#+id/task_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_black_24dp"
android:layout_toLeftOf="#id/task_delete"/>
</RelativeLayout>
I have some Java code that I want to be triggered everytime any of the buttons in my activity is pressed. I set each button to have an attribute of android:onClick="onClick". However, I get several error codes and am unsure about what to do with them. I get that "variable onClick is never used", "cannot resolve symbol view", and "cannot assign a value to final variable q". What can I do about each one? Many thanks!!
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private static final Random r_generator = new Random();
String textViewString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
final TextView tv = (TextView) findViewById(R.id.color_text);
final String[] myString = res.getStringArray(R.array.englishColorArray);
final String q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
textViewString = tv.getText().toString();
#Override
public void onClick(View view) {
//This casts your view to be a button so you can access its features as a button
textViewString = tv.getText().toString();
Button btn = (Button) view;
if(!btn.getText().equals(textViewString)){
q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
//Perform action to notify user that they pressed the wrong button
//Do not return here. This is what caused the program to lock up
//Maybe add a TextView that says correct/incorrect and can change that when the user is correct or incorrect
}
//Gets a random color from my string array and sets it to the TextView
q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
}
And xml code...
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#android:style/Theme.Holo.Light"
tools:context="com.example.cedric.learnthecolors.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/color_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="68sp"
android:gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/green_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Green"
android:textSize="0sp"
android:background="#drawable/green_button"/>
<Button
android:id="#+id/blue_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Blue"
android:textSize="0sp"
android:background="#drawable/blue_button"/>
<Button
android:id="#+id/red_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Red"
android:textSize="0sp"
android:background="#drawable/the_red_button"/>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/yellow_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Yellow"
android:textSize="0sp"
android:background="#drawable/yellow_button"/>
<Button
android:id="#+id/white_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/White"
android:textSize="0sp"
android:background="#drawable/white_button"/>
<Button
android:id="#+id/orange_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Orange"
android:textSize="0sp"
android:background="#drawable/orange_button"/>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/brown_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Brown"
android:textSize="0sp"
android:background="#drawable/brown_button"/>
<Button
android:id="#+id/pink_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Pink"
android:textSize="0sp"
android:background="#drawable/pink_button"/>
<Button
android:id="#+id/purple_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="#string/Purple"
android:textSize="0sp"
android:background="#drawable/purple_button"/>
</LinearLayout>
</LinearLayout>
This is because you integrated the onClick inside your onCreate (or you simply forgot to make the closing bracket "}" ), therefore the buttons can't reach the method. Insert the missing bracket, like this:
public class MainActivity extends AppCompatActivity {
private static final Random r_generator = new Random();
String textViewString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
final TextView tv = (TextView) findViewById(R.id.color_text);
final String[] myString = res.getStringArray(R.array.englishColorArray);
final String q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
textViewString = tv.getText().toString();
} // this bracket here was missing
#Override
public void onClick(View view) {
//This casts your view to be a button so you can access its features as a button
textViewString = tv.getText().toString();
Button btn = (Button) view;
if(!btn.getText().equals(textViewString)){
q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
//Perform action to notify user that they pressed the wrong button
//Do not return here. This is what caused the program to lock up
//Maybe add a TextView that says correct/incorrect and can change that when the user is correct or incorrect
}
//Gets a random color from my string array and sets it to the TextView
q = myString[r_generator.nextInt(myString.length)];
tv.setText(q);
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//some code
#override
onCreate(bundle SavedState){
//some code
button1.setOnClickListener(this);
button2.setOnclickListener(this);
}
//your click listener
#override
onClick(View view){
int id = view.getID();
}
}
In short, use click listener interface
Your code is messed up.
Your activity should be declared as implementing View.OnclickListener interface.
Your onClick
function should be in the Activity class, not within the onCreate
function.
You should declare q as a member variable of your class, and should not be marked as final.
So my layout is still visible, maybe someone sees mistake? I want to dispear it when infoButton(imagebutton) is clicked.
FrameLayout infolayout;
infolayout = (FrameLayout) findViewById(R.id.infoLayout);
public void infoPressed(View v){//info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
<FrameLayout
android:layout_width="193dp"
android:layout_height="200dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/serviceLayout"
android:id="#+id/infoLayout">
I wacthed: How to change visibility of layout programaticly
EDITED:
wrong code part, still not working for me
*Try 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.example.demo.MainActivity" >
<FrameLayout
android:id="#+id/serviceLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#000000" />
<Button
android:id="#+id/btnHide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="infoPressed"
android:text="Hide" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
FrameLayout infolayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infolayout = (FrameLayout) findViewById(R.id.serviceLayout);
}
public void infoPressed(View v){ //info button is pressed by user
//infoLayout.setVisibility(View.GONE);
infolayout.setVisibility(View.INVISIBLE);
}
}
if your ID declaration is correct I guess you have to change the line
infolayout.setVisibility(View.INVISIBLE);
to
infolayout.setVisibility(FrameLayout.INVISIBLE);