I have a problem with creating things using LayoutInflater:
package com.tip.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements View.OnClickListener {
EditText billamount, percent, people;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
billamount = (EditText)findViewById(R.id.billtext);
percent = (EditText)findViewById(R.id.percenttext);
people = (EditText)findViewById(R.id.numberpeople);
Button unevensplitbutton = (Button)findViewById(R.id.unevensplit);
Button calculates = (Button)findViewById(R.id.calculate);
unevensplitbutton.setOnClickListener(this);
calculates.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the XML of calculatenumbertable:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numbertable2">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Each Person Pays"
android:id="#+id/numberpeople"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/eachpersonedit"
android:layout_column="6" />
</TableRow></TableLayout>
And the XML of fragment_main:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bill amount"
android:id="#+id/bill"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/billtext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage %"
android:id="#+id/percentage"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/percenttext"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of people"
android:id="#+id/textView"
android:layout_column="3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/numberpeople"
android:layout_column="6" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Split Unevenly"
android:id="#+id/unevensplit"
android:layout_column="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/calculate"
android:layout_column="6" />
</TableRow>
</TableLayout>
</ScrollView>
It returns an error where:
12-16 21:22:56.825 28459-28459/com.tip.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.tip.calculator.MainActivity.onClick(MainActivity.java:35)
Where line 35 is the calculatenumberinflated.addView(view); line of code.
I feel lost right now, can someone please clarify what I am doing wrong? Thanks.
You are getting Nullpointer error at this line TableLayout calculatenumberinflated = (TableLayout)findViewById(R.id.numbertable2); which is not at all available in your fragment_main layout. It in the layout calculatenumbertable file and as you are inflating fragment_main layout so its not getting the correct id of your TableLayout.
Try to add your layout in TableRow besides TableLayout. Create separate row in your calculatenumbertable layout and then try add the layout in that row. You can not add layout directly in TableLayout.
Try as below:
In your calculatenumbertable add row in last as below:
<TableRow
android:id="#+id/tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableRow>
In your onClick add the layout as below:
public void onClick(View v) {
switch(v.getId()){
case R.id.calculate:
TableRowcalculatenumberinflated = (TableRow)findViewById(R.id.tablerow);
View view = getLayoutInflater().inflate(R.layout.calculatenumbertable,calculatenumberinflated,false);
calculatenumberinflated.addView(view);
break;
case R.id.unevensplit:
break;
}
}
Just move android:id="#+id/numbertable2" from calculatenumbertable.xml to fragment_main.xml in the tableLayouts.
Try this
LayoutInflater inflater=getLayoutInflater();
View view= inflater.inflate(R.layout.calculatenumbertable, null);
calculatenumberinflated.addView(view);
Related
So I have 2 edittext fields which to me look the same in my android project.
They are both for decimals.
However the KM one keeps bringing up a keyboard with the option of entering letters and doesn't let me close the keyboard.
The Miles one opens up a number only keyboard and has a checkmark which lets me close the keyboard.
Wondering if something in the code is making this happen
I have added images below
package a00891437.set3s.comp3975.convertunits;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
#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 onRadioButtonClicked(final View view){
EditText mText = (EditText) findViewById(R.id.MilesText);
EditText kmText = (EditText) findViewById(R.id.KilometersText);
mText.setText("");
kmText.setText("");
if(view.getId()== R.id.KmToMButton) {
mText.setEnabled(false);
kmText.setEnabled(true);
}
if(view.getId() == R.id.MToKmButton){
kmText.setEnabled(false);
mText.setEnabled(true);
}
}
public void onClickedText (final View view){
EditText kilometers = (EditText) findViewById(R.id.KilometersText);
EditText miles = (EditText) findViewById(R.id.MilesText);
double km= 0;
double m = 0;
if(kilometers.isEnabled() == true){
km = Double.parseDouble(kilometers.getText().toString());
m = km * 0.621371;
miles.setText(Double.toString(m));
}
if(miles.isEnabled() == true){
m = Double.parseDouble(miles.getText().toString());
km = m * 1.60934;
kilometers.setText(Double.toString(km));
}
}
}
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/screen">
TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp"
android:onClick="onClickedText"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/KilometersText"
android:layout_above="#+id/button"
android:layout_marginBottom="134dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:enabled="false"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/MilesText"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/KilometersText"
android:layout_alignStart="#+id/KilometersText"
android:enabled="false"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Km"
android:id="#+id/Kilometers"
android:layout_alignTop="#+id/KilometersText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="M"
android:id="#+id/Miles"
android:layout_alignTop="#+id/MilesText"
android:layout_alignRight="#+id/Kilometers"
android:layout_alignEnd="#+id/Kilometers" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/radio_group">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kilometers to Miles"
android:id="#+id/KmToMButton"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/MToKmButton"
android:layout_alignStart="#+id/MToKmButton"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Miles to Kilometers"
android:id="#+id/MToKmButton"
android:layout_below="#+id/KmToMButton"
android:layout_alignLeft="#+id/Kilometers"
android:layout_alignStart="#+id/Kilometers"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
</RadioGroup>
</RelativeLayout>
Also had this problem once where the EditTexts didn't seem to listen to the XML. this fixed it for me:
kmText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
So I am making an app for an assignment.
Basically convert Miles to KM or KM to Miles.
I've now got the radio buttons working but when I add the onclick event, upon clicking the edittext fields I get an exception. :
java.lang.IllegalArgumentException:
Expected receiver of type a00891437.set3s.comp3975.convertunits.MainActivity,
but got android.support.v7.internal.widget.TintContextWrapper
Here is the code
package a00891437.set3s.comp3975.convertunits;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 onRadioButtonClicked(final View view) {
View mText = findViewById(R.id.MilesText);
View kmText = findViewById(R.id.KilometersText);
if (view.getId() == R.id.KmToMButton) {
mText.setEnabled(false);
kmText.setEnabled(true);
}
if (view.getId() == R.id.MToKmButton) {
kmText.setEnabled(false);
mText.setEnabled(true);
}
}
public void onClickedText(final View view) {
Log.d("MainActivity", "Clicked");
}
}
The 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/screen">
TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="110dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/KilometersText"
android:layout_above="#+id/button"
android:layout_marginBottom="134dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:enabled="false"
android:onClick="onClickedText"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/MilesText"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/KilometersText"
android:layout_alignStart="#+id/KilometersText"
android:enabled="false"
android:onClick="onClickedText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Km"
android:id="#+id/Kilometers"
android:layout_alignTop="#+id/KilometersText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="M"
android:id="#+id/Miles"
android:layout_alignTop="#+id/MilesText"
android:layout_alignRight="#+id/Kilometers"
android:layout_alignEnd="#+id/Kilometers" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kilometers to Miles"
android:id="#+id/KmToMButton"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/MToKmButton"
android:layout_alignStart="#+id/MToKmButton"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Miles to Kilometers"
android:id="#+id/MToKmButton"
android:layout_below="#+id/KmToMButton"
android:layout_alignLeft="#+id/Kilometers"
android:layout_alignStart="#+id/Kilometers"
android:checked="false"
android:onClick="onRadioButtonClicked"/>/>
</RadioGroup>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp" />
</RelativeLayout>
You are specifying the onClick event method through XML, and it looks like this does not work with support library AppCompatActivity.
You should just get the reference to the EditText and call setOnClickListener().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText kilometers = (EditText) findViewById(R.id.KilometersText);
kilometers.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClickedText(v);
}
});
// repeat this for the miles EditText
// you'll need to add an id for the radio group for this to work:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangedListener(new RadioGroup.OnCheckedChangedListener() {
// I leave the code inside here for you to do as an exercise
});
}
06-30 11:59:40.153 27202-27202/com.downloader.newlevel.krim.newleveldownloader E/ViewSystem﹕ ViewRootImpl #2 Surface is not valid.
06-30 11:59:41.114 27202-27216/com.downloader.newlevel.krim.newleveldownloader E/Error﹕ https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2
06-30 12:00:57.429 27202-27202/com.downloader.newlevel.krim.newleveldownloader E/ViewSystem﹕ ViewRootImpl #2 Surface is not valid.
06-30 12:00:58.219 27202-27755/com.downloader.newlevel.krim.newleveldownloader E/Error﹕ https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2
This was found in my logcat, I am currently working on loading image from url.
Activity
package com.downloader.newlevel.krim.newleveldownloader;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import java.io.InputStream;
public class activity00 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity00);
String url000 = "https://photos-4.dropbox.com/t/2/AAAxrzwz2B32dmtjRTNlT1vhQfVd5JuglMkbVV_m8BRkLg/12/145533347/png/32x32/1/_/1/2/Hourglass%20Addon%20by%20Redstone.png/CKPTskUgASACIAMgBCAFIAYgBygBKAI/Jpa4t4f3nMDieOULhjrmus_8mVpqVO37XxrQYkzMoSo?size=800x600&size_mode=2";
new DownloadImageTask((ImageView) findViewById(R.id.image_main00)).execute(url000);
Intent splash = new Intent(this, splash_activity.class);
startActivity(splash);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, 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_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AsyncTask downloading image
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
xmlns:tools="http://schemas.android.com/tools"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
tools:context=".activity00"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TableLayout
android:id="#+id/tableLayout_main00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow>
<Button
android:text="Script"
android:textAllCaps="false"
android:background="#ffffff"/>
<Button
android:text="Add-on"
android:textAllCaps="false"
android:background="#fffffa6a"/>
</TableRow> <TableRow>
<Button
android:text="Map"
android:textAllCaps="false"
android:background="#ff6ec0ff"/>
<Button
android:text="Skin"
android:textAllCaps="false"
android:background="#ffff6d66"/>
</TableRow> <TableRow>
<Button
android:text="Texture Pack"
android:textAllCaps="false"/>
<Button
android:text="Coming Soon"
android:textAllCaps="false"/>
</TableRow>
</TableLayout>
<TextView
android:id="#+id/text_main00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New"
android:textSize="20sp"
android:layout_below="#+id/tableLayout_main00"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="20dp"/>
<android.support.v7.widget.CardView
android:id="#+id/cardview_main00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:layout_below="#+id/text_main00"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main00"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hourglass Addon by Redstone"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/text_main01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Popular"
android:textSize="20sp"
android:layout_below="#+id/cardview_main00"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.CardView
android:layout_below="#+id/text_main01"
android:id="#+id/cardview_main01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main01"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Armor Display by Chan"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/text_main02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Coming Soon"
android:textSize="20sp"
android:layout_below="#+id/cardview_main01"
android:clickable="true"
android:gravity="right"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.CardView
android:layout_below="#+id/text_main02"
android:id="#+id/cardview_main02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="4dp"
android:clickable="true"
android:layout_marginTop="5dp">
<ImageView
android:id="#+id/image_main02"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scaleType="fitStart"/>
<TextView
android:id="#+id/text_main_cardview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PortalPX by TM"
android:layout_above="#+id/text_main_cardview22"/>
<TextView
android:id="#+id/text_main_cardview22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Limited Edition: Cabon Version"
android:layout_gravity="bottom"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
I don't know why this logcat appeared. Can anyone explain and solve it please?
"ViewRootImpl #2 Surface is not valid." in logcat means something went wrong in layout. I fixed mine by writing whole layout again...to make sure everything is okay :)
Splash screen had nothing to do with this error
I'm not entirely convinced your ImageView belongs to the layout in which you set the content view to.
Double check if that layout is correct, because this error occurs when you execute findViewById without inflating the parent layout view first with setContentView or with LayoutInflator.
Me too faced the same issue, later I saw that I had made a .xml in res/values during renaming my cardView ids bcoz of which I was facing error, after i deleted that file, my app ran smoothly.
I want to display the hint of each country. When I click the country name it should display the description. For this I have designed the XML with a relative layout with two or more text views (country1,country2) and one text view to be display with description on clicking any country.
The problem is, when I click country 1 the run time text view is not displaying for country 2.
But for country 3 when I click it it is displaying at the bottom. So this means that the text views are not aligning. I get the concept but I'm stuck on how to implement it.
I'm using API version 21
Here's my Java:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class savev extends Activity implements OnClickListener{
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
t1=(TextView) findViewById(R.id.textview4);
t1.setVisibility(View.GONE);
findViewById(R.id.textview1).setOnClickListener(this);
findViewById(R.id.textview2).setOnClickListener(this);
findViewById(R.id.textview3).setOnClickListener(this);
}
public void toggle_contents(View v,int r,int s){
/*RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, r);*/
t1.setVisibility(t1.isShown()? View.GONE: View.VISIBLE);
t1.setText(s);
/*t1.setLayoutParams(p);*/
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.textview1:
toggle_contents(v,R.id.textview1,R.string.country1Meaning);
break;
case R.id.textview2:
toggle_contents(v,R.id.textview2,R.string.country2Meaning);
break;
case R.id.textview3:
toggle_contents(v,R.id.textview3,R.string.country1Meaning);
break;
default:break;
}
}
}
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rl"
>
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:text="country1"
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
>
</TextView>
<TextView
android:id="#+id/textview4"
android:drawablePadding="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dip"
android:drawableLeft="#drawable/customborder"
android:drawableStart="#drawable/customborder"
android:background="#FF0000"
>
</TextView>
<TextView
android:id="#+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:text="country2"
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
android:layout_below="#+id/textview1"
>
</TextView>
<TextView
android:id="#+id/textview3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:text="country3"
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
android:layout_below="#+id/textview2"
>
</TextView>
</RelativeLayout>
</ScrollView>
change relative layout to linear
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mainLayout"
>
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:text="country1"
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
>
</TextView>
<TextView
android:id="#+id/textview2"
android:drawablePadding="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dip"
android:drawableLeft="#drawable/customborder"
android:drawableStart="#drawable/customborder"
android:background="#FF0000"
>
</TextView>
<TextView
android:id="#+id/textview3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:text="country2"
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
android:layout_below="#+id/textview1"
>
</TextView>
<TextView
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:color="#FFFFFF"
android:padding="25dip"
android:visibility="gone
android:textStyle="bold"
android:clickable="true"
android:background="#D8D8D8"
android:layout_below="#+id/textview2"
>
</TextView>
</RelativeLayout>
</ScrollView>
Change description text view position dynamically:
public class savev extends Activity implements OnClickListener{
TextView txtDesc;
ViewGroup mainContainer; //Linearlayout with textviews
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
txtDesc=(TextView) findViewById(R.id.textview4);
mainContainer = (ViewGroup) findViewId(R.id.mainLayout);
txtDesc.setVisibility(View.GONE);
findViewById(R.id.textview1).setOnClickListener(this);
findViewById(R.id.textview2).setOnClickListener(this);
findViewById(R.id.textview3).setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.textview1:
txtDesc.setText(getString(R.string.country1Meaning));
break;
case R.id.textview2:
txtDesc.setText(getString(R.string.country2Meaning));
break;
case R.id.textview3:
txtDesc.setText(getString(R.string.country3Meaning));
break;
default:break;
}
mainContainer.removeView (txtDesc)
int pos = mainContainer.indexOfChild(v);
if(pos>0) {
txtDesc.setVisibility(VISIBLE);
if(pos == getChildCount)
mainContainer.addView(txtDesc);
else
mainContainer.addView(txtDesc,pos+1);
}
}
}
Im taking my first steps in developing for android. Following a tutorial i have created a button, textview,edit text, and am now trying to implimetn a list view.
i have followed the tutorial Exactly, and for some reason, when i implement the Tableview, my button and image disappear.
can anybody find out why?
.java:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
TextView mainTextView;
Button mainButton;
EditText mainEditText;
ListView mainListView;
ArrayAdapter mArrayAdapter;
ArrayList mNameList = new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//access the textview defined in Layout XML & set its value
mainTextView = (TextView) findViewById(R.id.main_textview);
mainTextView.setText("set In Java!");
//access the button defined in layout XML
mainButton = (Button) findViewById(R.id.main_button);
mainButton.setOnClickListener(this);
mainEditText = (EditText) findViewById(R.id.main_edittext);
mainListView = (ListView)findViewById(R.id.main_listview);
mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,mNameList);
mainListView.setAdapter(mArrayAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
mainTextView.setText(mainEditText.getText().toString() + " is learning Android Development");
mNameList.add(mainEditText.getText().toString());
mArrayAdapter.notifyDataSetChanged();
}
}
activity.XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<!--shows an image from your drawable rescources-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/main_textview"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/textview"/>
<EditText
android:id="#+id/main_edittext"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/hint"/>
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="#string/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
</LinearLayout>
Try this..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/main_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="textview" />
<EditText
android:id="#+id/main_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:hint="hint" />
</LinearLayout>
<ListView
android:id="#+id/main_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- set OnClickListener to trigger results when pressed -->
<Button
android:id="#+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="button" />
<!-- shows an image from your drawable rescources -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
LinearLayout is horizontal by default.
Your ListView is layout_width="match_parent" and root LinearLayout is horizontal.
so ListView Expands wide, ImageView and etc have pushed out and become invisible.
I recommend you to always specify android:orientation ("vertical" or "horizontal") in LinearLayout.