package com.example.kym;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.TextView;
public class Accumulation extends ActionBarActivity
{
TextView tv1,tv2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
tv1=(TextView)findViewById(R.id.acc1);
Typeface font=null;
tv2=(TextView)findViewById(R.id.acc2);
try
{
font = Typeface.createFromAsset(getAssets(),"fonts/Devanagari.ttf");
getData= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String s=getData.getString("lang", "E");
if(s.contentEquals("E"))
setContentView(R.layout.accumulation);
else if(s.contentEquals("H"))
{
setContentView(R.layout.accumulationh);
tv1.setTypeface(font);
tv1.setText("कर दिया");
}
else if(s.contentEquals("B"))
{
setContentView(R.layout.accumulationb);
}
getSupportActionBar().setTitle("Accumulation Distribution");
Drawable d=getResources().getDrawable(R.drawable.blue);
getSupportActionBar().setBackgroundDrawable(d);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#drawable/blue"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="insideInset">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/accimg1"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:text=""/>
<ImageView
android:id="#+id/acc1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/acc2"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Why is there a ClassCastException?
I am trying to use use a custom TypeFace on textView1 but its giving the same error everytime.
TextView1 is empty & i want to use a typeface to implement a different language. But everytime it is showing blank even when I applied tv1.setText.
You can't call findViewById() until after you've inflated the containing view by calling setContentView() on it or by inflating it manually.
Related
i did not suppose how to take the icon so that it would fill whole screen in the android.
it also gave the error java.lang.NullPointerException: Attempt to write to null array
my mainActivity file
package com.example.dk.bfit1;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import java.util.concurrent.ThreadLocalRandom;
public class MainActivity extends AppCompatActivity {
Button[] button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button[0]=(Button)findViewById(R.id.a1);
button[1]=(Button)findViewById(R.id.a2);
button[2]=(Button)findViewById(R.id.a3);
button[3]=(Button)findViewById(R.id.a4);
button[4]=(Button)findViewById(R.id.a5);
button[5]=(Button)findViewById(R.id.a6);
button[6]=(Button)findViewById(R.id.a7);
button[7]=(Button)findViewById(R.id.a8);
button[8]=(Button)findViewById(R.id.a9);
button[9]=(Button)findViewById(R.id.a10);
run();
}
void run(){
int i=0;
while(i<10){
i++;
int n =(int)(Math.random() *9 + 1);
try{
for(int j=0;i<10;j++) {
button[n].setBackgroundResource(android.R.color.black);
Thread.sleep(800);
Log.d("Dk", "BUtton icon is"+n);
}
}catch(InterruptedException ex){
Log.d("dk","error at "+ex);
}
}
}
my main xml file
<?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:id="#+id/activity_main"
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="com.example.dk.bfit1.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a6" />`enter code here`
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"`enter code here`
android:id="#+id/a7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a9" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/a10" />
</LinearLayout>
You need to initialise your Button Array.
Try Button[] button = new Button[10]; where 10 is the length of your array.
For more info about Arrays check: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
I have an app in which i want to assign a bitmap named original to an image view whose id is img1 in the layout by taking the position of the image view in the layout(here it is child at position 0). When i click the save button,i want to assign bitmap named original to image view whose id is img1 in the layout.I know we can assign by using ourImageView.setImageBitmap(bitmaptobeassigned). But in my app,my situation is to assign the bitmap original by means of View object child only.Please help me. My main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View child=frame.getChildAt(0);
//Here i obtained image view as a child.But I do not know how to assign bitmap to it.
//please help me how to write code here.
}
});
}
}
and my xml file is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I have found my answer.my main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView child=(ImageView) frame.getChildAt(0);
child.setImageBitmap(original);
}
});
}
}
and my xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I wanted to do some thing like this as shown in image below,Is there any widget to do this or we need to add a view & make it transparent.
Thanks .
you must be create a custom view for it and make it transparent..
Main Activity:
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopup();
}
#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;
}
public void initPopup() {
final Dialog view = new Dialog(this);
view.requestWindowFeature(Window.FEATURE_NO_TITLE);
view.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
view.setContentView(R.layout.transparent_layout);
view.setCanceledOnTouchOutside(true);
view.show();
RelativeLayout rl_chooseone_menu_main = (RelativeLayout) view
.findViewById(R.id.rl_chooseone_menu_main);
rl_chooseone_menu_main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.dismiss();
}
});
/*Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
view.cancel();
view.dismiss();
}
}, 3000);*/
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This is the Transparent View name is transparent_layout.xml:
<?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:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/rl_chooseone_menu_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="30dp"
android:text="Tap to view all" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:text="Tap to place your order" />
</RelativeLayout>
</RelativeLayout>
Enjoy Brother,i have used dimmy images,replace it with your own images..
You could make a tutorial Activity with a semi-transparent background, containing ImageViews and/or TextViews as shown in your example.
Tutorial activity's layout
<LinearLayout ...
android:background="#color/semitransparent"
... >
...
colors.xml
...
<color name="semitransparent">#80000000</color>
...
Then just start it on top of your usual Activity if you notice this is the first launch of your app by the user.
i am developing egreeting card application.i have two xml file in first xml file it contain buttons and list view.when i click on button list will display this list occupy the space of activity.So we cant put any widget in xml file.When click on list item i want to shows ImageView at same place of list .For to achieve this i created another xml and put ImageView inside that.now when i click on ListItem it new xml shows but not in same activiy.how could solve this.i attached full source code here. here is my xml files
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backcolor"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/linearback" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/list_items" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="6dp"
android:background="#drawable/search" />
</LinearLayout>
<!--
<LinearLayout
android:id="#+id/mylayouttwo"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imgview1"
android:layout_width="match_parent"
android:layout_height="300dp"></ImageView>
</LinearLayout>
-->
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
images.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#color/backcolor">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="234dp"
android:layout_marginTop="60dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
package com.triumph.greetings;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class Launch extends Activity implements OnClickListener {
boolean isListVisible=true;
ImageButton img1,img2;
ListView lv;
String[] festivales= new String[]{"Christmas","New year","BirthDay","Easter","Wedding","Valentine","Love","Nature"};
ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1=(ImageButton)findViewById(R.id.imageButton1);
img2=(ImageButton)findViewById(R.id.imageButton2);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Launch.this,android.R.layout.simple_list_item_1,festivales);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int postion,
long arg3)
{
// TODO Auto-generated method stub
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
Toast.makeText( Launch.this, ((TextView) v).getText(),Toast.LENGTH_LONG).show();
//state=true;
displayimages();
}
private void displayimages()
{
// TODO Auto-generated method stub
// fetch the images from server and shows in GridView
// call linear layout
setContentView(R.layout.images);
img=(ImageView)findViewById(R.id.imageView1);
img.setBackgroundColor(color.background_light);
}
});
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.imageButton1:
if(isListVisible)
{
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
}
else
{
lv.setVisibility(View.VISIBLE);
isListVisible=true;
}
break;
//Toast.makeText(getBaseContext(), "Hi", Toast.LENGTH_LONG).show();break;
case R.id.imageButton2:
break;
}
}
}
package com.example.hello.word;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class OptionsMenu extends Activity{
MediaPlayer ourSong;
Button btn1;
Intent openStartingPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menuoptions);
final ToggleButton onTog = (ToggleButton) findViewById(R.id.toggleButton1);
onTog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (onTog.isChecked()){
ourSong.start();
}else{
ourSong.stop();
}
}
});
I have an error under all the Buttons and its shows me for example: "menuoptions cannot be resolved or is not field".. the same error on togglebutton1
my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Volume"
android:textSize="40dp"
android:textStyle="bold"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="121dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="ToggleButton" />
<Button
android:id="#+id/button1"
android:layout_width="178dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:text="Back" />
</LinearLayout>
I tried to clean the project.. delete the R class and everything! what I can do to fix that Error?
Thanx for helpers..
You are getting that error because your are importing android.R you have to remove this import.
Try removing the line import android.R; from your code, this should fix your problem
When you press control + shift + O to organise your imports in eclipse, it sometimes adds the import for android.R
Here is a similar question
R cannot be resolved - Android error