I have this screen.
And want to convert it to this.
I hope you understand what I am trying to do. Here is my xml code.
<?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:background="#color/appBackground"
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"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="loginScreen.solution.example.com.loginScreen.WelcomeActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#CC5ec639"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Name: "
android:backgroundTint="#color/textboxTint"
/>
<TextView
android:id="#+id/tv_email"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Email: "/>
<TextView
android:id="#+id/tv_phone"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Phone: "/>
</LinearLayout>
and my activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button loginButton;
Button signUpButton;
ViewFlipper viewFlipper;
#Override
public void onCreate(Bundle instanceState) {
super.onCreate(instanceState);
setContentView(R.layout.content_main);
loginButton = (Button)findViewById(R.id.bt_login);
loginButton.setTag(true);
signUpButton = (Button)findViewById(R.id.bt_signup);
viewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
loginButton.setOnClickListener(this);
signUpButton.setOnClickListener(this);
updateTheViewBasedOnTag();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
loginButton.setTag(true);
updateTheViewBasedOnTag();
viewFlipper.startFlipping();
break;
case R.id.bt_signup:
signUpButton.setTag(true);
updateTheViewBasedOnTag();
break;
}
}
private void updateTheViewBasedOnTag() {
if((boolean)loginButton.getTag())
{
loginButton.setBackground(getDrawable(R.drawable.selected_background));
signUpButton.setBackground(getDrawable(R.drawable.normal_background));
}else
{
loginButton.setBackground(getDrawable(R.drawable.selected_background));
loginButton.setBackground(getDrawable(R.drawable.normal_background));
}
}
}
The
viewFlipper.startFlipping();
doesn't seem to work correctly. I don't click the signup button but still the sign up for is shown. Also, the clicking of the buttons don't respond
What's wrong?
Thanks,
Theo.
I am just trying to guess what is wrong with your code. You are setting the tag to true for one button and forgot to set the false for other button.
switch (v.getId()) {
case R.id.bt_login:
loginButton.setTag(true);
signUpButton.setTag(false);
updateTheViewBasedOnTag();
viewFlipper.startFlipping();
break;
case R.id.bt_signup:
signUpButton.setTag(true);
loginButton.setTag(false);
updateTheViewBasedOnTag();
break;
}
Related
I was updating an existing code by introducing dismiss button, and the onClick Butterknife method is not working for dismiss button, whereas its working for close button. In the below code, I am enabling the close button for some devices and for other devices I am enabling the dismiss button. I am quite unsure why this "dismiss" button is not working, whereas the close button is working fine. And both of them use the ButterKnife, onClick method - onSkip()
InfoActivity.java :
public class InfoActivity extends BccActiviy {
#BindView(R.id.ImageView_CloseView)
ImageView tCloseView;
#BindView(R.id.Dismiss)
Button tDismiss;
.....
#OnClick({R.id.Dismiss, R.id.ImageView_CloseView})
public void onSkip(View v) {
onDissmissPressed();
}
public void initialize(Bundle savedInstanceState) {
if (tConfig.isDismiss()) {
tCloseView.setVisibility(View.GONE);
tDismiss.setVisibility(View.VISIBLE);
} else {
tDismiss.setVisibility(View.GONE);
}
}
}
info_screen.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:ignore="All"
android:id="#+id/root_layout"
android:background="#color/fd_theme"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ImageView_CloseView"
style="#style/CancelButton"
android:layout_width="#di/target_size"
android:layout_marginTop="#di/p_6"
android:layout_height="#di/target_size"
android:layout_alignParentStart="true"
android:clickable="true"
android:contentDescription="#string/close_btn_txt"
android:importantForAccessibility="yes"
android:paddingStart="#di/p_14"
android:paddingEnd="#di/p_14"
android:background="?attr/bg_color"
app:srcCompat="#drawable/icn_close_white"/>
<LinearLayout
android:id="#+id/TextView_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/TextContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="#di/size_2x"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#color/white"
android:textSize="#di/rating"
android:textAlignment="center"/>
<com.package.button.star.views.widgets.StaView
android:id="#+id/StarView_AppRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
</com.package.button.star.views.widgets.StarView>
<Button
android:id="#+id/Dismiss"
style="#style/buttonStyle"
android:layout_marginTop="#di/margin_top"
android:paddingLeft="#di/padding_50"
android:paddingTop="#di/padding_18"
android:paddingRight="#di/padding_50"
android:paddingBottom="#di/padding_18"
android:text="Dismiss"
android:onClick="onSkip"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="#di/dismiss_fontSize"
android:visibility="visible"
android:focusable="true"
tools:text="Dismiss" />
<ViewStub
android:id="#+id/actions_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#di/margin_center"
android:inflatedId="#+id/view_actions"
android:layout="#layout/view_actions" />
</LinearLayout>
</FrameLayout>
Try removing the onClick from xml.
I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?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:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?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"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}
this sounds easy at the beginning but is driving me insane.
So i downloaded the latest android sdk and eclipse and now there is somthing new.... :
when iam creating a Activity and a Layout it generates me 2 Layout files somthing like: main_laout.xml and fragment_main.xml
however eclipse opend up only the fragment file and i made my GUI there. When iam starting my Application all my Buttons and TextViews are there. I Press a button and a Second Activity starts.
And here my Problem: The Second Activity is like the First one (the 2 layout xml files but here called status)
when iam trying to change a TextView there i get a nullPointer exception. Can anyone plz help me with this iam getting crazy.
My Code so far:
statusActivity:
public class StatusActivity extends ActionBarActivity{
private TextView version,dbstatus,dbrows;
private Button done,refresh;
NetworkTask task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
version=(TextView) findViewById(R.id.versionsOutputTextSTATUS);
dbstatus=(TextView) this.findViewById(R.id.dbstatusOutputTextSTATUS);
dbrows=(TextView) this.findViewById(R.id.dbRowsOutputTextSTATUS);
done=(Button) this.findViewById(R.id.beendenButtonSTATUS);
refresh=(Button) this.findViewById(R.id.refreshButtonSTATUS);
version.setText("Test");
}
And my xml files:
activity_status.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.StatusActivity"
tools:ignore="MergeRootFrame" />
fragment_status.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="#drawable/eisblumen"
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="de.project.zigbeecontrol.StatusActivity$PlaceholderFragment" >
<TextView
android:id="#+id/welcomeTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="#string/status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/versionsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/versionTextSTATUS"
android:layout_alignBottom="#+id/versionTextSTATUS"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/versionTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dbstatusTextSTATUS"
android:layout_alignLeft="#+id/versionsOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/beendenButtonSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/welcomeTextSTATUS"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/welcomeTextSTATUS"
android:background="#drawable/button_trans"
android:text="#string/endeStatus"
android:onClick="onClickStatus" />
<TextView
android:id="#+id/dbRowsTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/beendenButtonSTATUS"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_marginBottom="26dp"
android:text="#string/dbrows"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/beendenButtonSTATUS"
android:text="#string/datenbankstatus"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/versionTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_below="#+id/welcomeTextSTATUS"
android:layout_marginTop="56dp"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbRowsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/dbRowsTextSTATUS"
android:layout_alignBottom="#+id/dbRowsTextSTATUS"
android:layout_alignLeft="#+id/dbstatusOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/refreshButtonSTATUS"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/welcomeTextSTATUS"
android:layout_marginBottom="17dp"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/welcomeTextSTATUS"
android:text="#string/refresh"
android:onClick="onClickStatus" />
</RelativeLayout>
even when its working i post the parts of the "main" programm too so you can see how i worked there :
MainActivity:
public class MainActivity extends ActionBarActivity{
//Buttons
private Button beendenButton;
private Button statusbutton;
private Button restartButton;
private Button auswertungButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/*Zuweißung der Buttons */
beendenButton=(Button) this.findViewById(R.id.endButtonMAIN);
statusbutton=(Button) this.findViewById(R.id.statusButtonMAIN);
restartButton=(Button) this.findViewById(R.id.restartButtonMAIN);
auswertungButton=(Button)this.findViewById(R.id.auswertungButtonMAIN);
/*Fertig mit initzialisieren warten auf Eingabe*/
}
private void statusMethod() {
try{
Intent intent = new Intent(this, StatusActivity.class);
startActivity(intent);
this.finish();
}catch(Exception e){errorMessage();}
}
//.... Some uninterresting Stuff here
public void onClick(View v)
{
switch (v.getId())
{
/*Auswahl was gedrückt wurde und aufruf der Entsprechenden Methode */
case R.id.endButtonMAIN: endActivity(); break;
case R.id.statusButtonMAIN: statusMethod();break;
case R.id.restartButtonMAIN: restartMethod();break;
case R.id.auswertungButtonMAIN: sensorMethod(); break;
default: break;
}
}
}
And the two xmls from layout:
activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.MainActivity"
tools:ignore="MergeRootFrame"
/>
and last but not least fragment_main:
<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="#drawable/eisblumen"
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="de.project.zigbeecontrol.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/endButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/welcomeTextMAIN"
android:background="#drawable/button_trans"
android:text="#string/beenden"
android:onClick="onClick" />
<TextView
android:id="#+id/welcomeTextMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/endButtonMAIN"
android:layout_centerHorizontal="true"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/statusButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcomeTextMAIN"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:background="#drawable/button_trans"
android:text="#string/status"
android:onClick="onClick" />
<Button
android:id="#+id/restartButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/statusButtonMAIN"
android:layout_below="#+id/statusButtonMAIN"
android:layout_marginTop="19dp"
android:background="#drawable/button_trans"
android:text="#string/neustart"
android:onClick="onClick" />
<Button
android:id="#+id/auswertungButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/restartButtonMAIN"
android:layout_below="#+id/restartButtonMAIN"
android:layout_marginTop="33dp"
android:background="#drawable/button_trans"
android:text="#string/sensoren"
android:onClick="onClick" />
</RelativeLayout>
So PLZ! why do i get a NPE when trying:
version.setText("Test");
regards
Try this..
Change this..
setContentView(R.layout.activity_status);
to
setContentView(R.layout.fragment_status);
because TextViews and Buttons are in fragment_status.xml so setContentView should refer fragment_status.xml
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Ans same like that in MainActivity.java
Change this..
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Here is your problem
setContentView(R.layout.activity_status);
but your text view is not present in activity_status.xml. Create your Views in activity_status.xml instead of
fragment_status.xml and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
I have a piece of code which shows a static text, an edit text as a search box and a submit button to submit the query .
The code kind of looks like this :
public class MyActivity extends Activity {
TextView textView;
private EditText edittext;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("Enter your search String :");
addKeyListener();
addListenerOnButton();
}
//Implement the method
public void addKeyListener() {
// get edit text component
edittext = (EditText) findViewById(R.id.searchText);
// add a key listener to keep track user input
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if key down and "enter" is pressed
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
edittext.getText(), Toast.LENGTH_LONG).show();
return true;
} else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
// display a floating message
Toast.makeText(RecipeActivity.this,
"Aboriginal text!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
//Implement the button
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/* I will capture the search string here */
}
});
}
}
The activity screen comes up like this :
EDIT : The XML layout:
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".RecipeActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:label="#string/search_label"
android:hint="#string/search_hint" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</RelativeLayout>
As you can see the submit button is overlapping the edit text search box. How can i make the button element come down ?
It would be better to use LinearLayout with android:orientation="vertical" in your xml Layout because it's much cheaper than RelativeLayout to render.
Solution with LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
If you have to use RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/searchText"
android:text="Submit" />
</RelativeLayout>
See RelativeLayout Documentation for further Informations.
Use android:layout_alignParentBottom="true" in your
add android:below reference
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:below="#+id/searchText"
android:text="Submit" />
I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated
MainActivity.java
public class MainActivity extends SherlockActivity implements OnClickListener {
private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
doneButton = (Button) findViewById(R.id.done_button);
customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
customToastCloseButton.setOnClickListener(this);
doneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_button:
Crouton.cancelAllCroutons();
customToastMessageView.setText("Message");
Crouton.show(this, customToastView);
break;
case R.id.close_button:
Crouton.cancelAllCroutons();
break;
}
}
}
toast_custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/messages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="146dp"
android:text="Button" />
</RelativeLayout>